Question

I am trying to create a QWizard. I need to use combinations of QRadioButton and QComboBox in first Window, and QCheckBox and QComboBox in second Window.

In the first Window, the information coming as text per RadioButton is from Directories in a path specified, and the ComboBox list is the iteration of Directories inside RadioButton's directory.

In the Second Window, the process is same as above, instead of Radio buttons, there will be Check boxes though from another Directory path.

The radio buttons and Combobox are inter-dependent with Signals and Slots, if one radio button is checked, its corresponding Combobox must be activated and if next radio is chosen, the first must be disabled. But this is not happening, Combobox list is incrementing each and every time when I check it and corresponding combobox is also not disabling.

Here is my code:

    void SelectOption::initializeUi()
{
    this->setWindowTitle("Window-1");

    QGridLayout *gridLayout1 = new QGridLayout();
    gridLayout1->setMargin(5);
    gridLayout1->setSpacing(5);

    QSignalMapper* signalMapper = new QSignalMapper();

    for(int i = 0; i < list.size(); i++){
        radioButton[i] = new QRadioButton();
        radioButton[i]->setText(softwareList[i]);
        signalMapper->setMapping(radioButton[i], i);
        gridLayout1->addWidget(radioButton[i], i/1, i%1);
        connect(radioButton[i], SIGNAL(clicked()),signalMapper, SLOT(map()));
    }

connect(signalMapper, SIGNAL(mapped(const int &)),this, SIGNAL(radioChecked(const int &)));
connect(this, SIGNAL(radioChecked(const int &)),this, SLOT(test(const int)));

    QGridLayout *gridLayout2 = new QGridLayout();
    gridLayout2->setMargin(5);
    gridLayout2->setSpacing(5);

    for(int j = 0; j < list.size(); j++){
        comboBox[j] = new QComboBox();
        comboBox[j]->setDisabled(true);
        gridLayout2->addWidget(comboBox[j], j/1, j%1);
    }

    QPushButton *nextButton = new QPushButton("Next >");
    nextButton->setDefault(true);
    connect(nextButton, SIGNAL(clicked()), this, SLOT(showMainPage()));

    QPushButton *backButton = new QPushButton("< Back");
    backButton->setDefault(true);
    connect(backButton, SIGNAL(clicked()), this, SLOT(showSelectOS()));

    QPushButton *cancelButton = new QPushButton("Cancel");
    cancelButton->setDefault(true);
    connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel()));

    QHBoxLayout *hboxlayout;
    hboxlayout = new QHBoxLayout();
    hboxlayout->addLayout(gridLayout1);
    hboxlayout->addLayout(gridLayout2);

    QHBoxLayout *layout;
    layout = new QHBoxLayout();
    layout->addStretch(10);
    layout->addWidget(nextButton);
    layout->addWidget(backButton);
    layout->addWidget(cancelButton);
    layout->addStretch(10);

    QVBoxLayout *mainLayout;
    mainLayout = new QVBoxLayout();
    mainLayout->addLayout(hboxlayout);
    mainLayout->addLayout(layout);
    ui->centralwidget->setLayout(mainLayout);
}

void SelectOption::test(const int id) // REMEMBER
{
    if(radioButton[id]->isChecked()){
        comboBox[id]->setEnabled(true);
        comboBox[id]->addItem(" Select anyone ");
        QString path = qApp->applicationDirPath() + "/list/" + radioButton[id]->text();

        QDir dir;
        dir.cd(path);
        dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);

        QFileInfoList list = dir.entryInfoList();
        for (int i = 0; i < list.size(); ++i) {
            QFileInfo fileInfo = list.at(i);
            comboBox[id]->addItem(fileInfo.fileName());
        }

    }else{
        comboBox[id]->clear();
        comboBox[id]->setDisabled(true);
    }
}
Was it helpful?

Solution

Add all your radio buttons to one QButtonGroup. Then checking one of them will uncheck all others.

Update (code example).

There is absolutely nothing difficult in using QButtonGroup. All you need is to add QAbstractButton inheritor to it with void QButtonGroup::addButton ( QAbstractButton * button ) method, all other will be done automatically. Here is a simple code example, adding three radio buttons to a main window in it's constructor:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    setupUi(this);

    QVBoxLayout *layout = new QVBoxLayout(QMainWindow::centralWidget());

    QRadioButton *r1 = new QRadioButton("r1", this);
    QRadioButton *r2 = new QRadioButton("r2", this);
    QRadioButton *r3 = new QRadioButton("r3", this);

    layout->addWidget(r1);
    layout->addWidget(r2);
    layout->addWidget(r3);
    layout->addStretch();

    QButtonGroup *group = new QButtonGroup(this);
    group->addButton(r1);
    group->addButton(r2);
    group->addButton(r3);

    r1->setChecked(true);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top