Does tableView->horizontalHeader()->model() give you a suitable model to use as the list model?
Does tableView->horizontalHeader()->model() give you a suitable model to use as the list model?
Maybe. Do you mean that should set the horizontalHeaderModel as the model for the QListView?
I can set the table model as the model for the QListView and select which column to display with ; setModelColumn(0). But I need to extract the headers in some way to display them in a QComboBox. I think I need to use the same model in both views to be sure that they will be updated whenever data is changed in the model.
Is there a way to change QListView to use the model's headerData() and coulmnCount() instead of data() and rowCount()?
You can use proxy model
Qt Code:
{ public: { ; } { if(!parent.isValid()) return createIndex(row, column); } { } { if(!proxyIndex.isValid()) return createIndex(proxyIndex.row(), proxyIndex.column()); } { if(!sourceIndex.isValid()) return index(sourceIndex.row(), sourceIndex.column() ); } { return sourceModel()->columnCount(parent); } { return 1; } { return sourceModel()->headerData(index.row(), Qt::Horizontal, role); } }; //elsewhere TableWidget tableWidget; tableWidget.show(); // Populate rows, columns and other data. QListView listView; listView.show(); QComboBox comboBox; comboBox.show(); HeaderProxy m; m.setSourceModel(tableWidget.model()); listView.setModel(&m); comboBox.setModel(&m); //connect the required signal of comboBox to listViewTo copy to clipboard, switch view to plain text mode
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
Thanks!
It is almost what I want. When I use the proxy model in the combobox the items are not selectable. do I have to implement mapSelectionFromSource and mapSelectionToSource for them to be selectable?
If you are using the proxy in the example which I posted you should be able to select items in QComboBox.
BTW selectable option depends on Qt::ItemFlags HeaderProxy::flags(const QModelIndex &index) const; that too if you implement it, else the base class QAbstractProxyModel implementation should enable you to select by default
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
For some reason, the combobox still displays grayed out items.
I tried to implement
Qt::ItemFlags flags(const QModelIndex & index) const
{
return Qt::ItemIsSelectable;
}
but the combobox didn't respond to that.
1. Don't implement the flags() function
or
2. return Qt::ItemSeletable | Qt::ItemEnabaled
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
Thanks
Adding Qt::ItemIsEnabled did the trick!
This seems to only work if the row and column count are static and not changing. When Columns are inserted and removed I get empty feilds everywhere.
What do we need to reimpliment to get the proxy to track the source as it changes size?
Thanks
Bookmarks