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. Change Label on MainWindow not working
Qt 6.11 is out! See what's new in the release blog

Change Label on MainWindow not working

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 1.1k Views 1 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.
  • B Offline
    B Offline
    buzz_lightzyear
    wrote on last edited by
    #1

    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! :-)

    M 1 Reply Last reply
    0
    • B buzz_lightzyear

      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! :-)

      M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

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

      1 Reply Last reply
      1
      • B Offline
        B Offline
        buzz_lightzyear
        wrote on last edited by
        #3

        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!

        M 1 Reply Last reply
        1
        • B buzz_lightzyear

          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!

          M Offline
          M Offline
          mpergand
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • B Offline
            B Offline
            buzz_lightzyear
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mpergand
              wrote on last edited by
              #6

              The easy way is to use modal dialog:

              void MainWindow::on_pB_OpenDialog_clicked()
              {
                  TestForm tf = TestForm();
                  if(tf->exec()==QDialog::Accepted)
                      {
                      // update data here
                      }
              }
              
              1 Reply Last reply
              1
              • B Offline
                B Offline
                buzz_lightzyear
                wrote on last edited by
                #7

                omfg... it was that simple O_O

                Thank you very much!!!! :-)

                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