Insert row on top of a qtablewidget - c++

I'll like to know if it's possible to insert a row on top of a qtablewidget ?
Something like:
ui->myqtablewidget->insertRow(0);

Just a heads up - I'm answered based on the documentation and C++
knowledge, not direct experience with Qt.
It would seem that QTableWidget has an insertRow() function. You simply need to specify where you want the row placed. Read the documentation here.
Thus, it would seem from the documentation, you can insert a row at the beginning ("top") of your table with that exact line of code...
ui->myqtablewidget->insertRow(0);
...assuming ui and myqtablewidget are declared properly.

Related

How to hide the first column of a wxListCtrl in wxWidgets?

The context
In a wxWidgets (version 3.0.2) C++ application, I am trying to hide the first column of a wxListCtrl.
I did not find a member function to do this so I tried to set the width of the column to 0:
myListCtrl->SetColumnWidth(0, 0);
first argument being the column ID and second one the width in pixels (wxListCtrl documentation).
After running the program, the header of the first column is hidden as I wanted but the data of each row of the first column overlaps the data of each row of the second column (which is not hidden). It is obviously not what I want. The header and the data of the first column should be hidden.
The question
In wxWidgets 3.0.2, is there a way to hide the first column (header and data of each rows) of a wxListCtrl?
I don't believe you can. You have a few options.
Delete the column using DeleteColumn(int columnIndex). You aren't losing any data, just the display of it, so you can always re-insert the column and repopulate it if you need to re-add it. Obviously this could be time consuming if your data is excessively large.
Depending on your application, just don't create the column in the first place. You don't say why you want to hide it, so if you just don't want it, don't add it.
Implement your control as a virtual control which gives your application control over what to display where. The burden of data display management falls to you to do manually but you have a great deal more flexibility. Inherit the class with wxLC_VIRTUAL style and implement OnGetItemText http://docs.wxwidgets.org/3.0/classwx_list_ctrl.html#a92370967f97215e6068326645ee76624
Edit:
To expand on the comment question, how to get the selected item index:
The wxListCtrl is a little weird when it comes to selected items. I'm sure it has to do with needing to support report, icon, etc. different views. When dealing with a multi-column report mode, you might find that you can only select items in the first column. If you are on Windows, it should automatically be set to "Full Row Select" but I don't know about other OSs.
Anyway, here is a utility method that returns the first selected item (note that you can support multi-selection if you want to).
//Get the item currently selected
int ListView::GetItemSelected() const
{
for(int i=0; i<GetItemCount(); ++i)
if (GetItemState(i, wxLIST_STATE_SELECTED) == wxLIST_STATE_SELECTED)
return i;
return -1;
}
If you want (and it makes sense), you can connect the list item selected event.
this->Connect(wxEVT_COMMAND_LIST_ITEM_SELECTED, wxCommandEventHandler(ListView::selected_Changed), NULL, this);
and within that event handler, get the selected item and do what needs doing (depending entirely on your application).
You will note that I'm using a derived class here which just makes things a lot easier but you don't have to. You can connect to something like MyMainForm::sqlResults_selectedChanged or whatever.
There is more than one way to accomplish all this and you can also find some good suggestions and help here: https://wiki.wxwidgets.org/WxListCtrl

Best way to make a list of rows and column in wxpython with each row having buttons in last column?

I want to make a list of n rows with m columns. Last column must contain a button which on clicking deletes the entire row which that button belongs to.
Click here to see my desired GUI.
I have already looked for wx.grid and didn't get any success.
My questions are
Can this be achieved by using wx.ListCtrl?. If yes then how?
Which is the best wx widget other than wx.grid to achieve this.
Any sample code or illustration will be appreciated.
Thanks

Qt Delete selected row in QTableView

