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. Multiple QRadioButton::setChecked() in QDialog
Forum Updated to NodeBB v4.3 + New Features

Multiple QRadioButton::setChecked() in QDialog

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.1k Views 1 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.
  • ModelTechM Offline
    ModelTechM Offline
    ModelTech
    wrote on last edited by
    #1

    I have created a QDialog subclass that includes to sets of QRadioButtons. Each of these sets is defined by using QGroupBox and for each QGroupBox, I have set one of the QRadioButtons to be checked (by using QRadioButton::setChecked(true). However, when executing the code, only the QRadioButton for which the setChecked function is called last is actually visibly checked while for the other, its status is not visualized. See picture.
    Visibility Checked QRadioButton
    The code corresponding to the above QDialog looks as follows:

        // Create Buttons
    
        Minimum = new QRadioButton(tr("Minimum"), this);
        Average = new QRadioButton(tr("Average"), this);
        Maximum = new QRadioButton(tr("Maximum"), this);
        Maximum->setChecked(true);
    
        QRadioButton *Instantiated = new QRadioButton(tr("Instantiated Process Classes Only"), this);
        Instantiated->setChecked(true);
        All = new QRadioButton(tr("All Process Classes"), this);
    
        QDialogButtonBox *ButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
        connect(ButtonBox, SIGNAL(accepted()), this, SLOT(accept()));
        connect(ButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
    
        // Layout
    
        QGroupBox *Fix = new QGroupBox(tr("Replace Execution Time Distributions by"), this);
        QVBoxLayout *FixLayout = new QVBoxLayout;
        FixLayout->addWidget(Minimum);
        FixLayout->addWidget(Average);
        FixLayout->addWidget(Maximum);
        Fix->setLayout(FixLayout);
    
        QGroupBox *Classes = new QGroupBox(tr("For behavior of"), this);
        QVBoxLayout *ClassLayout = new QVBoxLayout;
        ClassLayout->addWidget(Instantiated);
        ClassLayout->addWidget(All);
        Classes->setLayout(ClassLayout);
    
        QVBoxLayout *Layout = new QVBoxLayout;
        Layout->addWidget(Fix);
        Layout->addWidget(Classes);
        Layout->addWidget(ButtonBox);
        setLayout(Layout);
    
        setWindowTitle(tr("Fix Execution Times"));
    

    Any ideas on how to make sure that both programmatically selected QRadioButtons are visibly checked?

    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by hskoglund
      #2

      Hi, I think because all 5 of the QRadioButtons share the parent widget only 1 can be active. Try reshuffling your code so that the first 3 buttons change owner to the Fix groupbox, then after that create the 2 button after that, like this:

      ...
      // Create Buttons
         Minimum = new QRadioButton(tr("Minimum"), this);
          Average = new QRadioButton(tr("Average"), this);
          Maximum = new QRadioButton(tr("Maximum"), this);
          Maximum->setChecked(true);
      
          QGroupBox *Fix = new QGroupBox(tr("Replace Execution Time Distributions by"), this);
          QVBoxLayout *FixLayout = new QVBoxLayout;
          FixLayout->addWidget(Minimum);
          FixLayout->addWidget(Average);
          FixLayout->addWidget(Maximum);
          Fix->setLayout(FixLayout);
      
          QRadioButton *Instantiated = new QRadioButton(tr("Instantiated Process Classes Only"), this);
          Instantiated->setChecked(true);
          All = new QRadioButton(tr("All Process Classes"), this);
      
          QDialogButtonBox *ButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
          connect(ButtonBox, SIGNAL(accepted()), this, SLOT(accept()));
          connect(ButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
      
          QGroupBox *Classes = new QGroupBox(tr("For behavior of"), this);
          QVBoxLayout *ClassLayout = new QVBoxLayout;
          ClassLayout->addWidget(Instantiated);
          ClassLayout->addWidget(All);
          Classes->setLayout(ClassLayout);
      ...
      

      Edit: instead of reshuffling, another solution is to create 2 QButtonGroup instances, insert the first 3 radiobuttons in one of them and 2 in the other (i.e. another way to split the owner of the buttons).

      ModelTechM 1 Reply Last reply
      4
      • hskoglundH hskoglund

        Hi, I think because all 5 of the QRadioButtons share the parent widget only 1 can be active. Try reshuffling your code so that the first 3 buttons change owner to the Fix groupbox, then after that create the 2 button after that, like this:

        ...
        // Create Buttons
           Minimum = new QRadioButton(tr("Minimum"), this);
            Average = new QRadioButton(tr("Average"), this);
            Maximum = new QRadioButton(tr("Maximum"), this);
            Maximum->setChecked(true);
        
            QGroupBox *Fix = new QGroupBox(tr("Replace Execution Time Distributions by"), this);
            QVBoxLayout *FixLayout = new QVBoxLayout;
            FixLayout->addWidget(Minimum);
            FixLayout->addWidget(Average);
            FixLayout->addWidget(Maximum);
            Fix->setLayout(FixLayout);
        
            QRadioButton *Instantiated = new QRadioButton(tr("Instantiated Process Classes Only"), this);
            Instantiated->setChecked(true);
            All = new QRadioButton(tr("All Process Classes"), this);
        
            QDialogButtonBox *ButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
            connect(ButtonBox, SIGNAL(accepted()), this, SLOT(accept()));
            connect(ButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
        
            QGroupBox *Classes = new QGroupBox(tr("For behavior of"), this);
            QVBoxLayout *ClassLayout = new QVBoxLayout;
            ClassLayout->addWidget(Instantiated);
            ClassLayout->addWidget(All);
            Classes->setLayout(ClassLayout);
        ...
        

        Edit: instead of reshuffling, another solution is to create 2 QButtonGroup instances, insert the first 3 radiobuttons in one of them and 2 in the other (i.e. another way to split the owner of the buttons).

        ModelTechM Offline
        ModelTechM Offline
        ModelTech
        wrote on last edited by
        #3

        @hskoglund Thanks for the hint to change the ownership. That solved the problem. I have now the following code:

            // Create Buttons
        
            QGroupBox *Fix = new QGroupBox(tr("Replace Execution Time Distributions by"), this);
            Minimum = new QRadioButton(tr("Minimum"), Fix);
            Average = new QRadioButton(tr("Average"), Fix);
            Maximum = new QRadioButton(tr("Maximum"), Fix);
            Maximum->setChecked(true);
        
            QGroupBox *Classes = new QGroupBox(tr("For behavior of"), this);
            All = new QRadioButton(tr("All Process Classes"), Classes);
            QRadioButton *Instantiated = new QRadioButton(tr("Instantiated Process Classes Only"), Classes);
            Instantiated->setChecked(true);
        
            QDialogButtonBox *ButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
            connect(ButtonBox, SIGNAL(accepted()), this, SLOT(accept()));
            connect(ButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
        
            // Layout
        
            QVBoxLayout *FixLayout = new QVBoxLayout;
            FixLayout->addWidget(Minimum);
            FixLayout->addWidget(Average);
            FixLayout->addWidget(Maximum);
            Fix->setLayout(FixLayout);
        
            QVBoxLayout *ClassLayout = new QVBoxLayout;
            ClassLayout->addWidget(Instantiated);
            ClassLayout->addWidget(All);
            Classes->setLayout(ClassLayout);
        
            QVBoxLayout *Layout = new QVBoxLayout;
            Layout->addWidget(Fix);
            Layout->addWidget(Classes);
            Layout->addWidget(ButtonBox);
            setLayout(Layout);
        
            setWindowTitle(tr("Fix Execution Times"));
        
        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