Changing the property's of widgets from another window [SOLVED]
-
wrote on 4 Jul 2015, 12:45 last edited by Bart 7 Aug 2015, 12:41
Hi,
I am new to c++ and Qt so maybe this is a stupid question but I can't figure it out. I made a widget application with two windows. I got a main window and a configuration window. Now I want to change the text of a button in my mainwindow when I press a button in my configuration window but I don't know how to. If anyone could provide some kind of help or tip it would be appreciated.
-
Hi, welcome to devnet.
Emit a signal from your dialog and connect it to a slot in your main window that will change the text.
Signlas/slots are one of the core concepts of Qt so it's important to grasp how they work and how to use them.
You can read a lot more about them and look at some examples here. -
wrote on 5 Jul 2015, 12:36 last edited by Bart 7 May 2015, 12:39
Hi thanks for the reply,
I tryed it but it didn't work. I got 2 classes: MainWindow & Configuration. This is my code:
Confgiuration.h
signals: void sendSignal();
Configuration.cpp
void Configuration::on_pushButton_clicked() { emit sendSignal(); }
MainWindow.h
private slots: void setVariable();
MainWindow.cpp
void MainWindow::setVariable() { ui->FrontSlot1->setText("frontslot1"); ui->FrontSlot1->setDisabled(false); } void MainWindow::setUpPage() { connect(Configuration, SIGNAL(sendSignal()), this, SLOT(setVariable())); }
And here is where I get the following error but I can't figure out why.
error: expected primary-expression before ',' token connect(Configuration, SIGNAL(sendSignal()), this, SLOT(setVariable()));
-
The first argument to
connect()
is a pointer to an instance that emits a signal, not a class name.
Pass there a pointer to theConfiguration
class instance that is your dialog. -
The first argument to
connect()
is a pointer to an instance that emits a signal, not a class name.
Pass there a pointer to theConfiguration
class instance that is your dialog.wrote on 5 Jul 2015, 13:28 last edited byThanks for replying,
I changed the code in my MainWindow.cpp to:
void MainWindow::setUpPage() { Configuration *configpointer = new Configuration; connect(configpointer, SIGNAL(setSlots()), this, SLOT(setSlots())); }
It compiles without any problem now but I don't see anything changing in the mainwindow.
-
Lifetime Qt Championwrote on 5 Jul 2015, 13:36 last edited by Chris Kawa 7 May 2015, 13:37
configpointer
is a local variable (that leaks memory by the way).
You should connect it to the actual instance of the dialog you use, not some temporary that never gets shown.
AlsosetSlots
is neither a signal in Configuration class nor a slot in MainWindow.Here's an example with both Configuration and MainWindow shown at sturtup:
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; Configuration cfg; connect(&cfg, &Configuration::sendSignal, &w, &MainWindow::setVariable); w.show(); cfg.show(); return a.exec(); }
If, on the other hand, you show your Configuration dialog in response to some button clicked in MainWindow here's how the code may look like:
void MainWindow::showConfigurationButtonClicked() { Configuration cfg; connect(&cfg, &Configuation::sendSignal, this, &MainWindow::setVariable); cfg.exec(); }
-
configpointer
is a local variable (that leaks memory by the way).
You should connect it to the actual instance of the dialog you use, not some temporary that never gets shown.
AlsosetSlots
is neither a signal in Configuration class nor a slot in MainWindow.Here's an example with both Configuration and MainWindow shown at sturtup:
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; Configuration cfg; connect(&cfg, &Configuration::sendSignal, &w, &MainWindow::setVariable); w.show(); cfg.show(); return a.exec(); }
If, on the other hand, you show your Configuration dialog in response to some button clicked in MainWindow here's how the code may look like:
void MainWindow::showConfigurationButtonClicked() { Configuration cfg; connect(&cfg, &Configuation::sendSignal, this, &MainWindow::setVariable); cfg.exec(); }
wrote on 5 Jul 2015, 14:00 last edited by Bart 7 May 2015, 14:00Sorry for being unclear but I don't think you understood my problem.
I got this code in my MainWindow.cpp now:
void MainWindow::on_Configuration_clicked() { Configuration Configurationob; Configurationob.setModal(true); Configurationob.exec(); } void MainWindow::setSlots(){ ui->FrontSlot1->setText("updated"); ui->FrontSlot1->setDisabled(false); ui->FrontSlot1->update(); } void MainWindow::setUpPage() { Configuration cfg; connect(&cfg, SIGNAL(sendSignal()), this, SLOT(setSlots())); }
I got a button in my mainwindow called "FrontSlot1" which is disabled by default. What I want is that when the user presses a button in the configuration window it will call the setSlots() function and execute the commands in it. I dont want to open a new window. I already did that with the void MainWindow::on_Configuration_clicked() function.
-
Lifetime Qt Championwrote on 5 Jul 2015, 14:16 last edited by Chris Kawa 7 May 2015, 14:17
I understood your problem. You didn't understood the answer, as it addresses exactly that ;)
Let me be more clear. Look at your code. In
on_Configuration_clicked
you create an instance of Configuration calledConfigurationob
and show it. InsetUpPage
method you create another, totally unrelated instance of that class calledcfg
and connect to it. That's not gonna do what you want.Configurationob
andcfg
are totally separate, unrelated pieces of data.The solution here is (similar to what I showed in the example) this:
void MainWindow::on_Configuration_clicked() { Configuration Configurationob; Configurationob.setModal(true); connect(&Configurationob, SIGNAL(sendSignal()), this, SLOT(setSlots())); //the important part Configurationob.exec(); } void MainWindow::setSlots(){ ui->FrontSlot1->setText("updated"); ui->FrontSlot1->setDisabled(false); ui->FrontSlot1->update(); }
-
I understood your problem. You didn't understood the answer, as it addresses exactly that ;)
Let me be more clear. Look at your code. In
on_Configuration_clicked
you create an instance of Configuration calledConfigurationob
and show it. InsetUpPage
method you create another, totally unrelated instance of that class calledcfg
and connect to it. That's not gonna do what you want.Configurationob
andcfg
are totally separate, unrelated pieces of data.The solution here is (similar to what I showed in the example) this:
void MainWindow::on_Configuration_clicked() { Configuration Configurationob; Configurationob.setModal(true); connect(&Configurationob, SIGNAL(sendSignal()), this, SLOT(setSlots())); //the important part Configurationob.exec(); } void MainWindow::setSlots(){ ui->FrontSlot1->setText("updated"); ui->FrontSlot1->setDisabled(false); ui->FrontSlot1->update(); }
wrote on 5 Jul 2015, 14:23 last edited byThank you very much!
9/9