Important: Please read the Qt Code of Conduct -

Connecting buttons of one page to label of another page

  • 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

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

        connect(formA->button, &QPushButton::clicked, this, &MainWindow::formAButtonClicked);
    void MainWindow::formAButtonClicked(bool checked /*= false*/)
        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.

  • 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

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

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

  • Lifetime Qt Champion

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

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

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

  • Lifetime Qt Champion

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

  • @jsulm Sorry
    Thank You

  • @Thank-You

    It's his signature :)

  • Lifetime Qt Champion

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

  • Wooow That's awesome

Log in to reply