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
Forum Updated to NodeBB v4.3 + New Features

Send signals to MainWindow from another window

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 Posters 3.8k 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 16 Dec 2019, 22:32 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
    • L Offline
      L Offline
      Lemat
      wrote on 17 Dec 2019, 22:57 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 17 Dec 2019, 23:54
      2
      • Q Offline
        Q Offline
        qt.1234
        wrote on 17 Dec 2019, 02:18 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 17 Dec 2019, 16:20 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
          • L Offline
            L Offline
            Lemat
            wrote on 17 Dec 2019, 20:41 last edited by
            #4

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

            Time to elevate.

            P 1 Reply Last reply 17 Dec 2019, 20:50
            0
            • L Lemat
              17 Dec 2019, 20:41

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

              P Offline
              P Offline
              Peter_Dev
              wrote on 17 Dec 2019, 20:50 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
              • L Offline
                L Offline
                Lemat
                wrote on 17 Dec 2019, 21:20 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 17 Dec 2019, 21:44
                1
                • L Lemat
                  17 Dec 2019, 21:20

                  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 17 Dec 2019, 21:44 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
                  • L Offline
                    L Offline
                    Lemat
                    wrote on 17 Dec 2019, 21:50 last edited by
                    #8

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

                    Time to elevate.

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      Lemat
                      wrote on 17 Dec 2019, 22:57 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 17 Dec 2019, 23:54
                      2
                      • L Lemat
                        17 Dec 2019, 22:57

                        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 17 Dec 2019, 23:54 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 L 2 Replies Last reply 18 Dec 2019, 06:34
                        0
                        • P Peter_Dev
                          17 Dec 2019, 23:54

                          @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 18 Dec 2019, 06:34 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
                            17 Dec 2019, 23:54

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

                            L Offline
                            L Offline
                            Lemat
                            wrote on 18 Dec 2019, 07:54 last edited by Lemat
                            #12

                            @Peter_Dev , hello please post your code...

                            Time to elevate.

                            P 1 Reply Last reply 18 Dec 2019, 14:46
                            0
                            • L Lemat
                              18 Dec 2019, 07:54

                              @Peter_Dev , hello please post your code...

                              P Offline
                              P Offline
                              Peter_Dev
                              wrote on 18 Dec 2019, 14:46 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!

                              L 1 Reply Last reply 18 Dec 2019, 15:17
                              0
                              • P Peter_Dev
                                18 Dec 2019, 14:46

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

                                L Offline
                                L Offline
                                Lemat
                                wrote on 18 Dec 2019, 15:17 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

                                1/14

                                16 Dec 2019, 22:32

                                • Login

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