2013-04-10 49 views
0

我想將選定QRadioButton的值從一個窗口傳遞到另一個窗口。我很困惑函數聲明接受第二個窗口中的文本值,這是我的代碼。將QRadioButton值從一個窗口傳遞到Qt中的另一個窗口(沒有匹配函數用於調用)

Window1.cpp

void SelectOS :: processNextButton(){ 
if(ui->win32->isChecked()){ 
QString loc = "WIN/32Bit"; 
SelectSoftware *ss = new SelectSoftware (loc); 
this->hide(); 
ss->show(); 
} 
else 
{ 
//QMessageBox:warning(); 
} 
} 

Window2.h

public: 
SelectSoftware(const QString &text, QWidget *parent=0); 

Window2.cpp

SelectSoftware::SelectSoftware(const QString &text, QWidget *parent):QMainWindow(parent),ui(new ui::SelectSoftware) 
{ 
QString softpath = text; 
qDebug << softpath; 
} 

但是,當我打電話

ss = new SelectSoftware(); 

ss= new SelectSoftware(const QString &text, QWidget *parent); 
在Window2.cpp

,我得到的錯誤:no matching function for call to SelectSoftware::SelectSoftware()

我在哪裏錯了?

UPDATE

Window2.cpp

#include "selectsoftware.h" 
#include "ui_selectsoftware.h" 

SelectSoftware *ss; 
QStringList selectedModuleList; 

SelectSoftware::SelectSoftware(const QString &text, QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::SelectSoftware) 
{ 
    ui->setupUi(this); 
    softpath = text; 
    setWindowPosition(); 
    getSoftwareDetails(); 
    initializeUi(); 
} 

SelectSoftware::~SelectSoftware() 
{ 
    delete ui; 
} 

void SelectSoftware::setWindowPosition() 
{ 
    QDesktopWidget *desktop = QApplication::desktop(); 
    int x = (desktop->width() - size().width())/2; 
    int y = (desktop->height() - size().height())/2; 
    move(x, y-50); 
    setFixedSize(size().width(), size().height()); 
} 

