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. Button inherit stylesheet for no reason

Button inherit stylesheet for no reason

Scheduled Pinned Locked Moved Solved General and Desktop
qpushbuttmouseeventstylesheet
17 Posts 6 Posters 3.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.
  • L Offline
    L Offline
    legitnameyo
    wrote on last edited by
    #7

    How do I implement this code way? I do my UI in code since that's what I prefer. This is what I've got so far and it isn't working...

    QButtonGroup *g_btns = new QButtonGroup(this);
    /* all buttons are created and I add the buttons in about this way*/
    btn1->setCheckable(true);
    //etc..
    g_btns->addButton(btn1);
    g_btns->addButton(btn2);
    g_btns->addButton(btn3);
    // etc ...
    
          connect(g_btns, SIGNAL(buttonClicked(int)), this, SLOT(onGroupButtonClicked(int)));
    
    void MainWindow::onGroupButtonClicked(int btn_id) {
    qDebug() << "clicked!";
    }
    

    And this gives me nothing. The onGroupButtonClicked does not activate.

    J.HilkJ jsulmJ 2 Replies Last reply
    0
    • L legitnameyo

      How do I implement this code way? I do my UI in code since that's what I prefer. This is what I've got so far and it isn't working...

      QButtonGroup *g_btns = new QButtonGroup(this);
      /* all buttons are created and I add the buttons in about this way*/
      btn1->setCheckable(true);
      //etc..
      g_btns->addButton(btn1);
      g_btns->addButton(btn2);
      g_btns->addButton(btn3);
      // etc ...
      
            connect(g_btns, SIGNAL(buttonClicked(int)), this, SLOT(onGroupButtonClicked(int)));
      
      void MainWindow::onGroupButtonClicked(int btn_id) {
      qDebug() << "clicked!";
      }
      

      And this gives me nothing. The onGroupButtonClicked does not activate.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #8

      @legitnameyo I never used QButtonGroup, but a quick look into the documentation tells me, you never gave your added buttons an ID

      void QButtonGroup::addButton(QAbstractButton *button, int id = -1)

      I'm guessing here and say, that buttons with the ID of -1 do not emit the buttonClicked(int) signal


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      mrjjM 1 Reply Last reply
      3
      • L legitnameyo

        How do I implement this code way? I do my UI in code since that's what I prefer. This is what I've got so far and it isn't working...

        QButtonGroup *g_btns = new QButtonGroup(this);
        /* all buttons are created and I add the buttons in about this way*/
        btn1->setCheckable(true);
        //etc..
        g_btns->addButton(btn1);
        g_btns->addButton(btn2);
        g_btns->addButton(btn3);
        // etc ...
        
              connect(g_btns, SIGNAL(buttonClicked(int)), this, SLOT(onGroupButtonClicked(int)));
        
        void MainWindow::onGroupButtonClicked(int btn_id) {
        qDebug() << "clicked!";
        }
        

        And this gives me nothing. The onGroupButtonClicked does not activate.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #9

        @legitnameyo It should work. Do you get any warnings at runtime (related to connect)?
        Is g_btns in connect() really the one you're creating at the beginning of the code snippet you provided?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        3
        • J.HilkJ J.Hilk

          @legitnameyo I never used QButtonGroup, but a quick look into the documentation tells me, you never gave your added buttons an ID

          void QButtonGroup::addButton(QAbstractButton *button, int id = -1)

          I'm guessing here and say, that buttons with the ID of -1 do not emit the buttonClicked(int) signal

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #10

          @J.Hilk
          Was a good guess,
          but it seems, it will use negative values for auto numbering and does, in fact, emit them.

            QButtonGroup *buttonGroup = new QButtonGroup(this);
          
              buttonGroup->addButton( ui->pushButton_2);
              buttonGroup->addButton( ui->pushButton_3);
              buttonGroup->addButton( ui->pushButton_4);
              buttonGroup->addButton( ui->pushButton_5);
          
          
              connect(buttonGroup, static_cast<void(QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked),
              [ = ](int id) {
                  qDebug() << id;
              });
          
          

          outputs.
          -2
          -2
          -3
          -5
          -4

          J.HilkJ 1 Reply Last reply
          1
          • mrjjM mrjj

            @J.Hilk
            Was a good guess,
            but it seems, it will use negative values for auto numbering and does, in fact, emit them.

              QButtonGroup *buttonGroup = new QButtonGroup(this);
            
                buttonGroup->addButton( ui->pushButton_2);
                buttonGroup->addButton( ui->pushButton_3);
                buttonGroup->addButton( ui->pushButton_4);
                buttonGroup->addButton( ui->pushButton_5);
            
            
                connect(buttonGroup, static_cast<void(QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked),
                [ = ](int id) {
                    qDebug() << id;
                });
            
            

            outputs.
            -2
            -2
            -3
            -5
            -4

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #11

            @mrjj
            I find this behavior actually not very intuitive.

            That the id increases when non is specified, maybe...
            But a higher negative number !?


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            mrjjM 1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @mrjj
              I find this behavior actually not very intuitive.

              That the id increases when non is specified, maybe...
              But a higher negative number !?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #12

              @J.Hilk
              Yep. I had to test it as sounded odd it would auto number with negative values.
              But it seems design wants to allow to have some auto numbered and some manually
              even i don't see why.

              JonBJ 1 Reply Last reply
              1
              • mrjjM mrjj

                @J.Hilk
                Yep. I had to test it as sounded odd it would auto number with negative values.
                But it seems design wants to allow to have some auto numbered and some manually
                even i don't see why.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #13

                @mrjj
                Negative-decrementing auto-numbers are usually used for "anonymous" items while allowing positive numbers for explicit use. Then the numbers won't clash.

                1 Reply Last reply
                3
                • L Offline
                  L Offline
                  legitnameyo
                  wrote on last edited by legitnameyo
                  #14

                  There is NO error such as this one below

                  QMetaObject::connectSlotsByName: No matching signal for onGroupButtonClicked(int)
                  

                  And yes g_btns is defined only ONCE. Everything is in the order I got it in my code. However one thing that might change things up is that I use a custom class for my buttons called QClickAgnostic, which inherits from QPushButton as such above

                  and the buttons are created like this

                          QClickAgnostic *btn = new QClickAgnostic(ui->frame_of_btns);
                  
                  KillerSmathK 1 Reply Last reply
                  0
                  • L legitnameyo

                    There is NO error such as this one below

                    QMetaObject::connectSlotsByName: No matching signal for onGroupButtonClicked(int)
                    

                    And yes g_btns is defined only ONCE. Everything is in the order I got it in my code. However one thing that might change things up is that I use a custom class for my buttons called QClickAgnostic, which inherits from QPushButton as such above

                    and the buttons are created like this

                            QClickAgnostic *btn = new QClickAgnostic(ui->frame_of_btns);
                    
                    KillerSmathK Offline
                    KillerSmathK Offline
                    KillerSmath
                    wrote on last edited by
                    #15

                    @legitnameyo
                    You need to notify the ButtonGroup about the click event.

                    I took a look at the source code of the abstractbutton and noticed the button directly notifies the group that it was clicked. But you broke this step when you reimplemented the MouseReleaseEvent function.

                    See a snippet of this step.

                    void QAbstractButton::mouseReleaseEvent(QMouseEvent *e)
                    {
                    ...
                        if (hitButton(e->pos())) {
                            d->repeatTimer.stop();
                            d->click(); // call click function 
                            e->accept();
                        } else {
                            setDown(false);
                            e->ignore();
                        }
                    }
                    void QAbstractButtonPrivate::click()
                    {
                        ...
                        if (guard)
                            emitReleased();
                        if (guard)
                            emitClicked(); // call emitClicked
                    }
                    
                    void QAbstractButtonPrivate::emitClicked()
                    {
                      ...
                    #if QT_CONFIG(buttongroup)
                        if (guard && group) {
                            emit group->buttonClicked(group->id(q)); // emit the signal
                            if (guard && group)
                                emit group->buttonClicked(q); // emit the signal
                        }
                    #endif
                    }
                    

                    So, you can solve this problem calling the click() function inside MouseReleaseEvent.

                    void QClickAgnostic::mouseReleaseEvent(QMouseEvent *e) {
                        if(e->button() == Qt::LeftButton) {
                            click(); // called
                            emit leftClicked();
                        } else if(e->button() == Qt::RightButton) {
                            click(); // called
                            emit rightClicked();
                        }
                    }
                    

                    @Computer Science Student - Brazil
                    Web Developer and Researcher
                    “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                    mrjjM 1 Reply Last reply
                    5
                    • KillerSmathK KillerSmath

                      @legitnameyo
                      You need to notify the ButtonGroup about the click event.

                      I took a look at the source code of the abstractbutton and noticed the button directly notifies the group that it was clicked. But you broke this step when you reimplemented the MouseReleaseEvent function.

                      See a snippet of this step.

                      void QAbstractButton::mouseReleaseEvent(QMouseEvent *e)
                      {
                      ...
                          if (hitButton(e->pos())) {
                              d->repeatTimer.stop();
                              d->click(); // call click function 
                              e->accept();
                          } else {
                              setDown(false);
                              e->ignore();
                          }
                      }
                      void QAbstractButtonPrivate::click()
                      {
                          ...
                          if (guard)
                              emitReleased();
                          if (guard)
                              emitClicked(); // call emitClicked
                      }
                      
                      void QAbstractButtonPrivate::emitClicked()
                      {
                        ...
                      #if QT_CONFIG(buttongroup)
                          if (guard && group) {
                              emit group->buttonClicked(group->id(q)); // emit the signal
                              if (guard && group)
                                  emit group->buttonClicked(q); // emit the signal
                          }
                      #endif
                      }
                      

                      So, you can solve this problem calling the click() function inside MouseReleaseEvent.

                      void QClickAgnostic::mouseReleaseEvent(QMouseEvent *e) {
                          if(e->button() == Qt::LeftButton) {
                              click(); // called
                              emit leftClicked();
                          } else if(e->button() == Qt::RightButton) {
                              click(); // called
                              emit rightClicked();
                          }
                      }
                      
                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #16

                      @KillerSmath
                      Good digging !

                      1 Reply Last reply
                      1
                      • L Offline
                        L Offline
                        legitnameyo
                        wrote on last edited by
                        #17

                        @KillerSmath fix did the work, combined with @mrjj QGroupButton! I added this code to style the pressed button

                        void MainWindow::onGroupButtonClicked(int btn_id) {
                            g_btns->button(btn_id)->setStyleSheet(QString("QPushButton {color: #444444; background: #cccccc; border: 2px solid #cccccc; border-width: 0px 0px 2px 0px;} QPushButton:hover {border: 2px black solid; color: #333333; background-color: #F7F7F7;} QPushButton:pressed {background: #cccccc;}"));
                        }
                        
                        

                        Thanks a lot guys!

                        1 Reply Last reply
                        1

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved