[SOLVED] Having trouble with QRadioButton::setChecked()



  • Hi guys, this is my first time posting. I'm creating an e-Exam program, like the ones they use in some schools to test the students.

    In class MainWindow I have a vector of QRadioButtons, which are used to select the answer. When you click Next or Previous the QRadioButtons change their text to the next or previous question's possible answers respectively. The thing is, when Next or Previous is clicked the program is suppose to uncheck all of the QRadioButtons. It doesn't do that. The problem is in "mainwindow.cpp" on line 54.

    Here's what happens when I run the program. I click any QRadioButton, press Next, the text of all of the QRadioButtons is changed to the next questions possible answers, but the QRadioButton that I clicked is still checked. I would really appreciate it if you guys can help me out.

    Here's the source:
    https://www.dropbox.com/s/kt6yp0a57k44ovz/ExamSource.zip

    After you compile it, just drag and drop "sample_test.txt" into the executable.

    Thanks in advance to anyone who replies!



  • Sounds like you are trying to implement "QWizard":http://qt-project.org/doc/qt-5/QWizard.html#details

    Have not look into sources yet though.



  • Welcome to the forum.

    Issue is Radiobuttons get into exclusive mode. I'm assuming that you are using the the same radiobutton object instances in each page. So once you set into check state, you will not able clear the state. You can do the following work-around.
    You need to find which radio button has been clicked. You can do this in slot using the sender() method. Once you find the radio button, you can do in the example given below. I have just implemented using the Timer to show the functionality.

    @Widget::Widget(QWidget *parent)
    : QWidget(parent)
    {
    QVBoxLayout *lyt = new QVBoxLayout(this);
    but = new QRadioButton("One");
    lyt->addWidget(but);
    but1 = new QRadioButton("Two");
    lyt->addWidget(but1);
    but2 = new QRadioButton("Three");
    lyt->addWidget(but2);
    tim.setInterval(5000);
    tim.start();
    connect(&tim,SIGNAL(timeout()),this,SLOT(myslot()));
    }
    void Widget::myslot(){
    qDebug() << "Setting to false"<<endl;

    but->setAutoExclusive(false);
    but->setChecked(false);
    but->setAutoExclusive(true);
    

    }@



  • @Dheerendra

    Thank you so much! Now everything works!


Log in to reply
 

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