ChrisW67
23rd January 2011, 23:52
Hi All,
I have a quick question regarding the behaviour of QComboBox with a multi-column QTableView custom view.
I have the combo box set up like this:
TypeModel *model = new TypeModel(this);
QComboBox *combo = new QComboBox(central);
QTableView *view = new QTableView(this);
view->setModel(model);
view->resizeColumnsToContents();
view->resizeRowsToContents();
view->verticalHeader()->setVisible(false);
view->setMinimumWidth(view->horizontalHeader()->length());
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded );
view->setSelectionMode(QAbstractItemView::SingleSelectio n);
view->setSelectionBehavior(QAbstractItemView::SelectRows );
view->setAutoScroll(false);
combo->setModel(model);
combo->setModelColumn(0);
combo->setView(view);
TypeModel is a simple table model returning code-meaning pairs. The flags() method looks like:
// column 0 is the code, column 1 is the meaning
Qt::ItemFlags TypeModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags theFlags = QAbstractItemModel::flags(index);
if (index.isValid()) {
theFlags &= ~Qt::ItemIsEditable;
if (index.column() != 0)
theFlags &= ~Qt::ItemIsSelectable;
}
return theFlags;
}
If lines 7 and 8 are as above then the combo pop-up only allows clicking on items in the first column, and the resulting code is displayed in the combo box. If lines 7-8 are omitted then the user can click on either column of the popup but the text of the cell they clicked on, which may be a meaning, is put in the combo box. Setting the model column for the combo box does not change this. In either case the combo box current index is correct.
Is there a way to have the popup clickable anywhere but have the code placed in the combo box?
Regards,
Chris
Full code attached. I am using Qt 4.6.3 on Linux.
I have a quick question regarding the behaviour of QComboBox with a multi-column QTableView custom view.
I have the combo box set up like this:
TypeModel *model = new TypeModel(this);
QComboBox *combo = new QComboBox(central);
QTableView *view = new QTableView(this);
view->setModel(model);
view->resizeColumnsToContents();
view->resizeRowsToContents();
view->verticalHeader()->setVisible(false);
view->setMinimumWidth(view->horizontalHeader()->length());
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded );
view->setSelectionMode(QAbstractItemView::SingleSelectio n);
view->setSelectionBehavior(QAbstractItemView::SelectRows );
view->setAutoScroll(false);
combo->setModel(model);
combo->setModelColumn(0);
combo->setView(view);
TypeModel is a simple table model returning code-meaning pairs. The flags() method looks like:
// column 0 is the code, column 1 is the meaning
Qt::ItemFlags TypeModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags theFlags = QAbstractItemModel::flags(index);
if (index.isValid()) {
theFlags &= ~Qt::ItemIsEditable;
if (index.column() != 0)
theFlags &= ~Qt::ItemIsSelectable;
}
return theFlags;
}
If lines 7 and 8 are as above then the combo pop-up only allows clicking on items in the first column, and the resulting code is displayed in the combo box. If lines 7-8 are omitted then the user can click on either column of the popup but the text of the cell they clicked on, which may be a meaning, is put in the combo box. Setting the model column for the combo box does not change this. In either case the combo box current index is correct.
Is there a way to have the popup clickable anywhere but have the code placed in the combo box?
Regards,
Chris
Full code attached. I am using Qt 4.6.3 on Linux.