I have a problem with QComboBox. I need a combo box with table elements.
For example, the default is QComboBox:
βββββββββββ β βΌ β βββββββββββ€ β index 0 β βββββββββββ€ β index 1 β βββββββββββ€ β index 2 β βββββββββββ€ β index 3 β βββββββββββ
I need to create a ComboBox as follows:
βββββββββββββββββββββ β βΌ β βββββββββββ¬ββββββββββ€ β index 0 β index 1 β βββββββββββΌββββββββββ€ β index 2 β index 3 β βββββββββββ΄ββββββββββ
I wrote a sample, but it does not work correctly:
QTableView *table = new QTableView(this); QComboBox *cb = new QComboBox; ui->verticalLayout->addWidget(cb); cb->setView(table); QStandardItemModel *model = new QStandardItemModel(2,2); cb->setModel(model); int x = 0; int y = 0; for (int i=0; i<4; i++) { model->setItem(x, y, new QStandardItem("index" + QString::number(i))); if (i == 1) { x++; y = 0; } else y++; }
The problem is that if I select index 3, the ComboBox will set index 2.
EDIT:
QTableView *table = new QTableView(this); QComboBox *cb = new QComboBox; ui->verticalLayout->addWidget(cb); cb->setView(table); QStandardItemModel *model = new QStandardItemModel(2,2); cb->setModel(model); for (int i=0; i<4; i++) model->setItem( i%2, i/2, new QStandardItem("index" + QString::number(i)));
It works.
source share