Which Class has to be reimplemented for custom Selection in QTableWidget? - qt

QTableWidget consists of several Selection Modi, which are selectable with the method
setSelectionMode (QAbstractItemView::SelectionMode mode)
None of the given modi fits the type of interactive selection i want to have. I want to implement a selection mode where when a user clicks in one table cell, and then shift+clicks in another table cell, the resulting selection is not a sum of columns and rows between these two, but starts from the first clicked cell, goes in read direction along row by row, ending by the second click. I hope i made clear what i want to do.
Do i need to overwrite the QItemSelectionModel or the QTableWidget?
Where are the user clicks for selection are processed?

You need to create a descendant of QItemSelectionModel and reimplement select.
virtual void select(const QModelIndex & index, QItemSelectionModel::SelectionFlags command)
virtual void select(const QItemSelection & selection, QItemSelectionModel::SelectionFlags command)
When reimplementing select you can call QItemSelectionModel::select with different arguments to achieve the needed result.
Then assign an instance of the selection model to QTableWidget with setItemSelectionModel.

Related

How to change the order of QTableView rows by dragging and store the changes to the model?

I want to know how to change the order of QTableView rows by dragging, and store the order changes to the model?
I use QTableView as view and QSqlTableModel as model. I am using Qt 5.15.
I set:
ui->table_view->setSelectionMode(QAbstractItemView::SingleSelection);
ui->table_view->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->table_view->setEditTriggers(QAbstractItemView::NoEditTriggers);
Methods I tried:
1.
ui->table_view->setSectionsMovable(true);
ui->table_view->setDragEnabled(true);
ui->table_view->setDragDropMode(QAbstractItemView::InternalMove);
ui->table_view->setAcceptDrops(true);
It doesn't take effect. The row cannot be dragged.
2.
ui->table_view->verticalHeader()->setSectionsMovable(true);
ui->table_view->verticalHeader()->setDragEnabled(true);
ui->table_view->verticalHeader()->setDragDropMode(QAbstractItemView::InternalMove);
ui->table_view->verticalHeader()->setAcceptDrops(true);
The row can be dragged by vertical header. But the order of changes will not affect the model.
As far as I know the QTableView does not call moveRows method of the model by itself. Instead drag and drop actions call *mimeData methods.
So, one option would be the following:
In your model you need to implement mimeData and dropMimeData functions. When you drag and drop a row the view will ask for mimeData() of the given row. If you are sure that you need only internal moves you can encode the selected row indices to QMimeData. Then in the dropMimeData() you decode the indices that were selected and use them to call your moveRows() implementation. Return false from this function to prevent removing of the moved out rows.
Another option can be to override the QTableView methods such as dropEvent() in a way, that it calls the model moveRows() method directly.

How to programmatically select multiple rows in QListView?

I have written a program where the user can select multiple rows from a QListView. Sometimes the program need to load a set of selection at startup, so the program needs to be able to select multiple rows from code. How can this be done?
you can use QItemSelectionModel class to manage the view selection and modify them.
from document :
Whenever you want to modify the selected items use select() and provide either a QItemSelection, or a QModelIndex and a QItemSelectionModel::SelectionFlag.

How to update selection in the QTableView after drag row

I have custom QTableView class that shows content of custom model based on QAbstractItemModel. In the model I've implemented all needed methods to support changing rows order by DragAndDrop ( using dropMimeData()).
But I do not know how to update selection in the view after model (and view) changed.
For example:
user clicks on the row, it becomes 'selected';
user drags this row to other place;
rows are swaps in the model and view;
BUT selection stays on the first selected row.
How model can notify view to change selection?
NOTE: I cant to create additional signals and slots because don't use MOC.
The solution is:
In function dropMimeData() need to use beginMoveRows() and andMoveRows() around place where data changed.
Need to process signal QAbstractItemModel::rowsMoved of the model, where we can retrieve index of the target row.

Setting multiple selection in QTreeView

I have a need here regarding multiple selection of items in the QTreeView. I have two widgets, QTreeView on left and another QGLWidget on the right. If I do a multiple selection, I must highlight the items in the glwidget. Vice versa, I need to highlight the items in the tree view if multiple selection is done on the glwidget. Currently, I am able to update single item by setting the current index of the tree view. Any ideas on how to update the selection of multiple items on the tree view with multiple selection on glwidget?
You can use the tree view's item selection model (treeView->selectionMode(), of type QItemSelectionModel). It has a signal selectionChanged() you can connect to to receive tree view selection changes and apply them to the GL view.
When receiving selection events from the GL view, you can use QItemSelectionModel::select() to propagate them to the tree view.
To enable multiselection on your treeview, call setSelectionMode( MultiSelection ).
Frank was faster, but I still post my (untested) code example for adding an item to the current selection:
treeView->selectionModel()->select(
treeView->model()->index(row, column, parent),
QItemSelectionModel::SelectCurrent);
There are other selection modes too, see the QItemSelectionModel reference. SelectCurrent is a short-hand for Select | Current, so means update current selection by selecting the given items. It does not mean "select as current selection" (replacing previous selection).
In Python (PyQt6) I do :
ui.my_treeView.setSelectionMode(ui.my_treeView.selectionMode().MultiSelection)
It works well.

How to detect doubleClick in QTableView

I'm using PyQt to create a GUI application. In a view inherited from QTableView, need to detect the row the user has selected when they double click a row. The table has sorting, but no editing.
How do I do it?
Note - tried the doubleClicked(int) signal. It is emitted by mouse buttons, not by data cells, so it was never fired. :(
Ian
I dont understand.
The doubleClicked signal of the QTableView has the signature
void doubleClicked ( const QModelIndex & index )
If you connect that signal you should obtain the correct QModelIndex.
No need to use SIGNALs anymore:
self.your_table.doubleClicked.connect(your_function)
"doubleClicked" being inherited from QAbstractItemView.
Once you have the modelIndex, (from Frank's comment above) you can use it to find which cell was double clicked.
def slotDoubleClicked(self, mi):
row = mi.row()
column = mi.column()
You then can use these row and col values to access the table with table.setItem(row, column, newdata) or other table method
Like #regomodo said, you can simply connect your function to the double click via:
self.your_table.doubleClicked.connect(your_function)
Then, if you want to know on which row the user double clicked, you can use the following code:
for idx in self.your_table.selectionModel().selectedIndexes():
row_number = idx.row()
column_number = idx.column()
It will return an integer corresponding to the row or the column number.
There will always only be a single value as the double click remove the previous selection.
If you link your function to a push button or another signal, you can receive a list containing multiple elements selected by the user.
For example, you can easily retrieve a list of all selected rows using this code:
rows = []
for idx in self.your_table.selectionModel().selectedIndexes():
rows.append(idx.row())
rows = list(set(rows))
This will return a list of all selected rows (The set function will also remove any duplicates).
Cheers!

Resources