You can use proxy model
{
public:
explicit HeaderProxy
(QObject * parent
= 0) {
;
}
{
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.
listView.show();
comboBox.show();
HeaderProxy m;
m.setSourceModel(tableWidget.model());
listView.setModel(&m);
comboBox.setModel(&m);
//connect the required signal of comboBox to listView
class HeaderProxy : public QAbstractProxyModel
{
public:
explicit HeaderProxy(QObject * parent = 0)
: QAbstractProxyModel(parent)
{
;
}
QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const
{
if(!parent.isValid())
return createIndex(row, column);
return QModelIndex();
}
QModelIndex parent(const QModelIndex & /*child*/) const
{
return QModelIndex();
}
QModelIndex mapToSource(const QModelIndex & proxyIndex) const
{
if(!proxyIndex.isValid())
return QModelIndex();
return createIndex(proxyIndex.row(), proxyIndex.column());
}
QModelIndex mapFromSource(const QModelIndex & sourceIndex) const
{
if(!sourceIndex.isValid())
return QModelIndex();
return index(sourceIndex.row(), sourceIndex.column() );
}
int rowCount(const QModelIndex &parent) const
{
return sourceModel()->columnCount(parent);
}
int columnCount(const QModelIndex &/*parent*/) const
{
return 1;
}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
{
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 listView
To copy to clipboard, switch view to plain text mode
Bookmarks