Retrieve information from mainwindow and display the information on newwindow::issues
-
Greetings,
Regarding to the title above, i m trying to use SIGNAL and SLOT method for implementation . Here my codes:
***mainwindow.h**** #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QObject> #include <QString> #include "newwindow.h" class mainWindow { int i_value; newWindow *myNewWindow(); public: mainWindow(); void setI_Value(int x); int getI_Value(); void changeValueTo(int y); signals: void dataAvailable(const int &y); }; #endif // MAINWINDOW_H
***newwindow.h*** #ifndef NEWWINDOW_H #define NEWWINDOW_H #include <QObject> class newWindow { public: newWindow(); slots: void getDataFromMainWindow(int &y); }; #endif // NEWWINDOW_H
***mainwindow.cpp*** #include "mainwindow.h" #include "newwindow.h" #include <QObject> using namespace std; mainWindow::mainWindow() { i_value = 10; QObject::connect(this,SIGNAL(dataAvailable(int)),myNewWindow,SLOT(getDataFromMainWindow(int))); } void mainWindow::setI_Value(int x) { i_value = x; } int mainWindow::getI_Value() { return i_value; } void mainWindow::changeValueTo(int &y) { if (y!=i_value){ i_value = y; emit dataAvailable(y); } }
***newwindow.cpp*** #include "newwindow.h" #include "mainwindow.h" #include <QObject> newWindow::newWindow() { } void newWindow::getDataFromMainWindow(int y) { int xyz = z; qDebug()<< xyz; }
Do spend some time to understand the codes, its quite simple to understand . Hoping you will get the full picture after you gain the understanding from my code. Now, when i compile them , i get a lot of error which me also dont understanding what is happening . Someone here please share with me your knowledge on this approach. Spend so many days to solve this issue. Without solving this issue, i can not proceed to my next task!Thank you in advance!
-
Whats that: newWindow *myNewWindow(); ?
I cannot understand your code. Mainwindow has not a base class. Is is not really a window !?
You can use signals and slots only in subclasses of a QObject and you must use the Q_OBJECT macro.
class SubClass : public QObject
{ Q_OBJECT
signals:
slots:
} -
Hi ,
The thing that you asked is to create a new object from NewWidow, an object from created different class. The purpose of this is i want to "put" this object in the connect function . something like this :
QObject :: connect (this, SIGNAL(dataAvailable(int y)),myNewWindow,SLOT(getDataFromMainWindow(int y)));
the signal is emitted from main window , and the search for the slots in new Window .
-
Please read http://doc.qt.io/qt-5.6/signalsandslots.html
As @Andy314 already said the class newWindow must be a subclass of QObject and have Q_OBJECT macro in it in order to use signals/slots!