One QStandardItemModel for different QTableView, Generic vs Specific Views - qt

I really don't know if this makes sense but this is what I trying to do:
I'm doing my game's editor in QT. Currently I'm using a QStandardItemModel to store all my scene items. These items have names, position, Textures(vector of Texture which is a custom class), Animations (vector of Animation), etc.
I find it useful to have one item for row cause I can add or remove these items easily besides having them in a single place so changing this model should affect the entire app.
Now, I'm trying to do specific views for say the "Textures" of a certain item. This QTableView should show the texture's name, path etc. So, basically how can I grab the vector of Textures in the general model and fill another view without doing another model?

You will want to use a QSortFilterProxy Model. Set one up like this.
QTableView *tableView = new QTableView;
MyItemModel *sourceModel = new MyItemModel(this);
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(sourceModel);
proxyModel->setFilterKeyColumn(column_#_to_filter_by);
proxyModel->setFilterRegExp(a_regex_that_matches_the_item_you_want_to_display);
tableView->setModel(proxyModel);
You should be able to use one model and different proxies to setup different views.

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.

Select objects on a JavaFX Pane

I've created an application to draw objects on a JavaFX pane (rectangle, paths, custom controls etc..).
Now I want to be able to select those objects to move, copy or group them. The javafx Pane don't have a selectionModel by default and I somehow didn't find out how to implement such function.
If someone got an Idea on how to do this, I'll be grateful
You need to create your own selection model.
First you create a class SelectionModel. In that you put a Set. In order to put nodes into that set, you have to create a mouse handler that adds nodes to the model and removes them from the model depending on shift/ctrl pressed while you click on the nodes.
When you move the selection via mouse, you get the position of the currently clicked on node and instead of moving the single node in the event handler you move all nodes of the list in the SelectionModel.
In order to group them, you need to create a class/collection in which you can store the various nodes you selected. Usually the group is just a parent node. But that varies depending on your requirements.
Copy/Paste is a different matter. You need to create some kind of factory that creates and positions new nodes depending on the nodes in your selection model.
Here's an example with code for a start. It shows you how to select nodes.

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.

Qt display not all fields from Model

I'm displaying the query from an QSqlModel in a table view. e.G.
SELECT id, name FROM person;
I don't want to display id in the view, however I need the id when the user clicks on a row in that view.
Is there a model in Qt, which allows me to display only specified columns in views but also providing access to invisible ones?
I used to do it by hiding the columns e.g.:
ui->myview->setColumnHidden( 0, true );
But probably there is a better solution.
Since the OP amended the question to exclude using QTableView the only alternative that would seem to work is the creation of a new class derived from QSortFilterProxyModel. One can override the filterAcceptsColumn method
Documentation on the class QSortFilterProxyModel can be found here
QSortFilterProxyModel can be used for sorting items, filtering out items, or both. The model transforms the structure of a source model by mapping the model indexes it supplies to new indexes, corresponding to different locations, for views to use. This approach allows a given source model to be restructured as far as views are concerned without requiring any transformations on the underlying data, and without duplicating the data in memory.
You'd also have to override MapToSource and SourceToMap . There is a good example of usage in this StackOverflow's answer

Programmatically adding a new row to a QAbstractListModel subclass

Within an already-instantiated QAbstractListModel subclass, how do I add a row with data in each column, and have the associated QListView display the new row?
It seems that the only way to do it is to reimplement insertRow and setData within my model, and then hack them together in some sort of sequence within another function to add a row. Must I do this? Surely Qt must make it easier to add a new row.
Thanks much!
--Dany.
Just change your model's data storage, in between beginInsertRows() and endInsertRows().
For instance, let's say you have a flat list model and your model stores the data internally in a QVector m_data. You want to prepend the list, i.e. insert a row at position 0:
beginInsertRows( QModelIndex(), 0, 0 ); //notify views and proxy models that a line will be inserted
m_data.prepend( somedata ); // do the modification to the model data
endInsertRows(); //finish insertion, notify views/models
I'm afraid you have to do it that way. From the docs:
Models that provide interfaces to resizable list-like data structures can provide implementations of insertRows() and removeRows().

Resources