jDoe
jDoe

Reputation: 1023

Qt - Passing data between two forms

I am new to Qt and I have a problem with making the following work:

PS: I've checked some other answered questions but I don't think I am able to solve the problem.

I defined a class that holds two integers with setters/getters in a header then declared a class instance in mainwindow.cpp . I want to get the user's input (two ints) after clicking on a button from a QDialog (it has it's own subclass in a seperate .cpp and .h namely dialogchangesev.h and dialogchangesev.cpp) that holds two QLineEdit , then set the properties of the class instance in mainwindow.cpp to the two inputted int.

The problem is : I get a set of errors. ( as shown below )

Thanks.

Code fragments:

// dialogchangesev.cpp

DialogChangeSEV::DialogChangeSEV(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogChangeSEV)
{
    ui->setupUi(this);
    connect(DialogChangeSEV, SIGNAL( sendIntData(int, int) ),
                      MainWindow, SLOT( setIntData(int,int)));
// Error : C2275: 'DialogChangeSEV' : illegal use of this type as an expression
// Error:   C2275: 'MainWindow' : illegal use of this type as an expression
// see declaration of 'MainWindow'
}
// code goes here ...
void DialogChangeSEV::on_setSEV_clicked()
{
    int se, sv;
    se = ui->setSE->text().toInt();
    sv = ui->setSV->text().toInt();
// emit sendIntData(se,sv) ;
}

//dialogchangesev.h

// code goes here ...
signals:
    void sendIntData(int datae, int datav);
};

//mainwindow.h

// code goes here ...
public slots:
    void setIntData(int datae,int datav);

//mainwindow.cpp

// code goes here ... includes and so on
sizeEV gDimensions;
// ...
void MainWindow::setIntData(int datae,int datav){
    gDimensions.setSE(datae);
    gDimensions.setSV(datav);
}

Upvotes: 1

Views: 1006

Answers (2)

Tyler Lewis
Tyler Lewis

Reputation: 881

Your problem resides in your connect. In the Qt framework, connect takes a pointer to an instance of an object, not the class name. So moving your connect to the constructor for MainWindow:

connect((pointer to a DialogChangeSEV object), SIGNAL(sendIntData(int, int)), this, SLOT(setIntData(int,int)));

So in your mainwindow.cpp, are you declaring a DialogChangeSEV object? If so, just use that pointer.

mainwindow.cpp:
class MainWindow : public QMainWindow()
{
    Q_OBJECT
public:
    ...
private:
    DialogChangeSEV *dcsev;
};

Then later in your MainWindow constructor...

MainWindow::MainWindow(QWidget *parent = 0)
{
    dcsev = new DialogChangeSEV(...);
    connect(dcsev, SIGNAL(sendIntData(int, int)), this, SLOT(setIntData(int, int));
    ...
}

Does that help?

Upvotes: 2

David Marquant
David Marquant

Reputation: 2247

You need to pass objects to connect rather than the types:

 MainWindow *mainWindow;
 DialogChangeSEV *dialog;

 ...

 connect(dialog, SIGNAL( sendIntData(int, int) ),
         mainWindow, SLOT( setIntData(int,int)));

Have a look at the reference for signals and slots in Qt5: http://doc.qt.io/qt-5/signalsandslots.html.

Upvotes: 2

Related Questions