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. Automatically accept choice of QComboBox in a QStyledItemDelegate
Forum Update on Monday, May 27th 2025

Automatically accept choice of QComboBox in a QStyledItemDelegate

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 904 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.
  • gde23G Offline
    gde23G Offline
    gde23
    wrote on last edited by
    #1

    Hello,
    I have a QComboBox as a QStyledItemDelegate in a ModelView
    Now I click a cell -> the combo box pops up -> I click my choice -> the combo box collapses.
    However afterwards I have to click somewhere else in the view (or press enter) to apply the change.
    How can I make it apply the change immediately?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      Inside setEditorData, as last line, add:
      connect(combobox,QOverload<int>::of(&QComboBox::currentIndexChanged),this,std::bind(&QStyledItemDelegate::commitData,this,combobox));

      "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

      1 Reply Last reply
      2
      • gde23G Offline
        gde23G Offline
        gde23
        wrote on last edited by
        #3

        Thanks for the hint, however this causes an error

        /homqobject.h:314: error: static assertion failed: Signal and slot arguments are not compatible.
        Q_STATIC_ASSERT_X((FunctorArgumentCount >= 0),
        ^

        Does the bind work in combination with signal / slot connection?

        VRoninV 1 Reply Last reply
        0
        • gde23G gde23

          Thanks for the hint, however this causes an error

          /homqobject.h:314: error: static assertion failed: Signal and slot arguments are not compatible.
          Q_STATIC_ASSERT_X((FunctorArgumentCount >= 0),
          ^

          Does the bind work in combination with signal / slot connection?

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

          @gde23 said in Automatically accept choice of QComboBox in a QStyledItemDelegate:

          Does the bind work in combination with signal / slot connection?

          Yes, it's used in Qt itself. Can you show us your connection including the type off all the arguments please?

          "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

          1 Reply Last reply
          1
          • gde23G Offline
            gde23G Offline
            gde23
            wrote on last edited by
            #5
            void MyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
            {
                QComboBox *comboBox = static_cast<QComboBox*>(editor);
                comboBox->setCurrentText(value);
                comboBox->showPopup();
                connect(comboBox,QOverload<int>::of(&QComboBox::currentIndexChanged),
                                     this, std::bind(&QStyledItemDelegate::commitData, this, comboBox));                
                 return;     
            }
            

            this is of type MyDelegate : public QStyledItemDelegate

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              it's a sneaky constness problem. easy but dirty solution: connect(comboBox,QOverload<int>::of(&QComboBox::currentIndexChanged),this,std::bind(&QStyledItemDelegate::commitData, const_cast<MyDelegate*>(this), comboBox));

              Better designed solution:

              • in MyDelegate add Q_SIGNAL void comboChanged(QWidget*) const.
              • in MyDelegate's constructor add connect(this,&MyDelegate::comboChanged,this,&MyDelegate::commitData);
              • as last line of setEditorData put connect(comboBox,QOverload<int>::of(&QComboBox::currentIndexChanged),this, std::bind(&MyDelegate::comboChanged, this, comboBox));

              "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

              1 Reply Last reply
              3
              • gde23G Offline
                gde23G Offline
                gde23
                wrote on last edited by
                #7

                Thanks a lot. Your solutions work perfectly (both of them).

                I've further changed the combeChanegd() signal to a slot that further emits a closeEditor() signal.

                void MyDelegate::comboChanged(QWidget *editor)
                {
                    emit commitData(editor);
                    emit closeEditor(editor, QAbstractItemDelegate::NoHint);
                }
                

                That way the delegate is directly closed after a selection was made.

                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