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. Combo box (or equivalent) - "one click" behavior
Forum Updated to NodeBB v4.3 + New Features

Combo box (or equivalent) - "one click" behavior

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 393 Views 2 Watching
  • 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.
  • gbettegaG Offline
    gbettegaG Offline
    gbettega
    wrote last edited by
    #1

    Hello Forum,
    I use QComboxBox into a QTreeView as item delegate.
    As you know the behavior is the following (see picture)

    comboBox.PNG

    One single click raises the editor, two clicks activate the drop down menu, and the user can select.
    Now: is would be possible to activate the dropdown after a single click, not two?
    Grazie mille
    Giovanni

    S 1 Reply Last reply
    0
    • gbettegaG gbettega

      Hello Forum,
      I use QComboxBox into a QTreeView as item delegate.
      As you know the behavior is the following (see picture)

      comboBox.PNG

      One single click raises the editor, two clicks activate the drop down menu, and the user can select.
      Now: is would be possible to activate the dropdown after a single click, not two?
      Grazie mille
      Giovanni

      S Offline
      S Offline
      SimonSchroeder
      wrote last edited by
      #2

      @gbettega Out of curiosity: Does it work when you click on the arrow on the right?

      My guess is that the first click gives the focus to the cell of the tree view. Accordingly, it should be possible to connect the corresponding signal (activated/clicked/pressed?) with an appropriate slot of QComboBox. I couldn't find a good slot right away. You might have to create a mouse event and send that to the QComboBox instead.

      I've seen also that QTreeView has an entered signal (you need to enable mouse tracking for the QTreeView first) which could be connected to the setFocus() slot of the QComboBox. Then, when you click, the combo box should already have the focus.

      gbettegaG 2 Replies Last reply
      2
      • S SimonSchroeder

        @gbettega Out of curiosity: Does it work when you click on the arrow on the right?

        My guess is that the first click gives the focus to the cell of the tree view. Accordingly, it should be possible to connect the corresponding signal (activated/clicked/pressed?) with an appropriate slot of QComboBox. I couldn't find a good slot right away. You might have to create a mouse event and send that to the QComboBox instead.

        I've seen also that QTreeView has an entered signal (you need to enable mouse tracking for the QTreeView first) which could be connected to the setFocus() slot of the QComboBox. Then, when you click, the combo box should already have the focus.

        gbettegaG Offline
        gbettegaG Offline
        gbettega
        wrote last edited by gbettega
        #3

        @SimonSchroeder Thank you for your time,
        the arrow on the right appears when one mouse press has occurred. It is not shown when the editor stands in the "grouund state"
        (let me use ground state with the meaning of Hydrogen atom ground state).
        I would like to be more precise: see picture, showing what happens

        comboBox1.PNG

        Looking at the cod, it seems that the trick is here

        void QComboBoxPrivate::showPopupFromMouseEvent(QMouseEvent *e)
        {
            Q_Q(QComboBox);
            QStyleOptionComboBox opt;
            q->initStyleOption(&opt);
            QStyle::SubControl sc = q->style()->hitTestComplexControl(QStyle::CC_ComboBox, &opt, e->pos(), q);
        
        [...]
        

        however it would be reasonable having something more simple to handle, I mean high level, avoiding dragging into the Qt code for such a simple task

        Grazie mille
        Have a nice day
        G

        1 Reply Last reply
        0
        • S SimonSchroeder

          @gbettega Out of curiosity: Does it work when you click on the arrow on the right?

          My guess is that the first click gives the focus to the cell of the tree view. Accordingly, it should be possible to connect the corresponding signal (activated/clicked/pressed?) with an appropriate slot of QComboBox. I couldn't find a good slot right away. You might have to create a mouse event and send that to the QComboBox instead.

          I've seen also that QTreeView has an entered signal (you need to enable mouse tracking for the QTreeView first) which could be connected to the setFocus() slot of the QComboBox. Then, when you click, the combo box should already have the focus.

          gbettegaG Offline
          gbettegaG Offline
          gbettega
          wrote last edited by
          #4

          @SimonSchroeder Forgot saying I Will try your solution
          Grazie
          G

          1 Reply Last reply
          0
          • aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote last edited by
            #5

            @gbettega QComboBox has a function virtual void mousePressEvent(QMouseEvent *e) override that you can override in subclasses and change the behavior ;)

            Qt has to stay free or it will die.

            gbettegaG 1 Reply Last reply
            0
            • aha_1980A aha_1980

              @gbettega QComboBox has a function virtual void mousePressEvent(QMouseEvent *e) override that you can override in subclasses and change the behavior ;)

              gbettegaG Offline
              gbettegaG Offline
              gbettega
              wrote last edited by gbettega
              #6

              @aha_1980 thank you. Actually this is the first attempt I tried: perhaps I did something wrong, however if I override mousePressEvent with one containing QComboBox::showPopup()
              nothing happens, also because showPopup should be reimplemented. I fear that for obtaining the behavior I described something more involved should be done, unfortunately.
              Grazie mille
              Buona giornata

              1 Reply Last reply
              0
              • jeremy_kJ Offline
                jeremy_kJ Offline
                jeremy_k
                wrote last edited by
                #7

                This sounds like setEditTriggers() behavior, using either CurrentChanged, or SelectedClicked.

                #include <QApplication>
                #include <QTableWidget>
                #include <QStyledItemDelegate>
                #include <QComboBox>
                
                struct Delegate : public QStyledItemDelegate {
                    QWidget * createEditor(QWidget *parent,  const QStyleOptionViewItem &option, const QModelIndex &index) const override
                    {
                        QComboBox *box = new QComboBox(parent);
                        box->addItems({"a", "b"});
                        box->showPopup();
                        return box;
                    }
                };
                
                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                    QTableWidget w;
                    w.setColumnCount(1);
                    w.model()->insertRows(0, 2);
                    w.setItemDelegate(new Delegate);
                    w.show();
                    w.setEditTriggers(QAbstractItemView::EditTrigger::CurrentChanged);
                    return a.exec();
                }
                

                Asking a question about code? http://eel.is/iso-c++/testcase/

                gbettegaG 1 Reply Last reply
                2
                • jeremy_kJ jeremy_k

                  This sounds like setEditTriggers() behavior, using either CurrentChanged, or SelectedClicked.

                  #include <QApplication>
                  #include <QTableWidget>
                  #include <QStyledItemDelegate>
                  #include <QComboBox>
                  
                  struct Delegate : public QStyledItemDelegate {
                      QWidget * createEditor(QWidget *parent,  const QStyleOptionViewItem &option, const QModelIndex &index) const override
                      {
                          QComboBox *box = new QComboBox(parent);
                          box->addItems({"a", "b"});
                          box->showPopup();
                          return box;
                      }
                  };
                  
                  int main(int argc, char *argv[])
                  {
                      QApplication a(argc, argv);
                      QTableWidget w;
                      w.setColumnCount(1);
                      w.model()->insertRows(0, 2);
                      w.setItemDelegate(new Delegate);
                      w.show();
                      w.setEditTriggers(QAbstractItemView::EditTrigger::CurrentChanged);
                      return a.exec();
                  }
                  
                  gbettegaG Offline
                  gbettegaG Offline
                  gbettega
                  wrote last edited by
                  #8

                  @jeremy_k thank for your time and your reply. I registered Qt forum more that 10 years ago, but occasionally used it. I have to say that the Qt community, apart from being very large as we know, is incredibly responsive and communicative. Also inclusive", most of all towards me, being a very rough Qt user.
                  I read aboun this "small" issue in serveral posts: one of them suggests correctly to use a QTimer for obtaining a "right" position on the screen of the combobox dropdown menu, and several other considerations. See for example

                  https://forum.qt.io/topic/51476/qcombobox-delegate-best-way-to-show-the-popup-menu-immediately/8

                  One of the authors also explains how the drop down is immediately hidden after a mouse release event (as in your case), but at the end no conclusive solution exposed to the word.
                  At the end I think this issue will remain an issue.
                  Not a problem: the user will do several "clicks" for using combobox, and only will perceive a bit of GUI slowness.
                  Have a nice day.
                  Giovanni

                  jeremy_kJ 1 Reply Last reply
                  0
                  • gbettegaG gbettega

                    @jeremy_k thank for your time and your reply. I registered Qt forum more that 10 years ago, but occasionally used it. I have to say that the Qt community, apart from being very large as we know, is incredibly responsive and communicative. Also inclusive", most of all towards me, being a very rough Qt user.
                    I read aboun this "small" issue in serveral posts: one of them suggests correctly to use a QTimer for obtaining a "right" position on the screen of the combobox dropdown menu, and several other considerations. See for example

                    https://forum.qt.io/topic/51476/qcombobox-delegate-best-way-to-show-the-popup-menu-immediately/8

                    One of the authors also explains how the drop down is immediately hidden after a mouse release event (as in your case), but at the end no conclusive solution exposed to the word.
                    At the end I think this issue will remain an issue.
                    Not a problem: the user will do several "clicks" for using combobox, and only will perceive a bit of GUI slowness.
                    Have a nice day.
                    Giovanni

                    jeremy_kJ Offline
                    jeremy_kJ Offline
                    jeremy_k
                    wrote last edited by
                    #9

                    @gbettega said in Combo box (or equivalent) - "one click" behavior:

                    One of the authors also explains how the drop down is immediately hidden after a mouse release event (as in your case), but at the end no conclusive solution exposed to the word.

                    This went off in a tangent that I didn't expect. The intent was

                    1. move from abstract discussion into a brief code example that cab be tested, and discussed in more concrete terms
                    2. suggest looking at the implemented item view functionality before overriding lower level mechanisms

                    Yes, the CurrentChanged edit trigger can have unexpected behavior. SelectedClicked is probably easier to work with. That takes one to two clicks (one to select a cell, one to edit the selection) to display the combo box popup, and doesn't appear to have the unintended popup close behavior. Sometimes it fails to create the editor, possibly when interpreting the second click on the newly selected cell as a double click.

                    Asking a question about code? http://eel.is/iso-c++/testcase/

                    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