Using an existing C ++ model (row, column) with QtQuick (Grid, TableView)

I have some models that use the QAbstractItemModel rules to retrieve and provide data for a table.

My sample model has several columns and rows. Unfortunately, QtQuick widgets can only process one column. Other "columns" are added to QtQuick widgets through roles. Thus, several columns in a view correspond to one column in the model. Other model columns are ignored, as explained in this question and its answer

I thought it shouldn't be too difficult to provide an abstraction for QML that will be used on the C ++ side (like QAbstractProxyModel ), which, when querying for a row N modulo, is output from the column of the original model and extracts data from the final actual column. This would seem to work for the Grid , but will not work for the TableView , since it relies on the TableViewColumn and role names instead of using only continuous row indexes. For this, the proxy model will have to distinguish between the role from which the column of the source model is extracted.

The snippet present in the answer http://qt-project.org/forums/viewthread/41793 does this to accept QSqlTableModel , but still skips the translation of a large number of signals you can use. As I assume, if the original SQL model columnsInserted , it should translate to the dataChanged signal with the new roles selected for this column and changing the names of the available roles. QMLifyProxyModel seems to look better, but not ready and dead for 4 years. It seems.

How can we better fix this so that the two worlds work freely, according to an official recommendation? Why are QtQuick views not using the notation (row, column) that QAbstractItemModel and QTableView have already used?

+6
source share
1 answer

Part of the difficulty is that the way you use QtQuick models is more restrictive than QWidget 's views, so you can try to do the opposite; transforming models to use roles and using a proxy model to map roles to column indexes + headerData in the same way as TableViewColumn, but for QWidget views. Column insertion and deletion signals should be easier to handle if the source is a static number of roles, rather than a variable number of columns.

Unfortunately, this does not help for built-in or more complex models.

Most of the QtQuick views were designed for 1D models when phones were his design goal. Roles were used to map the unordered properties of one row / element to their name.

TableView appeared a few years later and it seems that it should have done an extra mile to allow the use of 2D models, but probably it would have been some extra work at a time when QtQuick was already a pretty beast, especially since TableView was mostly written in QML itself.

+1
source

Source: https://habr.com/ru/post/976644/


All Articles