Passing QRadioButton value from one window to another window in Qt (no matching function for call to)

StackOverflow https://stackoverflow.com/questions/15919333

  •  03-04-2022
  •  | 
  •  

Question

I want to pass selected QRadioButton's value from one Window to another. I am confused with the function declaration to accept the text value in Second Window, here is my code.

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

But when I call

ss = new SelectSoftware();

or

ss= new SelectSoftware(const QString &text, QWidget *parent);

in Window2.cpp, I get the error : no matching function for call to SelectSoftware::SelectSoftware()

Where am I wrong?

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;
    }
Était-ce utile?

La solution

First of all - use signals and slots, Luke

Second of all, you cannot call ss = new SelectSoftware();, since you haven't declared SelectSoftware constructor without parameters, and calling ss= new SelectSoftware(const QString &text, QWidget *parent); is illegal in C++.

SelectSoftware *ss = new SelectSoftware (loc); is correct, though.

Autres conseils

1. In void SelectSoftware::processLine(QString str) addressing to list[0] without checking that list is not empty might be dangerous. I recomend you to add:

if (!list.size())
    return;

right after initialization.

2. In void SelectOption::initializeUi() what is list? Are you sure list.size() <= softwareList.size()? If not, it's a potential problem.

3. What is radioButton? I don't see it's initialization. If it is QList < QRadioButton * >, than radioButton[i] = new QRadioButton(); is a bad one and you should do this:

radioButton.append(new QRadioButton());

4. Same goes to comboBox.

Each of the list can cause the crash of your application. And I could easily miss something.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top