Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Send signals to MainWindow from another window



  • Hi, everyone!
    I created second dialogue window from my MainWindow by

    SecondWindow *SW = new SecondWindow();
    SW->show();
    

    SecondWindow contains 6 QPushButtons inside. I'd like to be able to send 6 different clicked()signals from SecondWindow to MainWindow, so that each button has it's own signal. I guess, the best to do this is by connect(), but i still can't figure out how to implement it in code.



  • 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!!!!



  • 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);



  • Should i somehow connect to buttons? I followed your instructions but, unfortunately, when i press any of buttons, i get no response.



  • You have theses buttons on ui or declared in QMainWindow cpp?



  • @Lemat , I created these buttons on ui of SecondWindow and i want to connect their signals (if they were clicked) to slots in MainWindow



  • 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!!!!



  • @Lemat , how can i get MainWindow in connect (this->qpushbutton, SIGNAL(clicked (bool)), MainWindow, SLOT(specific_slot (bool)), Qt::DirectConnection);?
    if i use

    MainWindow *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.



  • Wow, my apologies!!!, i have not pay attention...



  • 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?


  • Lifetime Qt Champion

    @Peter_Dev Please post your code.
    Did you make sure connect() succeeded (it returns a bool)?



  • @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!



  • @Peter_Dev
    Glad it worked for you. it's a solution I use very often. if everything is ok, pass this subject to resolved.


Log in to reply