我正在尝试创建一个QWizard。我需要在第一个窗口中使用QRadioButton和QComboBox的组合,在第二个窗口中使用QCheckBox和QComboBox。
在第一个窗口中,每个RadioButton的文本信息来自指定路径中的目录,而ComboBox列表是RadioButton目录中目录的迭代。
在第二个窗口中,过程与上面相同,不是单选按钮,而是来自另一个目录路径的复选框。
单选按钮和组合框与信号和插槽相互依赖,如果选中了一个单选按钮,则必须激活其对应的组合框,如果选择了下一个单选按钮,则必须禁用第一个单选按钮。但这并没有发生,组合框列表每次都在增加,当我检查它时,相应的组合框也没有禁用。
下面是我的代码: 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);
}
}
将所有单选按钮添加到一个QButtonGroup
。然后选中其中一个将取消选中其他所有。
更新(代码示例)。
使用QButtonGroup
绝对没有什么困难。你所需要做的就是用void QButtonGroup::addButton ( QAbstractButton * button )
方法添加QAbstractButton
继承器,其他的都将自动完成。下面是一个简单的代码示例,在它的构造函数中向主窗口添加三个单选按钮:
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);
}