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. How to get mouse double click event from only the title of QGroupBox?
Forum Update on Monday, May 27th 2025

How to get mouse double click event from only the title of QGroupBox?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.1k 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.
  • A Offline
    A Offline
    abdoemr11
    wrote on last edited by
    #1

    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?

    eyllanescE 1 Reply Last reply
    0
    • JoeCFDJ Online
      JoeCFDJ Online
      JoeCFD
      wrote on last edited by JoeCFD
      #2

      Override mouseevent in subclass of qgroupbox and check the position of the mouse event. If it is inside title area, do your thing. Otherwise, nothing.

      1 Reply Last reply
      0
      • A abdoemr11

        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?

        eyllanescE Offline
        eyllanescE Offline
        eyllanesc
        wrote on last edited by
        #3

        @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();
        }
        

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        JonBJ 1 Reply Last reply
        2
        • eyllanescE eyllanesc

          @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();
          }
          
          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #4

          @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()?

          eyllanescE 1 Reply Last reply
          0
          • JonBJ JonB

            @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()?

            eyllanescE Offline
            eyllanescE Offline
            eyllanesc
            wrote on last edited by
            #5

            @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

            If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

            JonBJ 1 Reply Last reply
            3
            • eyllanescE eyllanesc

              @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

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #6

              @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!

              1 Reply Last reply
              0
              • A Offline
                A Offline
                abdoemr11
                wrote on last edited by
                #7

                @eyllanesc Thanks man your solution is elegant. Thx again

                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