1

我正在尝试创建一个 QWizard。我需要在第一个窗口中使用 QRadioButton 和 QComboBox 的组合,在第二个窗口中使用 QCheckBox 和 QComboBox。

在第一个窗口中,每个 RadioButton 以文本形式出现的信息来自指定路径中的目录,而 ComboBox 列表是 RadioButton 目录中目录的迭代。

在第二个窗口中,过程与上面相同,而不是单选按钮,而是来自另一个目录路径的复选框。

单选按钮和 Combobox 与 Signals 和 Slots 相互依赖,如果选中一个单选按钮,则必须激活其对应的 Combobox,如果选择下一个单选,则必须禁用第一个单选。但这并没有发生,当我检查它时,组合框列表每次都会增加,并且相应的组合框也没有禁用。

这是我的代码:

    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);
    }
}
4

1 回答 1

2

将所有单选按钮添加到一个QButtonGroup. 然后选中其中一个将取消选中所有其他。

更新(代码示例)。

使用起来绝对没有什么难的QButtonGroup。您只需要使用方法向其添加QAbstractButton继承者void QButtonGroup::addButton ( QAbstractButton * button ),所有其他将自动完成。这是一个简单的代码示例,在其构造函数中向主窗口添加三个单选按钮:

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);
}
于 2013-04-09T13:38:58.613 回答