0

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;
    }
abhi abhi
  • 181
  • 1
  • 2
  • 10
  • Have you changed your `SelectSoftware` constructor? Can you show it? The code above should not crash in the line you marked. Though sometimes valid code, built by QtCreator crashes, because it rebuilds not all of changed object files. In order to fix that you need to rebuild your project ('Build->Clean All' + 'Build->Run qmake' + 'Build->Rebuild All') – Amartel Apr 10 '13 at 11:28
  • @Amartel Hmm maybe.. `SelectSoftware` constructor has been updated... please check... – abhi abhi Apr 10 '13 at 12:09

2 Answers2

3

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.

Amartel
  • 4,248
  • 2
  • 15
  • 21
  • Thanks again :D, I need this `loc` variable to be available in each every function definition in Window2.cpp, how can I do this?? – abhi abhi Apr 10 '13 at 07:59
  • 1
    You should make it a member of your `SelectSoftware` class. Add `QString softpath;` to a class definition in header and `softpath = loc;` in `SelectSoftware` constructor instead of `QString softpath = text;`. After that `softpath` will be available in each member of `SelectSoftware` class. – Amartel Apr 10 '13 at 08:03
  • Well, now I'll tell my problem: in Window2.cpp, there is one SLOT called test(), in that test, its being defined as `ss = new SelectSoftware` , as we've provided arguments `QString &text`, I've modified to `ss = new SelectSoftware(text)`. But this is creating the problem, again its executing `SelectSoftware(text)` function, where the value of text has become empty and the application is crashing!!! – abhi abhi Apr 10 '13 at 10:26
  • It's hard to tell where the problem is, since I don't know, how you are using this string, and what your test slot is for. You should run your application with debugger and find the line of your code, where your program crashes. Then you should fix the program logic. – Amartel Apr 10 '13 at 10:46
1

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.

Amartel
  • 4,248
  • 2
  • 15
  • 21
  • Can we pass multiple text parameter values to `SelectSoftware::SelectSoftware(const QString &text, QWidget *parent)` or we need to do like `SelectSoftware::SelectSoftware(const QString &text,const QString &path, QWidget *parent)` to pass another variable ?? – abhi abhi Apr 11 '13 at 06:24
  • Hmm.. then what is Overloading for? Can't we pass two text values to single Parameter? – abhi abhi Apr 11 '13 at 08:25
  • Please, read the article about overloading: http://en.wikipedia.org/wiki/Method_overloading . I hate to saying this, but some of your questions are described in manuals very well. – Amartel Apr 11 '13 at 08:31