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. QRadioButton signals don't work
Forum Updated to NodeBB v4.3 + New Features

QRadioButton signals don't work

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

    Hi, I'm trying to catch a signal from QRadioButtons, but I can't seem to get it working.

    Here's what I have:
    mainwindow.h:

    ...
    private slots:
        void rAuto_clicked();
    ...
    public:
        QRadioButton *rAuto;
    

    mainwindow.cpp:

    void MainWindow::rAuto_clicked()
    {
        qDebug() << "rAuto"; //doesn't print anything
    }
    ...
        ui->setupUi(this);
    
        rAuto = new QRadioButton;
        QObject::connect(rAuto,SIGNAL(clicked()),SLOT(rAuto_clicked()));
    ...
    

    I mainly usedQObject::connect()for sending signals from threads, so I might be doing this wrong. I also looked on Stackoverflow, but the answer there didn't help me at all.

    I also tried using thetoggled(const bool&)signal (and modified the slot), but that also didn't work.

    JonBJ 1 Reply Last reply
    0
    • S Sucharek

      Hi, I'm trying to catch a signal from QRadioButtons, but I can't seem to get it working.

      Here's what I have:
      mainwindow.h:

      ...
      private slots:
          void rAuto_clicked();
      ...
      public:
          QRadioButton *rAuto;
      

      mainwindow.cpp:

      void MainWindow::rAuto_clicked()
      {
          qDebug() << "rAuto"; //doesn't print anything
      }
      ...
          ui->setupUi(this);
      
          rAuto = new QRadioButton;
          QObject::connect(rAuto,SIGNAL(clicked()),SLOT(rAuto_clicked()));
      ...
      

      I mainly usedQObject::connect()for sending signals from threads, so I might be doing this wrong. I also looked on Stackoverflow, but the answer there didn't help me at all.

      I also tried using thetoggled(const bool&)signal (and modified the slot), but that also didn't work.

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

      @Sucharek said in QRadioButton signals don't work:

      QObject::connect(rAuto,SIGNAL(clicked()),SLOT(rAuto_clicked()));

      QObject::connect(rAuto,SIGNAL(clicked()),this,SLOT(rAuto_clicked()));

      Better would be:

      QObject::connect(rAuto, &QRadioButton::clicked, this, &MainWindow::rAuto_clicked);
      

      And of course make sure you actually put that rAuto = new QRadioButton; radiobutton on a widget somewhere!

      S 1 Reply Last reply
      0
      • JonBJ JonB

        @Sucharek said in QRadioButton signals don't work:

        QObject::connect(rAuto,SIGNAL(clicked()),SLOT(rAuto_clicked()));

        QObject::connect(rAuto,SIGNAL(clicked()),this,SLOT(rAuto_clicked()));

        Better would be:

        QObject::connect(rAuto, &QRadioButton::clicked, this, &MainWindow::rAuto_clicked);
        

        And of course make sure you actually put that rAuto = new QRadioButton; radiobutton on a widget somewhere!

        S Offline
        S Offline
        Sucharek
        wrote on last edited by
        #3

        Hi @JonB, thanks for your reply, but this doesn't work either.

        I did add the button to MainWindow. It's in a different void.

        JonBJ 1 Reply Last reply
        0
        • S Sucharek

          Hi @JonB, thanks for your reply, but this doesn't work either.

          I did add the button to MainWindow. It's in a different void.

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

          @Sucharek
          The above code should be correct, so I don't know where your problem is or what else you do/do not do.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            Nothing in your code hints that you are showing that button.

            Seeing that you have a call to setupUi I would venture that you are clicking on a button you added through Designer and connecting a button you created in code but not shown nor added to said Designer based part.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            S 1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              Nothing in your code hints that you are showing that button.

              Seeing that you have a call to setupUi I would venture that you are clicking on a button you added through Designer and connecting a button you created in code but not shown nor added to said Designer based part.

              S Offline
              S Offline
              Sucharek
              wrote on last edited by
              #6

              Hi @SGaist, I have a different void for adding that button.
              It's like a setup wizard, so I have to change what's in the MainWindow. I have a gridLayout set up in designer, where I add what I need to add or remove.

              I'm adding the button this way:

              ...
              } else if (scene == 4) {
                  ...
                  ui->gridLayout_Content->addWidget(rAuto, 2, 0);
                  ...
              }
              ...
              
              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Then are you sure that it is really that button that is shown and connected and you did not replace the value in that variable later on ?

                That said, since you mention a wizard, did you consider using QWizard ?

                Also, since it is supposed to be a wizard showing different "scenes", rather than trying to make everything in one single widget, you should rather have one widget per "scene" and use something like QStackedWidget to switch between them.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                S 1 Reply Last reply
                0
                • SGaistS SGaist

                  Then are you sure that it is really that button that is shown and connected and you did not replace the value in that variable later on ?

                  That said, since you mention a wizard, did you consider using QWizard ?

                  Also, since it is supposed to be a wizard showing different "scenes", rather than trying to make everything in one single widget, you should rather have one widget per "scene" and use something like QStackedWidget to switch between them.

                  S Offline
                  S Offline
                  Sucharek
                  wrote on last edited by
                  #8

                  @SGaist I'm sure it's the same button.
                  I tested it too. I made a button in the editor and when I clicked it, I got if the radio button is toggled. If it was, it printed true, if it wasn't, false. So I'm sure it is that button.

                  Thanks for the suggestions. I probably won't use QWizard, because I already have a lot of code written, but I might use QStackedWidget.

                  1 Reply Last reply
                  0
                  • JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by
                    #9

                    Can you try signal pressed()? You can add different text or background color to this radio button to make sure if it is the right one or not.

                    S 1 Reply Last reply
                    0
                    • JoeCFDJ JoeCFD

                      Can you try signal pressed()? You can add different text or background color to this radio button to make sure if it is the right one or not.

                      S Offline
                      S Offline
                      Sucharek
                      wrote on last edited by
                      #10

                      Hi @JoeCFD, no, that doesn't work too.

                      I know it's the right button. In fact, I have two of them and both are connected to 2 different slots.

                      1 Reply Last reply
                      0
                      • JoeCFDJ Offline
                        JoeCFDJ Offline
                        JoeCFD
                        wrote on last edited by JoeCFD
                        #11

                        @Sucharek said in QRadioButton signals don't work:

                        qDebug()

                        does qDebug() work at other places? Set a break point at

                        qDebug() << "rAuto"; //doesn't print anything
                        
                        S 1 Reply Last reply
                        0
                        • JoeCFDJ JoeCFD

                          @Sucharek said in QRadioButton signals don't work:

                          qDebug()

                          does qDebug() work at other places? Set a break point at

                          qDebug() << "rAuto"; //doesn't print anything
                          
                          S Offline
                          S Offline
                          Sucharek
                          wrote on last edited by
                          #12

                          @JoeCFD yes, qDebug() does work. I tried it on my temporary QPushButton.
                          I think I don't know how to use breakpoints. I set multiple poins, started the app in debug mode, but none of them done anything. Even the ones I know got executed.

                          JoeCFDJ 1 Reply Last reply
                          0
                          • S Sucharek

                            @JoeCFD yes, qDebug() does work. I tried it on my temporary QPushButton.
                            I think I don't know how to use breakpoints. I set multiple poins, started the app in debug mode, but none of them done anything. Even the ones I know got executed.

                            JoeCFDJ Offline
                            JoeCFDJ Offline
                            JoeCFD
                            wrote on last edited by
                            #13

                            @Sucharek replace qDebug() << "rAuto"; with

                            std::cout <<   "========= rAuto"  << std::endl;
                            

                            you need header
                            #include <iostream>

                            It can not be true that the signal of QRadioButton does not work. So many people are using it.

                            S 1 Reply Last reply
                            0
                            • JoeCFDJ JoeCFD

                              @Sucharek replace qDebug() << "rAuto"; with

                              std::cout <<   "========= rAuto"  << std::endl;
                              

                              you need header
                              #include <iostream>

                              It can not be true that the signal of QRadioButton does not work. So many people are using it.

                              S Offline
                              S Offline
                              Sucharek
                              wrote on last edited by
                              #14

                              @JoeCFD, nope, doesn't print anything.
                              I also tried commands likeclose()and they didn't work either.

                              Well, just realised it was my fault all along.
                              Apparently I put therAuto = new QRadioButtonin a wrong place. I put it in a void that sets the buttons text, font size, etc. and I guess I shouldn't do that.

                              Anyways, thanks for the help everyone.

                              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