2013-04-09 53 views
1

我想創建一個QWizard。我需要在第一個窗口中使用QRadioButton和QComboBox的組合,並在第二個窗口中使用QCheckBox和QComboBox。QWizard QCheckBox,QRadioButton和QComboBox:信號/插槽和SignalMapping - 所需信息

在第一個窗口中,作爲每個RadioButton的文本傳出的信息來自指定路徑中的目錄,而ComboBox列表是RadioButton目錄內目錄的迭代。

在第二個窗口中,過程與上述相同,而不是單選按鈕,但會從另一個目錄路徑中複選框。

單選按鈕和組合框與信號和插槽相互依賴,如果選中一個單選按鈕,則必須激活其相應的組合框,並且如果選擇了下一個無線電,則必須禁用第一個無線電。但是這並沒有發生,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); 
    } 
} 

回答

2

添加您的所有單選按鈕一個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); 
} 
+0

感謝您的回覆,但QButtonGroup不再可用我想?相反,我必須使用QGroupBox?你可以在我的代碼中展示給我... – 2013-04-09 13:49:22

+0

'QButtonGroup'和'QGroupBox'是兩個完全不同的東西。你爲什麼認爲它不可用?你使用Qt5嗎?這裏是:http://qt-project.org/doc/qt-5.0/qtwidgets/qbuttongroup.html – Amartel 2013-04-09 13:53:42

+0

再次感謝,我恐怕我不知道QRadioButtons的QButtonGroup的用法,請你幫我代碼?? – 2013-04-09 14:05:41