मुझे QComboBox के साथ कोई समस्या है। मुझे टेबलव्यू आइटम के साथ एक combobox की जरूरत है।सी ++ क्यूटी QComboBox tableview
उदाहरण के लिए, QComboBox के डिफ़ॉल्ट है:
┌─────────┐
│ ▼ │
├─────────┤
│ index 0 │
├─────────┤
│ index 1 │
├─────────┤
│ index 2 │
├─────────┤
│ index 3 │
└─────────┘
मैं इस तरह ComboBox बनाने की जरूरत है:
┌───────────────────┐
│ ▼ │
├─────────┬─────────┤
│ index 0 │ index 1 │
├─────────┼─────────┤
│ index 2 │ index 3 │
└─────────┴─────────┘
मैं नमूना लिखा था, लेकिन यह ठीक से काम नहीं:
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++;
}
समस्या यह है - अगर मैं इंडेक्स 3 चुनता हूं, तो कॉम्बोबॉक्स इंडेक्स 2 सेट करेगा।
संपादित करें:
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)));
// This one:
connect(table, SIGNAL(pressed(QModelIndex)), SLOT(setCheckBoxIndex(QModelIndex)));
//--SLOT--------
void MainWindow::setCheckBoxIndex(QModelIndex index)
{
QComboBox* combo = qobject_cast<QComboBox*>(sender()->parent()->parent());
combo->setModelColumn(index.column());
combo->setCurrentIndex(index.row());
}
यह काम करते हैं। लेकिन मुझे नहीं पता कि यह कितना सही है?
हाँ ,, यह काम करता है, बस मॉडल-व्यू वास्तुकला का फायदा नहीं उठाता है। थ्रेड था कि कोई भी एकल आयाम मॉडल को तालिका के दो-आयाम में परिवर्तित करने के लिए QAbstractProxyModel का उपयोग कर सकता है, लेकिन मेरे पास समाधान नहीं है, – Swift