void SelectSoftware::cancel() 
{ 
    qApp->exit(0); 
} 

    void SelectSoftware::showMainPage() 
    { 
     ss = new SelectSoftware(softpath); // here its creating problem, not going forward and app is crashing!!! 

     for(int j = 0; j < softwareList.size(); j++){ 
      if(checkBox[j]->isChecked()){ 
       if(!comboBox[j]->currentIndex()){ 
        QMessageBox::warning(this, "Select version !", "Select version number for all selected software"); 
        return; 
       } 
      } 
     } 

     for(int i = 0; i < softwareList.size(); i++){ 
      if(checkBox[i]->isChecked()){ 
       ss->selectedSoftList.push_back(checkBox[i]->text()); 
       ss->selectedVerList.push_back(comboBox[i]->currentText()); 
      } 
     } 

     if(!ss->selectedSoftList.size()){ 
      QMessageBox::warning(this, "No product Selected !", "Select one"); 
      return; 
     } 

    else{ 
      SelectionPage* sp = new SelectionPage; 
      this->hide(); 
      sp->show(); 
     } 
    } 

    void SelectSoftware::test(const int id) 
    { 
     if(checkBox[id]->isChecked()){ 
      comboBox[id]->setEnabled(true); 
      comboBox[id]->addItem(" Select anyone "); 
      QString path = qApp->applicationDirPath() + "/products/" + checkBox[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); 
     } 
    } 

    void SelectSoftware::getSoftwareDetails() 
    { 
     QString fileName = qApp->applicationDirPath() + "/abc/" + SOFTWARELIST; 
     QFile file(fileName&#41;; 
     if (!file.open(QIODevice::ReadOnly | QIODevice::Text&#41;){ 
      QString msg = "Could not find the file " + fileName; 
      errorExit(msg); 
     } 

     QTextStream in(&file); 
     while (!in.atEnd()) { 
      QString line = in.readLine(); 
      processLine(line.toLower()); 
     } 
    } 

    void SelectSoftware::processLine(QString str) 
    { 
     QStringList list = str.split(","); 
     QDir path = qApp->applicationDirPath() + "/products/" + list[0]; 
     if(path.exists() && (list.size() == 2)){ 
      QString tmp = list[0]; 
      tmp = tmp.toLower(); 
      softwareList.push_back(tmp); 
     } 
    } 

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

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

    QVector<QString> SelectSoftware::getSelectedSoftware() 
    { 
     return ss->selectedSoftList; 
    } 

    QVector<QString> SelectSoftware::getSelectedVersion() 
    { 
     return ss->selectedVerList; 
    } 

    QStringList SelectSoftware::getSelectedModules() 
    { 
     return selectedModuleList; 
    } 
+0

你是否改變了你的'SelectSoftware'構造函數?你能證明嗎?上面的代碼不應該在您標記的行中崩潰。儘管有時由QtCreator構建的有效代碼會崩潰,因爲它不會重建所有已更改的目標文件。爲了解決你需要重建你的項目('Build-> Clean All'+'Build-> Run qmake'+'Build-> Rebuild All') – Amartel 2013-04-10 11:28:09

+0

@Amartel嗯也許..'SelectSoftware'構造函數已更新...請檢查... – 2013-04-10 12:09:22

回答

3

首先 - 所有的use signals and slots, Luke

其次,你不能叫ss = new SelectSoftware();,因爲你還沒有不帶參數的聲明SelectSoftware構造,並且在C++中調用ss= new SelectSoftware(const QString &text, QWidget *parent);是非法的。

SelectSoftware *ss = new SelectSoftware (loc);是正確的,但。

+0

再次感謝:D,我需要這個'loc'變量在Window2.cpp的每個函數定義中都可用,我該怎麼做? – 2013-04-10 07:59:15

+1

你應該讓它成爲你的'SelectSoftware'類的成員。在'SelectSoftware'構造函數中添加'QString softpath;'到頭文件中的類定義和'softpath = loc;'而不是'QString softpath = text;'。之後''軟件路徑'將在每個'SelectSoftware'類的成員中可用。 – Amartel 2013-04-10 08:03:26

+0

好吧,現在我告訴我的問題:在Window2.cpp中,有一個名爲test()的SLOT,在該測試中,它被定義爲'ss = new SelectSoftware',因爲我們提供了參數'QString&text' ,我已經修改爲'ss = new SelectSoftware(text)'。但是這就產生了這個問題,再次執行它的'SelectSoftware(text)'函數,其中文本的值已經變空了,應用程序崩潰了! – 2013-04-10 10:26:10

1

1.void SelectSoftware::processLine(QString str)尋址到list[0]沒有檢查該列表不是空的可能是危險的。我建議您添加:

if (!list.size()) 
    return; 

初始化後。

2.void SelectOption::initializeUi()什麼是list?你確定list.size() <= softwareList.size()?如果不是,這是一個潛在的問題。

3.什麼是radioButton?我沒有看到它的初始化。如果QList < QRadioButton * >,比radioButton[i] = new QRadioButton();是一個壞的,你應該這樣做:

radioButton.append(new QRadioButton()); 

4.同去comboBox

每個列表都可能導致應用程序崩潰。我很容易錯過任何東西。

+0

我們是否可以將多個文本參數值傳遞給'SelectSoftware :: SelectSoftware(const QString&text,QWidget * parent)'或者我們需要像'SelectSoftware :: SelectSoftware(const QString&text,const QString&path,QWidget * parent)'傳遞另一個變量? – 2013-04-11 06:24:32

+0

是的,你是對的。 – Amartel 2013-04-11 06:38:25

+0

嗯..那麼什麼是超載?我們不能將兩個文本值傳遞給單個參數嗎? – 2013-04-11 08:25:19

相關問題