我有一个扩展 QSpinBox
NumericEditor::NumericEditor(QWidget *widget): QSpinBox(widget)
我使用此编辑器编辑 QTableWidget
中的类型 QVariant::Int
QItemEditorCreatorBase *numericEditor = new QStandardItemEditorCreator<NumericEditor>();
factory->registerEditor(QVariant::Int, numericEditor);
数据正常输入到表中。忽略“颜色”一词的用法。它基于颜色编辑器示例。
QTableWidgetItem *nameItem2 = new QTableWidgetItem(QString("label2"));
QTableWidgetItem *colorItem2 = new QTableWidgetItem();
colorItem2->setData(Qt::DisplayRole, QVariant(int(4)));
table->setItem(1, 0, nameItem2);
table->setItem(1, 1, colorItem2);
微调框出现并在 QTableWidget 中正常工作。
我的愿望是访问表格在编辑 QVariant::Int
单元格时使用的 QSpinBox 实例,以便我可以设置最小值和最大值。
我该怎么做?
最佳答案
您可以使用 QTableWidget::setItemDelegateForColumn
在列上安装委托(delegate),当打开编辑器时,将调用其 createEditor
方法。
class MyDelegate : public QStyledItemDelegate {
public:
QWidget *createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const {
// You could create the widget from scratch or call
// the base function which will use the QItemEditor you already wrote
QWidget * editor = QStyledItemDelegate::createEditor(parent, option, index);
// do whatever you need to do with the widget
editor->setProperty("minimum", 100);
editor->setProperty("maximum", 100);
return editor;
}
};
关于c++ - 在 QItemEditorCreatorBase 中使用后取回 QWidget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10869181/