Qt:使用 QStandardItemModel 创建 QItemDelegate 以在 QTableView 中拥有自定义编辑器(组合框)
-
12-12-2019 - |
题
我需要在 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)));
}
}
解决方案
假设在这两种 mainwindow.cpp 情况下,您显示的代码都在构造函数中,您会执行经典的“来自 java、python、perl 或任何您不关心内存管理的语言”错误来分配您的模型和/或委托在堆栈上而不是在堆上。
在 C++ 中,在堆栈上分配的内容(例如 QStandardItemModel model(4,2);
)从函数返回时被销毁,而在堆上分配的东西(例如 QStandardItemModel* model = new QStandardItemModel(4,2);
活到你为止 delete
它)。情况 1 有效,因为在堆栈上分配的内容 main()
在应用程序运行期间有效。
底线:对于情况 3,执行 ComboBoxDelegate* delegate = new ComboBoxDelegate();
和 ui->tablePoint->setItemDelegateForColumn(1,delegate);