How To Connect Emitter From MainWindow To AnotherWindow
-
I have a main program in MainWindow whose computational results I emit to SecondWindow
mainwindow.h
private: secondwindow *_secondwindow; signals: void sendLabel(QString stringDisplay);
secondwindow.h
private: MainWindow *_mainW; private slots: void changeDisplay(QString display);
mainwindow.cpp
void MainWindow::secondwindow(){ _secondwindow = new secondwindow(this) _secondwindow->showMaximized(); emit sendLabel("400"); }
secondwindow.cpp
secondwindow::secondwindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::secondwindow) { ui->setupUi(this); connect(mainW, SIGNAL(sendLabel()),this,SLOT(changeDisplay())); } void secondwindow::changeDisplay(QString display){ ui->lcdNumber->display(display); }
But I get an error because the main window is not recognized
error: ‘MainWindow’ does not name a type; did you mean ‘QMainWindow’? 27 | MainWindow *_mainW; | ^~~~~~~~~~ | QMainWindow
What is the correct way to emit variable from main window to another window ?
-
@Eshya said in How To Connect Emitter From MainWindow To AnotherWindow:
private:
MainWindow *_mainW;You do not need a pointer to MainWindow in second window! A child should usually not know anything about its parent.
The connection should be done in MainWindow after the creation of the second window instance. -
@jsulm Ok, but when i try to do this I got error force closed when show second window
_secondwindow->showMaximized(); connect(mainW, SIGNAL(sendLabel()),_secondwindow,SLOT(changeDisplay())); emit sendLabel("400");
or
QObject::connect: No such signal MainWindow::sendLabel() in
-
@Eshya said in How To Connect Emitter From MainWindow To AnotherWindow:
I got error
Of course you get an error: please check the signature of your signal and what you have in your connect - they do not match!
And you also should use the new Qt5 connect syntax. -
@jsulm said in How To Connect Emitter From MainWindow To AnotherWindow:
Qt5 connect syntax.
Okay, thank you. Silly me because not yet updated
connect(this,&MainWindow::sendLabel,_secondwindow,&SecondWindow::changeDisplay);
I just need to change thic section and done. Thank you