Unsolved model/view
-
@user4592357 said in model/view:
and also, what should i do if i want to display this data in tree view too? should i use another model?
Yes, you need another model. Tree and table models are different by their
rowCount()
andparent()
functions.
Tree models are more complex than table/list models. See this example.currently my model receives the hash of hashes and in constructor saves it as the matrix above
When it is fixed as you say, you can use it as it is. Just use the mapping from rows/cols to the corresponding QString indexes. Then you do not need to copy it again into another matrix-structure.
-
@raven-worx
i can't clearly picture how that mapping should be done.
for example, for the structure i wrote above? -
@user4592357
as i've already explained above, for example:QVariant MyModel::data( const QModelIndex &index, int role) const { if( role != Qt::DisplayRole ) return QVariant(); const int r = index.r(); const int c = index.c(); QString rowKey; switch( r ) { case 0: rowKey = "Key1"; break; case 1: rowKey = "Key2"; break; case 2: rowKey = "Key3"; break; ... } QHash<QString, QStringList> rowData = mHash.value( rowKey ); // mHash is 'QHash<QString,QHash<QString, QStringList>>' QString columnKey; switch( r ) { case 0: columnKey = "Key1"; break; case 1: columnKey = "Key2"; break; case 2: columnKey = "Key3"; break; ... } QStringList colData = rowData.value( columnData ); return QVariant::fromValue<QString>( colData .join(", ") ); }
-
@raven-worx
thanks. but what if i don't beforehand know the values of rows/columns?and what base model should i use for implementing model tree view?
-
@user4592357
For tables:QAbstractTableModel
For trees:QAbstractItemModel
But i suggest you first implement a table model, before jumping onto the tree model implementation, for the sake of simplicity.
About how many cells in the end are we talking actually?
If we are not talking about hundreds to thousands you can also use QStandartItems (with QTreeWidget/QTableWidget) as @VRonin suggested. -
@raven-worx
the cells are a lot actuallyand about your code snippet, what if the row/column values aren't known beforehand?
-
@raven-worx said in model/view:
If we are not talking about hundreds to thousands you can also use QStandartItems
I actually use tens of thousands tree elements with
QStandartItemModel
and works for me -
@VRonin said in model/view:
I actually use tens of thousands tree elements with QStandartItemModel and works for me
i never said it's not working, but it's a waste of memory.
And thus discouraged to do so (from my side). -
@raven-worx
about your code snippet, what if the row/column values aren't known beforehand? -
@user4592357
then reusing the QHash structure is not ideal, you could use your matrix approach then -
@raven-worx
can't i useQTableWidget
andQTreeWidget
since my models aren't reused? -
@user4592357 That's basically the same as using
QStandartItemModel
that we suggested at the beginning -
@VRonin
how is it the same?should i use the same
QStandartItemModel
for both views? -
I mean
QStandartItemModel
+QTreeView
=QTreeWidget
andQStandartItemModel
+QTableView
=QTableWidget
-
@VRonin @raven-worx
i have another question.so my table widget consists of 2 widgets:
QTableView
and aQTextEdit
under it to display info about a particular cell.the data container i use to set model data consists of two maps:
- data storage for table view
- data storage for text edit
how should i handle this in
data()
? -
should i user two different containers???