我需要在 Qt 中实现一个表格,在特定列的每一行上显示一个组合框。
到目前为止,基于这个例子: http ://doc.qt.nokia.com/4.7-snapshot/itemviews-spinboxdelegate.html 和这个问题: QStandardItem + QComboBox 我成功地创建了一个 QItemDelegate。
我的问题是,如果我从 main.cpp 中的 main() 函数实现它,一切正常,但如果我在 Qt Designer 中插入表以使用它,然后在 mainwindow.cpp 中的 MainWindow 类的函数中使用它,它就不能正常工作.
你能给我一个线索吗?提前致谢!
main.cpp 上的委派(效果很好,当我双击第二列时,它会显示一个组合框):
QStandardItemModel model(4, 2);
QTableView tableView;
tableView.setModel(&model);
ComboBoxDelegate delegate;
tableView.setItemDelegateForColumn(1,&delegate);
tableView.horizontalHeader()->setStretchLastSection(true);
for (int row = 0 ; row < 4; ++row) {
for (int col = 0; col < 2; ++col) {
QModelIndex index = model.index(row, col, QModelIndex());
model.setData(index, QVariant((row+1) * (col+1)));
}
}
tableView.show();
mainwindow.cpp 替代方案 1 上的委派(它显示一个空表)
QStandardItemModel model(4,2);
ui->tablePoint->setModel(&model);
ComboBoxDelegate delegate;
ui->tablePoint->setItemDelegateForColumn(1,&delegate);
ui->tablePoint->horizontalHeader()->setStretchLastSection(true);
for (int row = 0 ; row < 4; ++row) {
for (int col = 0; col < 2; ++col) {
QModelIndex index = model.index(row, col, QModelIndex());
model.setData(index, QVariant((row+1) * (col+1)));
}
}
mainwindow.cpp 替代方案 2 上的委派(它显示了表格,但是当我双击第二列时,它不显示组合框。而是显示常规的旋转框):
QStandardItemModel* model = new QStandardItemModel(4,2);
ui->tablePoint->setModel(model);
ComboBoxDelegate delegate;
ui->tablePoint->setItemDelegateForColumn(1,&delegate);
ui->tablePoint->horizontalHeader()->setStretchLastSection(true);
for (int row = 0 ; row < 4; ++row) {
for (int col = 0; col < 2; ++col) {
QModelIndex index = model->index(row, col, QModelIndex());
model->setData(index, QVariant((row+1) * (col+1)));
}
}