Send signals to MainWindow from another window
-
wrote on 16 Dec 2019, 22:32 last edited by
Hi, everyone!
I created second dialogue window from my MainWindow bySecondWindow *SW = new SecondWindow(); SW->show();
SecondWindow contains 6
QPushButtons
inside. I'd like to be able to send 6 differentclicked()
signals from SecondWindow to MainWindow, so that each button has it's own signal. I guess, the best to do this is byconnect()
, but i still can't figure out how to implement it in code. -
wrote on 17 Dec 2019, 22:57 last edited by Lemat
on the secondWindow, create signal in header for each button. then when you will clicked on a button, the on_clicked slot you implemented from ui for each button would emits the specific signal.
Like this:SecondWindow.h :
signals: Void button0 (); Void button1 (); //same for 4 remaining buttons
SecondWindow.cpp : (after create slots from ui)
Void on_button0_clicked () { emit button0(); } //repeat for 5 others buttons
MainWindow cpp:
SecondWindow *SW = new SecondWindow(); connect (SW, SIGNAL (button0()), this, SLOT(desired_slot ())); //repeat for 5 others signals SW->show();
Test it!!!!
-
wrote on 17 Dec 2019, 02:18 last edited by
do the connection in your MainWindow.
declare signal clicked() in your SecondWindow.h
and declare slot buttonClickSlot() in your MainWindow.cpp and MainWindow.h
QObject::connect(SW, SIGNAL(clicked()), this, SLOT(buttonClickSlot()), Qt::AutoConnection); -
wrote on 17 Dec 2019, 16:20 last edited by
Should i somehow connect to buttons? I followed your instructions but, unfortunately, when i press any of buttons, i get no response.
-
wrote on 17 Dec 2019, 20:41 last edited by
You have theses buttons on ui or declared in QMainWindow cpp?
-
wrote on 17 Dec 2019, 21:20 last edited by Lemat
From my knowledge, declare the specifics slots in MainWindow as public, and instantiate MainWindow in SecondWindow. After try this:
connect (this->qpushbutton, SIGNAL(clicked (bool)), MainWindow, SLOT(specific_slot (bool)), Qt::DirectConnection); this refer to SecondWindow
Hope it will work for you, test it!!!!
-
From my knowledge, declare the specifics slots in MainWindow as public, and instantiate MainWindow in SecondWindow. After try this:
connect (this->qpushbutton, SIGNAL(clicked (bool)), MainWindow, SLOT(specific_slot (bool)), Qt::DirectConnection); this refer to SecondWindow
Hope it will work for you, test it!!!!
wrote on 17 Dec 2019, 21:44 last edited by@Lemat , how can i get MainWindow in
connect (this->qpushbutton, SIGNAL(clicked (bool)), MainWindow, SLOT(specific_slot (bool)), Qt::DirectConnection);
?
if i useMainWindow *win = new MainWindoow();
i guess there's a endless cycle creates because i open a SecondWindow from MainWindow and i don't see any window. Program just uses my processor.
-
wrote on 17 Dec 2019, 21:50 last edited by
Wow, my apologies!!!, i have not pay attention...
-
wrote on 17 Dec 2019, 22:57 last edited by Lemat
on the secondWindow, create signal in header for each button. then when you will clicked on a button, the on_clicked slot you implemented from ui for each button would emits the specific signal.
Like this:SecondWindow.h :
signals: Void button0 (); Void button1 (); //same for 4 remaining buttons
SecondWindow.cpp : (after create slots from ui)
Void on_button0_clicked () { emit button0(); } //repeat for 5 others buttons
MainWindow cpp:
SecondWindow *SW = new SecondWindow(); connect (SW, SIGNAL (button0()), this, SLOT(desired_slot ())); //repeat for 5 others signals SW->show();
Test it!!!!
-
on the secondWindow, create signal in header for each button. then when you will clicked on a button, the on_clicked slot you implemented from ui for each button would emits the specific signal.
Like this:SecondWindow.h :
signals: Void button0 (); Void button1 (); //same for 4 remaining buttons
SecondWindow.cpp : (after create slots from ui)
Void on_button0_clicked () { emit button0(); } //repeat for 5 others buttons
MainWindow cpp:
SecondWindow *SW = new SecondWindow(); connect (SW, SIGNAL (button0()), this, SLOT(desired_slot ())); //repeat for 5 others signals SW->show();
Test it!!!!
-
@Lemat , thanks a lot for your time. I followed your instructions, but still i don't receive a signal from SecondWindow, though i get no error message. Does your code work for you?
@Peter_Dev Please post your code.
Did you make sure connect() succeeded (it returns a bool)? -
@Lemat , thanks a lot for your time. I followed your instructions, but still i don't receive a signal from SecondWindow, though i get no error message. Does your code work for you?
wrote on 18 Dec 2019, 07:54 last edited by Lemat@Peter_Dev , hello please post your code...
-
@Peter_Dev , hello please post your code...
-
@Lemat, my code is very mess. I'm trying to clean it now. I think i know where i made mistake..
Thanks a lot for your time. I tried your solution on another project and it works perfect!wrote on 18 Dec 2019, 15:17 last edited by@Peter_Dev
Glad it worked for you. it's a solution I use very often. if everything is ok, pass this subject to resolved.
1/14