Frage

I have a numeric editor that extends the QSpinBox

NumericEditor::NumericEditor(QWidget *widget): QSpinBox(widget)

I use this editor to edit the type QVariant::Int in the QTableWidget

QItemEditorCreatorBase *numericEditor = new QStandardItemEditorCreator<NumericEditor>();
factory->registerEditor(QVariant::Int, numericEditor); 

Data is entered in the table as normal. Ignore the usage of the word "color". Its based off the color editor example.

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);

The spin box appears and works fine in the QTableWidget.

My desire is to get access to the instance of QSpinBox that the table uses when it edits QVariant::Int cells so I can set the min and max values.

How can I do this?

War es hilfreich?

Lösung

You can install a delegate on the column with QTableWidget::setItemDelegateForColumn, and when an editor is opened, its createEditor method will be called.

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;
    }
};
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top