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. Connecting buttons of one page to label of another page

Connecting buttons of one page to label of another page

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 4 Posters 2.3k Views
  • 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.
  • T Offline
    T Offline
    Thank You
    wrote on 11 Nov 2020, 08:03 last edited by
    #1

    Re: Multiple Windows Desktop application
    I want to connect button from one form to another form.
    I have a button in one form named "a.h". In the mainwindow I have created widget which is promoted to "a.h". How can I connect the signal of button in a.h to the label in mainwindow

    Let's make QT free or It will go forever

    TRUE AND FALSE <3

    J 1 Reply Last reply 11 Nov 2020, 08:19
    0
    • T Thank You
      11 Nov 2020, 08:03

      Re: Multiple Windows Desktop application
      I want to connect button from one form to another form.
      I have a button in one form named "a.h". In the mainwindow I have created widget which is promoted to "a.h". How can I connect the signal of button in a.h to the label in mainwindow

      J Offline
      J Offline
      JonB
      wrote on 11 Nov 2020, 08:19 last edited by JonB 11 Nov 2020, 08:21
      #2

      @Thank-You
      Much like it shows in the link you reference.

      MainWindow::MainWindow()
      {
          connect(formA->button, &QPushButton::clicked, this, &MainWindow::formAButtonClicked);
      }
      
      void MainWindow::formAButtonClicked(bool checked /*= false*/)
      {
          Q_UNUSED(checked);
          label->setText("Form A button was clicked");
      }
      

      Having said this, you will have to "expose" button in FormA class so that it is accessible from MainWindow. (I did it directly by making the button public for FormA::button, you could instead wrap that into a public method returning the button which is a bit nicer.) This is not ideal, but doable. Over time you might want to change that to, say, having FormA raise a custom signal when its button is clicked which you slot onto: that way the main window does not have to know about the internals of FormA, it won't have to access its button directly.

      1 Reply Last reply
      1
      • T Offline
        T Offline
        Thank You
        wrote on 11 Nov 2020, 08:30 last edited by Thank You 11 Nov 2020, 08:33
        #3

        b6d33ef0-3d93-4eee-b6e0-7be5991f3b6e-image.png image url)

        @JonB This is what i want to create.
        The navigation is promoted to "navigation.h" where navigation is made. When I click on any button I want to change the text of label in the right side of the form.

        I understood what you said but I am beginner at Qt and I dont know how to make the button public and assign ui->pushbutton in the "navigation.h"

        Can we do it with friend function?????
        Thank you

        Let's make QT free or It will go forever

        TRUE AND FALSE <3

        J 1 Reply Last reply 11 Nov 2020, 08:49
        0
        • T Thank You
          11 Nov 2020, 08:30

          b6d33ef0-3d93-4eee-b6e0-7be5991f3b6e-image.png image url)

          @JonB This is what i want to create.
          The navigation is promoted to "navigation.h" where navigation is made. When I click on any button I want to change the text of label in the right side of the form.

          I understood what you said but I am beginner at Qt and I dont know how to make the button public and assign ui->pushbutton in the "navigation.h"

          Can we do it with friend function?????
          Thank you

          J Offline
          J Offline
          JonB
          wrote on 11 Nov 2020, 08:49 last edited by JonB 11 Nov 2020, 08:55
          #4

          @Thank-You
          No, we really do not want to start using friend here! At least I don't think so.

          If you really want to do it this way: Since you are using a designer-generated ui->pushbutton it won't be public. You might do something like:

          // In a.h
          public:
              QPushButton *aButton() { return ui->aButton; }
          
          // In mainwindow.cpp
          connect(formA->aButton(), &QPushButton::clicked, this, &MainWindow::formAButtonClicked);
          

          Though, as I said earlier, I would actually raise a custom signal from FormA instead. Which I could show you, but it may not be necessary because...

          ...Having said this: your picture looks like you should be using some kind of QMenu, plus QAction::trigger(), within your MainWindow class. If you do that you won't have a different source file with QPushButtons to connect to, and your problem will go away.

          T 1 Reply Last reply 11 Nov 2020, 09:21
          2
          • J JonB
            11 Nov 2020, 08:49

            @Thank-You
            No, we really do not want to start using friend here! At least I don't think so.

            If you really want to do it this way: Since you are using a designer-generated ui->pushbutton it won't be public. You might do something like:

            // In a.h
            public:
                QPushButton *aButton() { return ui->aButton; }
            
            // In mainwindow.cpp
            connect(formA->aButton(), &QPushButton::clicked, this, &MainWindow::formAButtonClicked);
            

            Though, as I said earlier, I would actually raise a custom signal from FormA instead. Which I could show you, but it may not be necessary because...

            ...Having said this: your picture looks like you should be using some kind of QMenu, plus QAction::trigger(), within your MainWindow class. If you do that you won't have a different source file with QPushButtons to connect to, and your problem will go away.

            T Offline
            T Offline
            Thank You
            wrote on 11 Nov 2020, 09:21 last edited by
            #5

            @JonB Ya I actually did this But It is not working
            No I am not using any QMenu. I am using QWidget which is just promoted to 'a.h'

               formA  n;
                connect(n.homeButton() , &QPushButton::clicked,this,&mainwindow::connect_home);
            
            // and in  a.h
            QPushButton *homeButton();
            //in a.cpp
            QPushButton *navigation::homeButton()
            {
                return ui->home;
            }
            

            I just want to connect these two things. Is it good to use custom signal here.

            Let's make QT free or It will go forever

            TRUE AND FALSE <3

            J J 2 Replies Last reply 11 Nov 2020, 09:27
            0
            • T Thank You
              11 Nov 2020, 09:21

              @JonB Ya I actually did this But It is not working
              No I am not using any QMenu. I am using QWidget which is just promoted to 'a.h'

                 formA  n;
                  connect(n.homeButton() , &QPushButton::clicked,this,&mainwindow::connect_home);
              
              // and in  a.h
              QPushButton *homeButton();
              //in a.cpp
              QPushButton *navigation::homeButton()
              {
                  return ui->home;
              }
              

              I just want to connect these two things. Is it good to use custom signal here.

              J Online
              J Online
              jsulm
              Lifetime Qt Champion
              wrote on 11 Nov 2020, 09:27 last edited by
              #6

              @Thank-You said in Connecting buttons of one page to label of another page:

              Is it good to use custom signal here

              Yes, you can use a custom signal here to hide the internal widgets from the outside world (tip: you can connect a signal to another signal).

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

              T 1 Reply Last reply 11 Nov 2020, 09:42
              4
              • J jsulm
                11 Nov 2020, 09:27

                @Thank-You said in Connecting buttons of one page to label of another page:

                Is it good to use custom signal here

                Yes, you can use a custom signal here to hide the internal widgets from the outside world (tip: you can connect a signal to another signal).

                T Offline
                T Offline
                Thank You
                wrote on 11 Nov 2020, 09:42 last edited by Thank You 11 Nov 2020, 09:43
                #7

                @jsulm Thank you for your answer.
                If your are ok please say me what I did wrong and you flagged me to Qt_code_of_conduct. Please say me so that I wont make mistake that like in future posts.
                I read all the code of conducts there but I don't think I have done mistakes.
                Thank you for suggesting me.. 😃

                Let's make QT free or It will go forever

                TRUE AND FALSE <3

                J 1 Reply Last reply 11 Nov 2020, 09:45
                0
                • T Thank You
                  11 Nov 2020, 09:21

                  @JonB Ya I actually did this But It is not working
                  No I am not using any QMenu. I am using QWidget which is just promoted to 'a.h'

                     formA  n;
                      connect(n.homeButton() , &QPushButton::clicked,this,&mainwindow::connect_home);
                  
                  // and in  a.h
                  QPushButton *homeButton();
                  //in a.cpp
                  QPushButton *navigation::homeButton()
                  {
                      return ui->home;
                  }
                  

                  I just want to connect these two things. Is it good to use custom signal here.

                  J Offline
                  J Offline
                  JonB
                  wrote on 11 Nov 2020, 09:44 last edited by JonB 11 Nov 2020, 09:45
                  #8

                  @Thank-You said in Connecting buttons of one page to label of another page:

                  Ya I actually did this But It is not working

                  I don't know why it's not working, it should from what I can see (assuming it compiles in your code, and you mean it doesn't work at runtime).

                  I think it's a shame you don't want to change over to main window having a QMenu and doing it that way, avoiding all this issue. Up to you.

                  Then as I & @jsulm have said, it would be nicer if in a.h you declare a custom signal, in a.cpp you connect the button click to emit() that signal, and in mainwindow.cpp you connect(formA, &FormA::customSignal, this, &MainWindow::formACustomSlot).

                  1 Reply Last reply
                  1
                  • T Thank You
                    11 Nov 2020, 09:42

                    @jsulm Thank you for your answer.
                    If your are ok please say me what I did wrong and you flagged me to Qt_code_of_conduct. Please say me so that I wont make mistake that like in future posts.
                    I read all the code of conducts there but I don't think I have done mistakes.
                    Thank you for suggesting me.. 😃

                    J Online
                    J Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on 11 Nov 2020, 09:45 last edited by
                    #9

                    @Thank-You said in Connecting buttons of one page to label of another page:

                    and you flagged me to Qt_code_of_conduct

                    I did not :-)
                    It is done automatically for all.

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

                    T 1 Reply Last reply 11 Nov 2020, 10:02
                    1
                    • J jsulm
                      11 Nov 2020, 09:45

                      @Thank-You said in Connecting buttons of one page to label of another page:

                      and you flagged me to Qt_code_of_conduct

                      I did not :-)
                      It is done automatically for all.

                      T Offline
                      T Offline
                      Thank You
                      wrote on 11 Nov 2020, 10:02 last edited by
                      #10

                      @jsulm Sorry
                      Thank You

                      Let's make QT free or It will go forever

                      TRUE AND FALSE <3

                      Pl45m4P 1 Reply Last reply 11 Nov 2020, 10:13
                      0
                      • T Thank You
                        11 Nov 2020, 10:02

                        @jsulm Sorry
                        Thank You

                        Pl45m4P Offline
                        Pl45m4P Offline
                        Pl45m4
                        wrote on 11 Nov 2020, 10:13 last edited by
                        #11

                        @Thank-You

                        It's his signature :)


                        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                        ~E. W. Dijkstra

                        J 1 Reply Last reply 11 Nov 2020, 10:15
                        1
                        • Pl45m4P Pl45m4
                          11 Nov 2020, 10:13

                          @Thank-You

                          It's his signature :)

                          J Online
                          J Online
                          jsulm
                          Lifetime Qt Champion
                          wrote on 11 Nov 2020, 10:15 last edited by
                          #12

                          @Pl45m4 You're right! I didn't realise that actually. No idea why I have it as signature, can't remember setting it.

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

                          T 1 Reply Last reply 11 Nov 2020, 12:56
                          0
                          • J jsulm
                            11 Nov 2020, 10:15

                            @Pl45m4 You're right! I didn't realise that actually. No idea why I have it as signature, can't remember setting it.

                            T Offline
                            T Offline
                            Thank You
                            wrote on 11 Nov 2020, 12:56 last edited by
                            #13

                            Wooow That's awesome

                            Let's make QT free or It will go forever

                            TRUE AND FALSE <3

                            1 Reply Last reply
                            0
                            • T Offline
                              T Offline
                              Thank You
                              wrote on 12 Nov 2020, 08:40 last edited by
                              #14

                              This is solved one. if anyone need the answer

                              https://forum.qt.io/topic/120765/connecting-buttons-with-custom-signals-and-slots/11

                              Let's make QT free or It will go forever

                              TRUE AND FALSE <3

                              1 Reply Last reply
                              0

                              1/14

                              11 Nov 2020, 08:03

                              • 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