Connecting buttons of one page to label of another page
-
wrote on 11 Nov 2020, 08:03 last edited by
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 -
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 mainwindowwrote on 11 Nov 2020, 08:19 last edited by JonB 11 Nov 2020, 08:21@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
inFormA
class so that it is accessible fromMainWindow
. (I did it directly by making the buttonpublic
forFormA::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, havingFormA
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 ofFormA
, it won't have to access itsbutton
directly. -
wrote on 11 Nov 2020, 08:30 last edited by Thank You 11 Nov 2020, 08:33
@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 -
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 youwrote on 11 Nov 2020, 08:49 last edited by JonB 11 Nov 2020, 08:55@Thank-You
No, we really do not want to start usingfriend
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
, plusQAction::trigger()
, within yourMainWindow
class. If you do that you won't have a different source file withQPushButton
s to connect to, and your problem will go away. -
@Thank-You
No, we really do not want to start usingfriend
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
, plusQAction::trigger()
, within yourMainWindow
class. If you do that you won't have a different source file withQPushButton
s to connect to, and your problem will go away.wrote on 11 Nov 2020, 09:21 last edited by@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.
-
@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.
@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-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).
wrote on 11 Nov 2020, 09:42 last edited by Thank You 11 Nov 2020, 09:43@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.. 😃 -
@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.
wrote on 11 Nov 2020, 09:44 last edited by JonB 11 Nov 2020, 09:45@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, ina.cpp
you connect the button click toemit()
that signal, and inmainwindow.cpp
youconnect(formA, &FormA::customSignal, this, &MainWindow::formACustomSlot)
. -
@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:
and you flagged me to Qt_code_of_conduct
I did not :-)
It is done automatically for all. -
@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. -
wrote on 11 Nov 2020, 10:13 last edited by
It's his signature :)
-
It's his signature :)
@Pl45m4 You're right! I didn't realise that actually. No idea why I have it as signature, can't remember setting it.
-
@Pl45m4 You're right! I didn't realise that actually. No idea why I have it as signature, can't remember setting it.
wrote on 11 Nov 2020, 12:56 last edited byWooow That's awesome
-
wrote on 12 Nov 2020, 08:40 last edited by
This is solved one. if anyone need the answer
https://forum.qt.io/topic/120765/connecting-buttons-with-custom-signals-and-slots/11
1/14