Solved How to read struct members from another class
-
I have a struct in my mainwindow class:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include "dialog.h" #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); struct properties{ int ID = -1; }; properties ret_func(); private slots: void on_btn1_clicked(); private: Ui::MainWindow *ui; properties _properties; Dialog *_dialog; }; #endif // MAINWINDOW_H
I set value to it's memeber in
mainwindow.cpp
by clicking onbtn1
:MainWindow::properties MainWindow::ret_func() { return _properties; } void MainWindow::on_btn1_clicked() { _properties.ID = ui->lineEdit->text().toInt(); qDebug()<<_properties.ID; _dialog->exec(); }
by clicking on
btn1
another window is opened and I want to read the struct member value by clicking onbtn2
. This is my code:void Dialog::on_btn2_clicked() { qDebug()<<MainWindow::ret_func().ID;//->this line has error }
Error:
error: cannot call member function 'MainWindow::properties MainWindow::ret_func()' without object qDebug()<<MainWindow::ret_func().ID; ^
I have read some threads having similar problems but couldn't understand solution
please help me -
Hi
you need a pointer to the existing MainWindow and
say
qDebug()<< winPtr->ret_func().ID;However, since Dialog also needs _properties, why not give it to Dialog directly
by extending the constructor to take such parameter.
Dialog mydia(this, _properties );
mydia.exec(); -
@mrjj
you mean this?MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); _dialog = new Dialog(this, _properties); }
Error:
error: no matching function for call to 'Dialog::Dialog(MainWindow*, MainWindow::properties&)' _dialog = new Dialog(this, _properties); ^
-
@rezaMSLM
yes but you have to change the Dialogs constructor so it can take one. -
@mrjj
I don't get your meaning -
@rezaMSLM
Show Dialogs constructor please :) -
-
@rezaMSLM
#include "MainWindow.h" // so it knows it.explicit Dialog(QWidget *parent , MainWindow::properties & theProps );
also add the same in . cpp file
Note the &. if you omit that, it will get a copy.
-
@mrjj
in my dialog.h:public: explicit Dialog(QWidget *parent = 0, MainWindow::_properties & theProps); ~Dialog();
in dialog.cpp
Dialog::Dialog(QWidget *parent, MainWindow::_properties &theProps) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); }
But I receive Errors in these lines of mainwindow.h:
private: Ui::MainWindow *ui; properties _properties;// error: 'MainWindow::properties MainWindow::_properties' is private properties _properties; ^ Dialog *_dialog;// error: 'Dialog' does not name a type Dialog *_dialog; };
and in dialog.h:
public: explicit Dialog(QWidget *parent = 0, MainWindow::_properties & theProps);/* error: default argument missing for parameter 2 of 'Dialog::Dialog(QWidget*, int&)' explicit Dialog(QWidget *parent = 0, MainWindow::_properties & theProps); ^ */ ~Dialog();
-
Hi
Remove the =0
explicit Dialog(QWidget *parent = 0, MainWindow::_properties & theProps);
or swap the order of parameters if you want a default value.for the
Dialog *_dialog;// error: 'Dialog' does not name a type Dialog *_dialog;is due to us using Mainwindow.h in Dialog.h and made a circular includes. ( which is forbidden)
to fix it.
over MainWindow class.
type
class Dialog; // this is a forward
and move the #include "Dialog.h" to mainwindow. cpp file.