Combo box (or equivalent) - "one click" behavior
-
Hello Forum,
I use QComboxBox into a QTreeView as item delegate.
As you know the behavior is the following (see picture)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 -
Hello Forum,
I use QComboxBox into a QTreeView as item delegate.
As you know the behavior is the following (see picture)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@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 thesetFocus()
slot of the QComboBox. Then, when you click, the combo box should already have the focus. -
@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 thesetFocus()
slot of the QComboBox. Then, when you click, the combo box should already have the focus.@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 happensLooking 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 -
@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 thesetFocus()
slot of the QComboBox. Then, when you click, the combo box should already have the focus.@SimonSchroeder Forgot saying I Will try your solution
Grazie
G -
@gbettega
QComboBox
has a functionvirtual void mousePressEvent(QMouseEvent *e) override
that you can override in subclasses and change the behavior ;)@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 -
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(); }
-
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(); }
@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 examplehttps://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_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 examplehttps://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@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
- move from abstract discussion into a brief code example that cab be tested, and discussed in more concrete terms
- 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.