在QItemEditorCreatorBase中使用后找回QWidget

Get back QWidget after using in QItemEditorCreatorBase

本文关键字:QWidget QItemEditorCreatorBase      更新时间:2023-10-16

我有一个数字编辑器可以扩展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 中正常工作。

我的愿望是访问表在编辑单元格时使用的 QSpinBox 实例QVariant::Int以便我可以设置最小值和最大值。

我该怎么做?

您可以使用 QTableWidget::setItemDelegateForColumn 在列上安装委托,当打开编辑器时,将调用其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;
    }
};