I want to delete a selected row from the table when I click on the delete button.
But I can't find anything regarding deleting rows in the Qt documentation. Any ideas?
You can use the bool QAbstractItemModel::removeRow(int row, const QModelIndex & parent = QModelIndex()) functionality for this.
Here you can find an example for all this.
Also, here is an inline quote from that documentation:
removeRows()
Used to remove rows and the items of data they contain
from all types of model. Implementations must call beginRemoveRows()
before inserting new columns into any underlying data structures, and
call endRemoveRows() immediately afterwards.
The second part of the task would be to connect the button's clicked signal to the slot executing the removal for you.
If you are removing multiple rows you can run into some complications using the removeRow() call. This operates on the row index, so you need to remove rows from the bottom up to keep the row indices from shifting as you remove them. This is how I did it in PyQt, don't know C++ but I imagine it is quite similar:
rows = set()
for index in self.table.selectedIndexes():
rows.add(index.row())
for row in sorted(rows, reverse=True):
self.table.removeRow(row)
Works perfectly for me! However one thing to know, in my case this function gets called when a user clicks on a specific cell (which has a pushbutton with an 'X'). Unfortunately when they click on that pushbutton it deselects the row, which then prevents it from getting removed. To fix this I just captured the row of the sender and appended it to the "remove_list" at the very beginning, before the "for loops". That looks like this:
rows.add(self.table.indexAt(self.sender().pos()).row())
You can use another way by deleting the row from database, then clear the model and fill it again, this solution is also safe when you are removing multiple rows.

MFC CListCtrl updating text of any cell

This question is to understand how to update any row programatically.
Details.
I have a listcrtl, that accepts the data from either from a file or from the edit controls in the dialog. When the items are added I will know its position, that I added, so I can change its subitem texts. I have even implemented the sort functionality in the list, so now the position keeps changing. I have an identifier column for each row, so that I can recognize the row.
Now, from an out side event, if I have to change an other columns value of an ID that I know , I have to first find the position of the item by comparing the id column, then with that position, I have set the subitemtext.
This works fine except that it takes time to find the row first then it need to update the column.
Now, in order to get the row directly, I need some help.
I have gone through
http://msdn.microsoft.com/en-us/library/windows/desktop/hh298346(v=vs.85).aspx
But this does not use MFC. Please help me achieving this.
If you have many items you should consider switching to Virtual Lists. It is the fastest way to access the data. If you do not want to invest time to this, then the easiest way for you will be the following:
When you populate the CListCtrl store the ID of each item in the item data using the SetItemData() method. The ID will always be associated with the item, even after re-sorting.
When you need to locate the required item, just scan all items, but do not use GetItemText(). Use GetItemData() instead. This will be faster

How do I add a header with data to a QTableWidget in Qt?

I'm still learning Qt and I am indebted to the SO community for providing me with great, very timely answers to my Qt questions. Thank you.
I'm quite confused on the idea of adding a header to a QTableWidget. What I'd like to do is have a table that contains information about team members. Each row for a member should contain his first and last name, each in its own cell, an email address in one cell, and office in the other cell. I'd to have a header above these columns to name them as appropriate.
I'm trying to start off easy and get just the header to display "Last" (as in last name). Here is my code.
int column = m_ui->teamTableWidget->columnCount();
m_ui->teamTableWidget->setColumnCount(column+1);
QString* qq = new QString("Last");
m_ui->teamTableWidget->horizontalHeader()->model()->setHeaderData(0,
Qt::Horizontal, QVariant(QVariant::String, &qq));
My table gets rendered corretly, but the header doesn't contain what I would expect. It contains 1 cell that contains the text "1".
I am obviously doing something very silly here that is wrong, but i am lost. I keep pouring over the documentation, finding nothing.
Thanks for any and all help.
The easiest solution is setHorizontalHeaderLabels(myListOfHeaderLabels).
I see one potential problem, and also an easier way to do this.
First, the problem:
QString* qq = new QString("Last"); // <- qq is a pointer to a string.
m_ui->teamTableWidget->horizontalHeader()->model()->setHeaderData(0,
Qt::Horizontal,
QVariant(QVariant::String, &qq)); // <- You take the address of a pointer, or create a handle.
I think you want to do this instead:
QString* qq = new QString("Last");
m_ui->teamTableWidget->horizontalHeader()->model()->setHeaderData(0,
Qt::Horizontal, QVariant(QVariant::String, *qq));
Now, the easier way to set the data for a header item:
m_ui->teamTableWidget->horizontalHeaderItem( 0 )->setText( "Last" );
At the request of the person who steered me toward the right place, I am posting the way I accomplished this as an answer and I am accepting it.
m_ui->teamTableWidget->setColumnCount(m_ui->teamTableWidget->columnCount()+1);
QTableWidgetItem* qtwi = new QTableWidgetItem(QString("Last"),QTableWidgetItem::Type);
m_ui->teamTableWidget->setHorizontalHeaderItem(0,qtwi);
For posterity:
The default implimentations of setHeaderData() and headerData() in QAbstractItemModel do not do anything. You can (should?) (re)impliment headerData() in order to return useful a label.