QGraphicsItemGroup List - qt

I have multiple QGraphicsItemGroup drawn in my QGraphicsScene. I want to obtain the list of selected QGraphicsItemGroup.
I'm selecting using
setDragMode(QGraphicsView::RubberBandDrag);
The selectedItems() function returns a list of selected QGraphicsItem.
Is there any alternative to do the same for groups?
Edit
I have created a list of QGraphicsItemGroup as:
QList<QGraphicsItemGroup *> selectionList;
I want to get the selected items(groups) in this list.
How do I type cast QList<QGraphicsItem *> to obtain selectionList?

If you look at the documentation for QGraphicsItem::ItemIsSelectable, it states: -
The item supports selection. Enabling this feature will enable setSelected() to toggle selection for the item. It will also let the item be selected automatically as a result of calling QGraphicsScene::setSelectionArea(), by clicking on an item, or by using rubber band selection in QGraphicsView.
Since QGraphicsItemGroup is derived from QGraphicsItem, I suspect that by default, a QGraphicsItemGroup does not have this flag selected. So, for each group, set this flag and if you only want the groups returned from the rubber band selection, turn off the flags for all the other items.

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.

Display a QFileSystemModel in two Views: Tree View and a TableView/ListView of selected items

I have a model derived from QFileSystemModel. I extended it by a Role which holds a bool value which indicates whether a file has been selected for later use. This model is displayed in a qml TreeView. In this view I can select files via checkboxes. I have a list of QPersistentModelIndex of the selected indices stored in my model.
In a different view (probably a ListView or TableView), I would like to display only items, which have have been selected in the TreeView. In this other view I also want to be able to remove the selection. So the question is whether this can be done with only one model (or maybe a QSortFilterProxyModel inbetween) or whether I need to have two models communicating with each other.

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.

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

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.

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.

Resources