QButtonGroup - cannot check initial button



  • Hi,
    I create QButtonGroup, but I don't know how to do initial checking.
    I've tried simple setChecked(bool) method, but it causes app crash.

    m_boxSingle = new QCheckBox(tr("Single calibration"));  
    m_boxStereo = new QCheckBox(tr("Stereo calibration"));
    
    connect(m_boxSingle, SIGNAL(toggled(bool)), this, SLOT(setSingleCalibration(bool)));
    connect(m_boxStereo, SIGNAL(toggled(bool)), this, SLOT(setStereoCalibration(bool)));
    
    QVBoxLayout* vBoxType = new QVBoxLayout(typeCalibBox);
    vBoxType->addWidget(m_boxSingle);
    vBoxType->addWidget(m_boxStereo);
    
    QButtonGroup* typeCalibGroup = new QButtonGroup(typeCalibBox);
    typeCalibGroup->addButton(m_boxSingle);
    typeCalibGroup->addButton(m_boxStereo);
    //m_boxSingle->setChecked(true);  //<-- here I get error
    typeCalibGroup->setExclusive(true);
    

    Is there any different way to initial check?


  • Lifetime Qt Champion

    Hi,

    Did you check with a debugger the content of m_boxSingle ?

    Is your code sample all called in a single function ?



  • If you look in above code I have two connections there.

    connect(m_boxSingle, SIGNAL(toggled(bool)), this, SLOT(setSingleCalibration(bool)));
    connect(m_boxStereo, SIGNAL(toggled(bool)), this, SLOT(setStereoCalibration(bool)));
    

    In my GUI I have one QGroupBox, where user can choose from two options: single or stereo calibration. Depending on user choice in second QGroupBox (showed in code in first post) he can choose one (for single) or two (for stereo) cameras. So I created that connections to change exlusive option for second group.

    void Calibration::setSingleCalibration(bool checked)
    {
        for (int i = 0; i < m_cameraCheckBox.size(); i++)
            m_cameraCheckBox[i]->setChecked(false);
    
        if(m_cameraCheckBox.size() > 0)
            m_cameraCheckBox[0]->setChecked(true);
        m_cameraGroup->setExclusive(true);
    }
    
    void Calibration::setStereoCalibration(bool checked)
    {
        m_cameraGroup->setExclusive(false);
    }

  • Lifetime Qt Champion

    Why not use QRadioButton ? Looks like a better widget for selecting only one option.



  • I thought that QChecBox is better choice because sometimes I choose only one option and sometimes more than one option. It depends on what is user's first choice. If he/she chooses single calibration option then second button group should be exclusive, if he/she chooses stereo calibration option then second button group should not be exclusive.

    However I checked QRadioButton, and it also causes app crash


  • Moderators

    @never_ever App crash? Then you most probably dereference an invalid pointer. Did you try to debug to see where it crashes?



  • Ok,. I found where problem is. I make two connections. One of them was connection to slot setSingleCalibration(), and that slot made troubles, because it was called before creating second group of buttons that were used in that slot. My mistake :P


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.