Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Send signals to MainWindow from another window

Send signals to MainWindow from another window

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 Posters 4.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Peter_Dev
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • LematL Offline
      LematL Offline
      Lemat
      wrote on last edited by Lemat
      #9

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

      Time to elevate.

      P 1 Reply Last reply
      2
      • Q Offline
        Q Offline
        qt.1234
        wrote on last edited by
        #2

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

        1 Reply Last reply
        3
        • P Offline
          P Offline
          Peter_Dev
          wrote on last edited by
          #3

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

          1 Reply Last reply
          0
          • LematL Offline
            LematL Offline
            Lemat
            wrote on last edited by
            #4

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

            Time to elevate.

            P 1 Reply Last reply
            0
            • LematL Lemat

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

              P Offline
              P Offline
              Peter_Dev
              wrote on last edited by
              #5

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

              1 Reply Last reply
              0
              • LematL Offline
                LematL Offline
                Lemat
                wrote on last edited by Lemat
                #6

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

                Time to elevate.

                P 1 Reply Last reply
                1
                • LematL 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!!!!

                  P Offline
                  P Offline
                  Peter_Dev
                  wrote on last edited by
                  #7

                  @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.

                  1 Reply Last reply
                  0
                  • LematL Offline
                    LematL Offline
                    Lemat
                    wrote on last edited by
                    #8

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

                    Time to elevate.

                    1 Reply Last reply
                    0
                    • LematL Offline
                      LematL Offline
                      Lemat
                      wrote on last edited by Lemat
                      #9

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

                      Time to elevate.

                      P 1 Reply Last reply
                      2
                      • LematL 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!!!!

                        P Offline
                        P Offline
                        Peter_Dev
                        wrote on last edited by
                        #10

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

                        jsulmJ LematL 2 Replies Last reply
                        0
                        • P Peter_Dev

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

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #11

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

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • P Peter_Dev

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

                            LematL Offline
                            LematL Offline
                            Lemat
                            wrote on last edited by Lemat
                            #12

                            @Peter_Dev , hello please post your code...

                            Time to elevate.

                            P 1 Reply Last reply
                            0
                            • LematL Lemat

                              @Peter_Dev , hello please post your code...

                              P Offline
                              P Offline
                              Peter_Dev
                              wrote on last edited by
                              #13

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

                              LematL 1 Reply Last reply
                              0
                              • P Peter_Dev

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

                                LematL Offline
                                LematL Offline
                                Lemat
                                wrote on last edited by
                                #14

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

                                Time to elevate.

                                1 Reply Last reply
                                1

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved