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 reselect quickly and deselect again after enabling the radiobutton

QRadioButton reselect quickly and deselect again after enabling the radiobutton

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 5 Posters 6.7k 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.
  • AlienA Alien

    Hello Everyone,

    I bind some radio button to a check box with the following scenario:
    1-when the check box is checked user can select one of radio buttons.
    2-when the check box is unchecked all of radio buttons become disable and deselect radio buttons that previously was selected by user.

    Now if user checked the checkbox in order to re enabling the radio buttons you can see that the radio button will select for a moment and deselect again(That's weird) the following code make the scenario that I've explained :

    void MainWindow::on_myCheckBox_clicked()
    {
        bool check=ui->myCheckBox->isChecked();
        if(check)
        {
            /*Check*/
            ui->radioButton->setAutoExclusive(true);
            ui->radioButton->setCheckable(true);
            ui->radioButton_2->setAutoExclusive(true);
            ui->radioButton_2->setCheckable(true);
            ui->radioButton_3->setAutoExclusive(true);
            ui->radioButton_3->setCheckable(true);
    
            ui->radioButton->setEnabled(true);
            ui->radioButton_2->setEnabled(true);
            ui->radioButton_3->setEnabled(true);
        }
        else
        {
            /*Unchek*/
            ui->radioButton->setAutoExclusive(false);
            ui->radioButton->setChecked(false);
            ui->radioButton_2->setAutoExclusive(false);
            ui->radioButton_2->setChecked(false);
            ui->radioButton_3->setAutoExclusive(false);
            ui->radioButton_3->setChecked(false);
    
            ui->radioButton->setEnabled(false);
            ui->radioButton_2->setEnabled(false);
            ui->radioButton_3->setEnabled(false);
        }
    }
    

    Why Qt does in this way and how to solve the problem?
    I appreciate any idea.

    K Offline
    K Offline
    koahnig
    wrote on last edited by
    #2

    @Alien

    Most likely you are running into a dependency issue. You better check how many times the slot routine is being called.

    Not only checking the radio button with the mouse is going to trigger the routine. Also your application activities of checking and unchecking has the same effect. In addition it looks like you have all in a checkbox, which may create another level of dependencies. It is very easy to create also some infinite loops that way.

    Vote the answer(s) that helped you to solve your issue(s)

    AlienA 1 Reply Last reply
    0
    • K koahnig

      @Alien

      Most likely you are running into a dependency issue. You better check how many times the slot routine is being called.

      Not only checking the radio button with the mouse is going to trigger the routine. Also your application activities of checking and unchecking has the same effect. In addition it looks like you have all in a checkbox, which may create another level of dependencies. It is very easy to create also some infinite loops that way.

      AlienA Offline
      AlienA Offline
      Alien
      wrote on last edited by
      #3

      @koahnig
      Thanks for your reply but I didn't get your point could you please explain it more and give me a snippet code for that.

      1 Reply Last reply
      0
      • Chris HennesC Offline
        Chris HennesC Offline
        Chris Hennes
        wrote on last edited by
        #4

        Looking at your code it seems that your sequence of calls will probably result in not just one, but sometimes even two selections, as you deselect each radio button in the group. That's to say:

                ui->radioButton->setAutoExclusive(false);
                ui->radioButton->setChecked(false);
        
        // OK, so now if radioButton was checked, the next one in the sequence gets checked.
        
                ui->radioButton_2->setAutoExclusive(false);
                ui->radioButton_2->setChecked(false);
        
        // Now, if radioButton_2 was checked, radioButton_3 gets checked
        
                ui->radioButton_3->setAutoExclusive(false);
                ui->radioButton_3->setChecked(false);
        

        Do you really need to do that first set of calls (the setAutoExclusive(false) and setChecked(false)? In most cases I've run into setEnabled(false) by itself gets me the effect I'm after.

        AlienA 1 Reply Last reply
        2
        • Chris HennesC Chris Hennes

          Looking at your code it seems that your sequence of calls will probably result in not just one, but sometimes even two selections, as you deselect each radio button in the group. That's to say:

                  ui->radioButton->setAutoExclusive(false);
                  ui->radioButton->setChecked(false);
          
          // OK, so now if radioButton was checked, the next one in the sequence gets checked.
          
                  ui->radioButton_2->setAutoExclusive(false);
                  ui->radioButton_2->setChecked(false);
          
          // Now, if radioButton_2 was checked, radioButton_3 gets checked
          
                  ui->radioButton_3->setAutoExclusive(false);
                  ui->radioButton_3->setChecked(false);
          

          Do you really need to do that first set of calls (the setAutoExclusive(false) and setChecked(false)? In most cases I've run into setEnabled(false) by itself gets me the effect I'm after.

          AlienA Offline
          AlienA Offline
          Alien
          wrote on last edited by Alien
          #5

          Dear @Chris-Hennes,
          Yes I want to clear all of the radio buttons and then make them disable.Actually it seems a QT bug I really don't know!
          My problem is reappearing of selected radio button to select state for a moment and again it disappears quickly after enabling radio buttons that is odd behavior.

          Chris HennesC 1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #6

            How about using QButtonGroup.
            You add your 3 buttons to it and add another invisible one. check the invisible button when all the others should be unchecked

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            3
            • AlienA Alien

              Dear @Chris-Hennes,
              Yes I want to clear all of the radio buttons and then make them disable.Actually it seems a QT bug I really don't know!
              My problem is reappearing of selected radio button to select state for a moment and again it disappears quickly after enabling radio buttons that is odd behavior.

              Chris HennesC Offline
              Chris HennesC Offline
              Chris Hennes
              wrote on last edited by Chris Hennes
              #7

              @Alien said in QRadioButton reselect quickly and deselect again after enabling the radiobutton:

              Actually it seems a QT bug I really don't know!

              I'd disagree - the whole point of a radio button is that one button in the group must be selected. So every time you try to deselect one, the next one gets selected. I think @VRonin 's suggestion is the best way, if you can't just hide those radios.

              1 Reply Last reply
              0
              • AlienA Offline
                AlienA Offline
                Alien
                wrote on last edited by Alien
                #8

                Dear @VRonin and @Chris-Hennes ,
                Might be I couldn't say the problem clearly I use the QButtonGroup now but the problem still exist so could you please run my code and say what you see?

                Header:

                #ifndef MAINWINDOW_H
                #define MAINWINDOW_H
                
                #include <QMainWindow>
                #include <QButtonGroup>
                
                namespace Ui
                {
                class MainWindow;
                }
                
                class MainWindow : public QMainWindow
                {
                    Q_OBJECT
                
                public:
                    explicit MainWindow(QWidget *parent = 0);
                    ~MainWindow();
                private slots:
                    void on_checkBox_clicked();
                private:
                    Ui::MainWindow *ui;
                    QButtonGroup *m_btnGroup;
                };
                
                #endif // MAINWINDOW_H
                
                

                source file :

                #include "mainwindow.h"
                #include "ui_mainwindow.h"
                #include <QDebug>
                
                MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::MainWindow)
                {
                    ui->setupUi(this);
                    m_btnGroup = new QButtonGroup();
                    m_btnGroup->addButton(ui->radioButton);
                    m_btnGroup->addButton(ui->radioButton_2);
                    ui->checkBox->setChecked(true);
                }
                
                MainWindow::~MainWindow()
                {
                    m_btnGroup->deleteLater();
                    delete ui;
                }
                
                void MainWindow::on_checkBox_clicked()
                {
                    bool check = ui->checkBox->isChecked();
                    if(check)
                    {
                         m_btnGroup->setExclusive(true);
                         ui->radioButton->setEnabled(true);
                         ui->radioButton_2->setEnabled(true);
                    }
                    else
                    {
                        m_btnGroup->setExclusive(false);
                        ui->radioButton->setChecked(false);
                        ui->radioButton_2->setChecked(false);
                
                        ui->radioButton->setEnabled(false);
                        ui->radioButton_2->setEnabled(false);
                    }
                }
                
                

                Just create project and put one check box and two radio buttons on ui file then try to select one radio button and uncheck the checkbox now check it again while you are watching the radio buttons(what you see).
                QT SDK = 5.8.0 on windows by mingw compiler set.
                I appreciate your help.

                1 Reply Last reply
                0
                • Chris HennesC Offline
                  Chris HennesC Offline
                  Chris Hennes
                  wrote on last edited by
                  #9
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • Chris HennesC Offline
                    Chris HennesC Offline
                    Chris Hennes
                    wrote on last edited by
                    #10

                    OK, I see what you are getting at -- it's almost like Qt is caching the actual drawing of the checked, active radio button and using that image briefly before updating it to the correct unchecked state. I haven't chased through the signal propagation, but I wonder if hooking into the repaint event of those widgets will give you any information about why this is happening. No amount of fiddling with the radio button grouping or the order of checking/enabling/etc. seems to resolve it. I even tried using a one-shot timer to push the updating of the widget to later in the event queue, which had no effect.

                    AlienA 1 Reply Last reply
                    1
                    • Chris HennesC Chris Hennes

                      OK, I see what you are getting at -- it's almost like Qt is caching the actual drawing of the checked, active radio button and using that image briefly before updating it to the correct unchecked state. I haven't chased through the signal propagation, but I wonder if hooking into the repaint event of those widgets will give you any information about why this is happening. No amount of fiddling with the radio button grouping or the order of checking/enabling/etc. seems to resolve it. I even tried using a one-shot timer to push the updating of the widget to later in the event queue, which had no effect.

                      AlienA Offline
                      AlienA Offline
                      Alien
                      wrote on last edited by
                      #11

                      @Chris-Hennes That's right. So what should I do to get rid of that?

                      1 Reply Last reply
                      0
                      • hskoglundH Online
                        hskoglundH Online
                        hskoglund
                        wrote on last edited by
                        #12

                        Hi, this "ghosting" occurs I think because Qt doesn't paint/render the buttons until you exit on_checkBox_clicked().

                        What you can try is to stage the updates: first do ->setChecked(false) and let Qt paint/render the screen, and then do setEnabled(false), like this:

                            m_btnGroup->setExclusive(false);
                            ui->radioButton->setChecked(false);
                            ui->radioButton_2->setChecked(false);
                        
                             qApp->processEvents();   // <-- forces screen update
                        
                             ui->radioButton->setEnabled(false);
                             ui->radioButton_2->setEnabled(false);
                        

                        qApp->processEvents() is a bit perilous though, you can easily fall into recursive loops, perhaps a better approach would be to "include timer.h" and:

                            m_btnGroup->setExclusive(false);
                            ui->radioButton->setChecked(false);
                            ui->radioButton_2->setChecked(false);
                        
                            QTimer::singleShot(10,[this]
                            {
                                ui->radioButton->setEnabled(false);
                                ui->radioButton_2->setEnabled(false);
                            });
                        

                        Either way that "ghosting" should go away :-)

                        AlienA 1 Reply Last reply
                        4
                        • hskoglundH hskoglund

                          Hi, this "ghosting" occurs I think because Qt doesn't paint/render the buttons until you exit on_checkBox_clicked().

                          What you can try is to stage the updates: first do ->setChecked(false) and let Qt paint/render the screen, and then do setEnabled(false), like this:

                              m_btnGroup->setExclusive(false);
                              ui->radioButton->setChecked(false);
                              ui->radioButton_2->setChecked(false);
                          
                               qApp->processEvents();   // <-- forces screen update
                          
                               ui->radioButton->setEnabled(false);
                               ui->radioButton_2->setEnabled(false);
                          

                          qApp->processEvents() is a bit perilous though, you can easily fall into recursive loops, perhaps a better approach would be to "include timer.h" and:

                              m_btnGroup->setExclusive(false);
                              ui->radioButton->setChecked(false);
                              ui->radioButton_2->setChecked(false);
                          
                              QTimer::singleShot(10,[this]
                              {
                                  ui->radioButton->setEnabled(false);
                                  ui->radioButton_2->setEnabled(false);
                              });
                          

                          Either way that "ghosting" should go away :-)

                          AlienA Offline
                          AlienA Offline
                          Alien
                          wrote on last edited by
                          #13

                          Dear @hskoglund ,
                          Thank you so much that is work for me.

                          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