0

我想将选定的 QRadioButton 的值从一个窗口传递到另一个窗口。我对在第二个窗口中接受文本值的函数声明感到困惑,这是我的代码。

窗口1.cpp

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

窗口2.h

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

窗口2.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()

我哪里错了?

更新

窗口2.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;
    }
4

2 回答 2

3

首先 -使用信号和插槽,卢克

其次,您不能 call ss = new SelectSoftware();,因为您没有声明SelectSoftware没有参数的构造函数,并且在 C++ 中调用ss= new SelectSoftware(const QString &text, QWidget *parent);是非法的。

SelectSoftware *ss = new SelectSoftware (loc);不过是正确的。

于 2013-04-10T07:34:48.710 回答
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

每个列表都可能导致您的应用程序崩溃。我很容易错过一些东西。

于 2013-04-10T15:51:28.587 回答