QTableWidgetItem will use a spinbox for editor automatically if setData is called
-
Env
Linux x86-64
Qt 6.2.0Problem
For QTableWidget
If let every cell empty initially, the editor for each item isQLineEdit.
If useQTableWidgetItem::setData(Qt::DisplayRole, 123)to set some item values, the editor for each of these items becomesQSpinBoxinstead ofQLineEdit.Question
Why does it create
QSpinBoxin the latter case?
How to useQLineEditas editor in the latter case? -
Env
Linux x86-64
Qt 6.2.0Problem
For QTableWidget
If let every cell empty initially, the editor for each item isQLineEdit.
If useQTableWidgetItem::setData(Qt::DisplayRole, 123)to set some item values, the editor for each of these items becomesQSpinBoxinstead ofQLineEdit.Question
Why does it create
QSpinBoxin the latter case?
How to useQLineEditas editor in the latter case? -
@JonB said in QTableWidgetItem will use a spinbox for editor automatically if setData is called:
Because 123 is a number. Use "123" if you want to prevent this.
Solved, thanks.
BTW, the QSpinBox looks as below

Why "1" on the right side of the spin box?
-
@JonB said in QTableWidgetItem will use a spinbox for editor automatically if setData is called:
Because 123 is a number. Use "123" if you want to prevent this.
Solved, thanks.
BTW, the QSpinBox looks as below

Why "1" on the right side of the spin box?
@jronald
BTW (before somebody comments), for the original question I picked the "simplest" way. If you want your data to actually stay as a number but change the editing behaviour only, you could do it viaQTableWidget::setItemDelegate(), as per e.g. @VRonin's https://forum.qt.io/topic/71692/using-setdata-qt-editrole-xxx-with-qtablewidgetitem/2. But more work.For your pic I don't know. I don't see it like that under Linux. Is that what a
QSpinBoxnormally looks like for you? Looks like the1on the right-hand side between the arrows is the current value where you change it, I don't know. -
@jronald
BTW (before somebody comments), for the original question I picked the "simplest" way. If you want your data to actually stay as a number but change the editing behaviour only, you could do it viaQTableWidget::setItemDelegate(), as per e.g. @VRonin's https://forum.qt.io/topic/71692/using-setdata-qt-editrole-xxx-with-qtablewidgetitem/2. But more work.For your pic I don't know. I don't see it like that under Linux. Is that what a
QSpinBoxnormally looks like for you? Looks like the1on the right-hand side between the arrows is the current value where you change it, I don't know. -
Env
Linux x86-64
Qt 6.2.0Problem
For QTableWidget
If let every cell empty initially, the editor for each item isQLineEdit.
If useQTableWidgetItem::setData(Qt::DisplayRole, 123)to set some item values, the editor for each of these items becomesQSpinBoxinstead ofQLineEdit.Question
Why does it create
QSpinBoxin the latter case?
How to useQLineEditas editor in the latter case?@jronald said in QTableWidgetItem will use a spinbox for editor automatically if setData is called:
Why does it create QSpinBox in the latter case?
Because it uses
QItemEditorFactoryand the standard editing widget forintis aQSpinBoxaccording to the docsHow to use QLineEdit as editor in the latter case?
class IntLineEditCreator : public QItemEditorCreatorBase { public: IntLineEditCreator() = default; QWidget* createWidget(QWidget *parent) const override{ return new QLineEdit(parent); } QByteArray valuePropertyName() override{ return QByteArrayLiteral("text"); } };then you can use it with something like:
IntLineEditCreator* creator = new IntLineEditCreator; QItemEditorFactory *factory = new QItemEditorFactory; factory->registerEditor(QMetaType::Int, creator); factory->registerEditor(QMetaType::UInt, creator); tableWidget->delegate()->setItemEditorFactory(factory); connect(tableWidget,&QObject::destroyed,[creator,factory](){delete creator; delete factory;}); -
@jronald said in QTableWidgetItem will use a spinbox for editor automatically if setData is called:
Why does it create QSpinBox in the latter case?
Because it uses
QItemEditorFactoryand the standard editing widget forintis aQSpinBoxaccording to the docsHow to use QLineEdit as editor in the latter case?
class IntLineEditCreator : public QItemEditorCreatorBase { public: IntLineEditCreator() = default; QWidget* createWidget(QWidget *parent) const override{ return new QLineEdit(parent); } QByteArray valuePropertyName() override{ return QByteArrayLiteral("text"); } };then you can use it with something like:
IntLineEditCreator* creator = new IntLineEditCreator; QItemEditorFactory *factory = new QItemEditorFactory; factory->registerEditor(QMetaType::Int, creator); factory->registerEditor(QMetaType::UInt, creator); tableWidget->delegate()->setItemEditorFactory(factory); connect(tableWidget,&QObject::destroyed,[creator,factory](){delete creator; delete factory;});@VRonin said in QTableWidgetItem will use a spinbox for editor automatically if setData is called:
class IntLineEditCreator : public QItemEditorCreatorBase {
public:
IntLineEditCreator() = default;
QWidget* createWidget(QWidget *parent) const override{
return new QLineEdit(parent);
}
QByteArray valuePropertyName() override{ return QByteArrayLiteral("text"); }
};then you can use it with something like:
IntLineEditCreator* creator = new IntLineEditCreator;
QItemEditorFactory *factory = new QItemEditorFactory;
factory->registerEditor(QMetaType::Int, creator);
factory->registerEditor(QMetaType::UInt, creator);
tableWidget->delegate()->setItemEditorFactory(factory);
connect(tableWidget,&QObject::destroyed,creator,factory{delete creator; delete factory;});Awesome, thanks