Qt - QTableView - Clickable button in table row - c++

I require a button/link within a table row of a QTableView. This is to open a dialog to allow that row to be edited more efficiently.
After hours of looking on the web I am yet to find a decent example.
I am aware that this is likely to be done using a QItemDelegate, but I am unsure how to have a functional widget within the row without forcing the item into edit mode first.
Any help would be greatly appreciated.

You can use setIndexWidget for that, see the Qt documentation for more information.
As an example, to embed a push button in the first column of the second row (untested code):
tableView->setIndexWidget(tableView->model()->index(2, 1), new QPushButton);

You could emulate the functionality of a link by underlining the clickable text, then capturing the cell click via the cellClicked(row, col) signal and check that col == editColumn. Then row would correspond to which item you are editing.
For example,
Data Name | Value 1 | Value 2 | Edit
connect (tableWidget, SIGNAL(cellClicked(int,int)), this, SLOT(editSlot(int, int)));
...
void ClassName::editSlot(int row, int col){
if (col == 3) {
doWork(row);
}
}

Related

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

QTreeView/QHeaderView resize only first column to stretch and interactive both

I have a QTreeView which has 5 columns. The requirement is to stretch only the first column but all the 5 columns should be resizable by user(i.e. they can be interactive as well) also. So I wrote the following code:
int numCols = myModel->columnCount();
for(int i=0;i<numCols;i++)
{
myQTreeView->resizeColumnToContents(i);
if(i==0)
{
myQTreeView->header()->setResizeMode(i,QHeaderView::Stretch);
}
else
{
myQTreeView->header()->setResizeMode(i,QHeaderView::Interactive);
}
}
But this does not work as expected for 1st column. Although the 1st column stretches but it is not resizable/interactive like rest of the columns. Hence I want to add stretch+interactive for the 1st column. Rest of the 4 columns might as well be interactive only.
Is this possible?
According to the documentation for QHeaderView::ResizeMode, when the mode has been set to QHeaderView::Stretch...
The size cannot be changed by the user or programmatically.
So, no, I don't think it's possible to achieve what you want using the standard APIs.
You could try setting the resize mode to QHeaderView::Custom and overriding the various mouse event handlers in the QHeaderView by either installing an event filter on the existing QHeaderView or by creating your own class inheriting from QHeaderView and installing an instance of it in your view via QTreeView::setHeader.

Eclipse scout Neon disabled only one row in table

I would like to disable only one row in Table.
So after performing some action I would like to disabled one row. (I have table that is editable and "disabled" row for me mean that I can't change value)
row.setEnabled(false);
does not work, because it disable whole table.
Edit :
I actually need enabling/ disabling cells in rows.
I figure it out.
Interfaces of row and cell doesn't have this specification, but if you use method
#Override
protected void execDecorateCell(final Cell view, final ITableRow row, final IColumn<?> col) {
super.execDecorateCell(view, row, col);
// set cell editable.
view.setEditable(false);
}
And this disable specific row from editing.
If you would like to disable specific row, you could limit it by row and col.
So you can disable really specific cell. Don't forget to add
else {
view.setEditable(true);
}
so when this method is called again it will update old cells (if they maybe are enabled this time).

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.

QTableWidget change selection mode

When clicking on QTableWidget cell, it selects only the cell. How to configure tablewidget, so that when click on a cell , the whole row will be selected which contains the cell?
It can be done using signal,slots. I'm curious is there standard way doing it?
Simply use setSelectionBehavior
QTableView * tmp = new QTableView();
tmp->setSelectionBehavior(QAbstractItemView::SelectRows);
http://doc.qt.io/qt-5/qabstractitemview.html#SelectionBehavior-enum