将QRadioButton值从一个窗口传递到Qt中的另一个窗口(没有匹配函数用于调用)

问题描述:

我想将选定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

首先 - 所有的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.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