Change Label on MainWindow not working
-
Hello guys,
I am running out of ideas with my problem. I have a simple gui that creates a simple test form, on that test form I want to press a button and change a label on the MainWindow. Please look at my code:
Testform.cpp
#include "testform.h" #include "ui_testform.h" TestForm::TestForm(QWidget *parent) : QWidget(parent), ui(new Ui::TestForm) { ui->setupUi(this); this->mw = new MainWindow(); connect(ui->pB_ChangeLabelOnMainForm, &QPushButton::clicked, this->mw, &MainWindow::changeLabelOnUi); } TestForm::~TestForm() { delete ui; }
Testform.h
#ifndef TESTFORM_H #define TESTFORM_H #include "mainwindow.h" #include <QWidget> namespace Ui { class TestForm; } class TestForm : public QWidget { Q_OBJECT public: explicit TestForm(QWidget *parent = nullptr); ~TestForm(); private: Ui::TestForm *ui; MainWindow *mw; }; #endif // TESTFORM_H
mainwindow.cpp
#include "mainwindow.h" #include "testform.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pB_OpenDialog_clicked() { TestForm *tf = new TestForm(); tf->show(); } //+++ HERE IS MY PROBLEM+++ void MainWindow::changeLabelOnUi() { this->ui->lblToChangeOnMF->setText("Hello"); // NOT WORKING qDebug() << "TESTOUTPUT"; // WORKING }
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_pB_OpenDialog_clicked(); public slots: void changeLabelOnUi(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
Can someone tell me why the label-change-code in the function changeLabelOnUi() in the mainwindow.cpp is not working but the qDebug() line does? When I call this function for example in the constructor, the label has changed.
I have tried another example in that the MainForm is the sender and I have changed a label on the TestForm, here was everything fine.
Thank you! :-)
-
Hello guys,
I am running out of ideas with my problem. I have a simple gui that creates a simple test form, on that test form I want to press a button and change a label on the MainWindow. Please look at my code:
Testform.cpp
#include "testform.h" #include "ui_testform.h" TestForm::TestForm(QWidget *parent) : QWidget(parent), ui(new Ui::TestForm) { ui->setupUi(this); this->mw = new MainWindow(); connect(ui->pB_ChangeLabelOnMainForm, &QPushButton::clicked, this->mw, &MainWindow::changeLabelOnUi); } TestForm::~TestForm() { delete ui; }
Testform.h
#ifndef TESTFORM_H #define TESTFORM_H #include "mainwindow.h" #include <QWidget> namespace Ui { class TestForm; } class TestForm : public QWidget { Q_OBJECT public: explicit TestForm(QWidget *parent = nullptr); ~TestForm(); private: Ui::TestForm *ui; MainWindow *mw; }; #endif // TESTFORM_H
mainwindow.cpp
#include "mainwindow.h" #include "testform.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pB_OpenDialog_clicked() { TestForm *tf = new TestForm(); tf->show(); } //+++ HERE IS MY PROBLEM+++ void MainWindow::changeLabelOnUi() { this->ui->lblToChangeOnMF->setText("Hello"); // NOT WORKING qDebug() << "TESTOUTPUT"; // WORKING }
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_pB_OpenDialog_clicked(); public slots: void changeLabelOnUi(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
Can someone tell me why the label-change-code in the function changeLabelOnUi() in the mainwindow.cpp is not working but the qDebug() line does? When I call this function for example in the constructor, the label has changed.
I have tried another example in that the MainForm is the sender and I have changed a label on the TestForm, here was everything fine.
Thank you! :-)
@buzz_lightzyear
I guess the windows are not the same.void MainWindow::on_pB_OpenDialog_clicked() { TestForm *tf = new TestForm(); tf->show(); }
Here you create a TestForm that in turn create a new Window , strange !
-
omg... thank you. I think I now see the problem... I was wrong all the time. And the qDebug() - Output comes from that other form, but nothing changes on my main form.
But how do I get to "that" Main Form I need? With a Singleton? Or are there other ways?
Thank you in advance!
-
omg... thank you. I think I now see the problem... I was wrong all the time. And the qDebug() - Output comes from that other form, but nothing changes on my main form.
But how do I get to "that" Main Form I need? With a Singleton? Or are there other ways?
Thank you in advance!
@buzz_lightzyear said in Change Label on MainWindow not working:
But how do I get to "that" Main Form I need? With a Singleton? Or are there other ways?
Explain what you want to do exactly.
-
The code exampe is - of course - only an abstract demonstration of my problem. In that program I want to develop I want to open a dialog window, enter some data, with the ok-Button the data is stored into a database, on the cancel button nothing happens. And: With the click on the ok-button the list view on the main window should get updated.
Now I do that with a update button, but for the next version of my program I want to remove that button and the update of the list view should happen automatically.
-
omfg... it was that simple O_O
Thank you very much!!!! :-)