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 becomesQSpinBox
instead ofQLineEdit
.Question
Why does it create
QSpinBox
in the latter case?
How to useQLineEdit
as 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?
-
@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
QSpinBox
normally looks like for you? Looks like the1
on the right-hand side between the arrows is the current value where you change it, I don't know. -
@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
QItemEditorFactory
and the standard editing widget forint
is aQSpinBox
according 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