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

Connecting buttons of one page to label of another page

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 4 Posters 2.5k Views 2 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.
  • Thank YouT Thank You

    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

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #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
    • Thank YouT Offline
      Thank YouT Offline
      Thank You
      wrote on last edited by Thank You
      #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

      JonBJ 1 Reply Last reply
      0
      • Thank YouT Thank You

        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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #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.

        Thank YouT 1 Reply Last reply
        2
        • JonBJ JonB

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

          Thank YouT Offline
          Thank YouT Offline
          Thank You
          wrote on 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

          jsulmJ JonBJ 2 Replies Last reply
          0
          • Thank YouT Thank You

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

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on 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

            Thank YouT 1 Reply Last reply
            4
            • jsulmJ jsulm

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

              Thank YouT Offline
              Thank YouT Offline
              Thank You
              wrote on last edited by Thank You
              #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

              jsulmJ 1 Reply Last reply
              0
              • Thank YouT Thank You

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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #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
                • Thank YouT Thank You

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

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 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

                  Thank YouT 1 Reply Last reply
                  1
                  • jsulmJ jsulm

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

                    Thank YouT Offline
                    Thank YouT Offline
                    Thank You
                    wrote on 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
                    0
                    • Thank YouT Thank You

                      @jsulm Sorry
                      Thank You

                      Pl45m4P Offline
                      Pl45m4P Offline
                      Pl45m4
                      wrote on 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

                      jsulmJ 1 Reply Last reply
                      1
                      • Pl45m4P Pl45m4

                        @Thank-You

                        It's his signature :)

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 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

                        Thank YouT 1 Reply Last reply
                        0
                        • jsulmJ jsulm

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

                          Thank YouT Offline
                          Thank YouT Offline
                          Thank You
                          wrote on 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
                          • Thank YouT Offline
                            Thank YouT Offline
                            Thank You
                            wrote on 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

                            • Login

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