Start by extending QComboBox with a "currentIndexChanged" signal with QModelIndex parameter:
{
Q_OBJECT
public:
{
connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(emitCurrentIndexChanged(int)));
}
signals:
void currentIndexChanged(const QModelIndex& index);
private slots:
void emitCurrentIndexChanged(int index)
{
emit currentIndexChanged(rootModelIndex().child(index, modelColumn()));
}
};
class ComboBox : public QComboBox
{
Q_OBJECT
public:
ComboBox(QWidget* parent = 0) : QComboBox(parent)
{
connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(emitCurrentIndexChanged(int)));
}
signals:
void currentIndexChanged(const QModelIndex& index);
private slots:
void emitCurrentIndexChanged(int index)
{
emit currentIndexChanged(rootModelIndex().child(index, modelColumn()));
}
};
To copy to clipboard, switch view to plain text mode
Then you can just connect ComboBox::currentIndexChanged(QModelIndex) to QAbstractItemView::setRootIndex(QModelIndex) and you're done.
int main(int argc, char* argv[])
{
ComboBox comboBox;
comboBox.setModel(&dirModel);
comboBox.show();
listView.setModel(&dirModel);
listView.show();
return a.exec();
}
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
QDirModel dirModel;
ComboBox comboBox;
comboBox.setModel(&dirModel);
comboBox.show();
QListView listView;
listView.setModel(&dirModel);
listView.show();
a.connect(&comboBox, SIGNAL(currentIndexChanged(QModelIndex)), &listView, SLOT(setRootIndex(QModelIndex)));
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks