QComboBox ile ilgili bir sorunum var. Tableview öğeleri ile bir combobox'a ihtiyacım var. Böyle ComboBox oluşturmak gerekC++ Qt QWboBox ile tablo görünümü
┌─────────┐
│ ▼ │
├─────────┤
│ index 0 │
├─────────┤
│ index 1 │
├─────────┤
│ index 2 │
├─────────┤
│ index 3 │
└─────────┘
:
Örneğin, QComboBox varsayılan olan
┌───────────────────┐
│ ▼ │
├─────────┬─────────┤
│ index 0 │ index 1 │
├─────────┼─────────┤
│ index 2 │ index 3 │
└─────────┴─────────┘
Ben örnek yazdım, ancak doğru çalışmıyor:
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++;
}
Sorun şu ki - 3 numaralı indeksi seçtiğimde, ComboBox dizin 2'yi ayarlayacaktır.
DÜZENLEME:
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());
}
It işi. Ama ne kadar doğru olduğunu bilmiyorum.
evet ,, çalıştığını, sadece model görünümü mimarisinin avantajlarından almaz. Tek boyut modelini tablonun iki boyutuna dönüştürmek için QAbstractProxyModel kullanabileceği bir iş parçacığı vardı, ancak elimde çözüm yok, derleme yapmadan – Swift