How to get mouse double click event from only the title of QGroupBox?
-
Hi
I had a QGroupBox and I wanted to do some action when I double click on it, However I want to restrict the area of interaction to only the title of QGroupBox not his body nor the widget inside it. Is there an easy way to achive that?@abdoemr11 Demo:
#include <QApplication> #include <QGroupBox> #include <QRadioButton> #include <QVBoxLayout> #include <QStyle> #include <QMouseEvent> #include <QStyleOptionGroupBox> #include <QDebug> class GroupBox: public QGroupBox{ public: using QGroupBox::QGroupBox; protected: void mouseDoubleClickEvent(QMouseEvent *event){ QGroupBox::mouseDoubleClickEvent(event); QStyleOptionGroupBox box; initStyleOption(&box); QStyle::SubControl sc = style()->hitTestComplexControl(QStyle::CC_GroupBox, &box, event->pos(), this); if(sc & QStyle::SC_GroupBoxLabel){ qDebug() << "clicked" << title(); } } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); GroupBox groupBox("Exclusive Radio Buttons"); QRadioButton *radio1 = new QRadioButton("Radio button 1"); QRadioButton *radio2 = new QRadioButton("Radio button 2"); QRadioButton *radio3 = new QRadioButton("Radio button 3"); radio1->setChecked(true); QVBoxLayout *vbox = new QVBoxLayout; vbox->addWidget(radio1); vbox->addWidget(radio2); vbox->addWidget(radio3); vbox->addStretch(1); groupBox.setLayout(vbox); groupBox.show(); return a.exec(); }
-
@abdoemr11 Demo:
#include <QApplication> #include <QGroupBox> #include <QRadioButton> #include <QVBoxLayout> #include <QStyle> #include <QMouseEvent> #include <QStyleOptionGroupBox> #include <QDebug> class GroupBox: public QGroupBox{ public: using QGroupBox::QGroupBox; protected: void mouseDoubleClickEvent(QMouseEvent *event){ QGroupBox::mouseDoubleClickEvent(event); QStyleOptionGroupBox box; initStyleOption(&box); QStyle::SubControl sc = style()->hitTestComplexControl(QStyle::CC_GroupBox, &box, event->pos(), this); if(sc & QStyle::SC_GroupBoxLabel){ qDebug() << "clicked" << title(); } } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); GroupBox groupBox("Exclusive Radio Buttons"); QRadioButton *radio1 = new QRadioButton("Radio button 1"); QRadioButton *radio2 = new QRadioButton("Radio button 2"); QRadioButton *radio3 = new QRadioButton("Radio button 3"); radio1->setChecked(true); QVBoxLayout *vbox = new QVBoxLayout; vbox->addWidget(radio1); vbox->addWidget(radio2); vbox->addWidget(radio3); vbox->addStretch(1); groupBox.setLayout(vbox); groupBox.show(); return a.exec(); }
@eyllanesc said in How to get mouse double click event from only the title of QGroupBox?:
QStyle::SubControl sc = style()->hitTestComplexControl(QStyle::CC_GroupBox, &box, event->pos(), this);
Interesting, I had not heard of QStyle::hitTestComplexControl().
However the docs there state "Note that the position is expressed in screen coordinates." But QMouseEvent::pos() says "Returns the position of the mouse cursor, relative to the widget that received the event." Is there not an issue here using
event->pos()
? -
@eyllanesc said in How to get mouse double click event from only the title of QGroupBox?:
QStyle::SubControl sc = style()->hitTestComplexControl(QStyle::CC_GroupBox, &box, event->pos(), this);
Interesting, I had not heard of QStyle::hitTestComplexControl().
However the docs there state "Note that the position is expressed in screen coordinates." But QMouseEvent::pos() says "Returns the position of the mouse cursor, relative to the widget that received the event." Is there not an issue here using
event->pos()
? -
@JonB Maybe it is a bug in the docs, I have used this method several times in other applications. And if you check the mousePressEvent method of QGroupBox you will see that it uses it in the same way that I did
@eyllanesc said in How to get mouse double click event from only the title of QGroupBox?:
I have used this method several times in other applications.
Then that's fine. I had wondered whether
event->globalPos()
might have been necessary. Maybe I misunderstand the docs anyway, I take "screen coordinates" to mean "relative to screen" but maybe that's not the same thing as "(0,0) is top-left of screen".It's still a nice method to avoid having to figure out what is where on a widget oneself!
-
@eyllanesc Thanks man your solution is elegant. Thx again