Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableWidgetItem will use a spinbox for editor automatically if setData is called
Forum Updated to NodeBB v4.3 + New Features

QTableWidgetItem will use a spinbox for editor automatically if setData is called

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 965 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • jronaldJ Offline
    jronaldJ Offline
    jronald
    wrote on last edited by jronald
    #1

    Env

    Linux x86-64
    Qt 6.2.0

    Problem

    For QTableWidget
    If let every cell empty initially, the editor for each item is QLineEdit.
    If use QTableWidgetItem::setData(Qt::DisplayRole, 123) to set some item values, the editor for each of these items becomes QSpinBox instead of QLineEdit.

    Question

    Why does it create QSpinBox in the latter case?
    How to use QLineEdit as editor in the latter case?

    JonBJ VRoninV 2 Replies Last reply
    0
    • jronaldJ jronald

      Env

      Linux x86-64
      Qt 6.2.0

      Problem

      For QTableWidget
      If let every cell empty initially, the editor for each item is QLineEdit.
      If use QTableWidgetItem::setData(Qt::DisplayRole, 123) to set some item values, the editor for each of these items becomes QSpinBox instead of QLineEdit.

      Question

      Why does it create QSpinBox in the latter case?
      How to use QLineEdit as editor in the latter case?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @jronald
      Because 123 is a number. Use "123" if you want to prevent this.

      1 Reply Last reply
      2
      • jronaldJ Offline
        jronaldJ Offline
        jronald
        wrote on last edited by
        #3

        @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
        QTableWidget.png

        Why "1" on the right side of the spin box?

        JonBJ 1 Reply Last reply
        0
        • jronaldJ jronald

          @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
          QTableWidget.png

          Why "1" on the right side of the spin box?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @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 via QTableWidget::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 the 1 on the right-hand side between the arrows is the current value where you change it, I don't know.

          jronaldJ 1 Reply Last reply
          2
          • JonBJ JonB

            @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 via QTableWidget::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 the 1 on the right-hand side between the arrows is the current value where you change it, I don't know.

            jronaldJ Offline
            jronaldJ Offline
            jronald
            wrote on last edited by
            #5

            @JonB

            It's not a typical problem, I will write a demo and post a new issue for it.

            Thanks

            1 Reply Last reply
            0
            • jronaldJ jronald

              Env

              Linux x86-64
              Qt 6.2.0

              Problem

              For QTableWidget
              If let every cell empty initially, the editor for each item is QLineEdit.
              If use QTableWidgetItem::setData(Qt::DisplayRole, 123) to set some item values, the editor for each of these items becomes QSpinBox instead of QLineEdit.

              Question

              Why does it create QSpinBox in the latter case?
              How to use QLineEdit as editor in the latter case?

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              @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 for int is a QSpinBox according to the docs

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

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              jronaldJ 1 Reply Last reply
              2
              • VRoninV VRonin

                @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 for int is a QSpinBox according to the docs

                How 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;});
                
                jronaldJ Offline
                jronaldJ Offline
                jronald
                wrote on last edited by
                #7

                @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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved