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. [SOLVED] QLabel in main window is not updated/refreshed when modified in second window (QDialog)
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QLabel in main window is not updated/refreshed when modified in second window (QDialog)

Scheduled Pinned Locked Moved General and Desktop
18 Posts 6 Posters 7.4k 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.
  • JeroentjehomeJ Offline
    JeroentjehomeJ Offline
    Jeroentjehome
    wrote on last edited by
    #2

    Hi, First of all, welcome!! Qt is really a great C++ based programming environment/framework/library (what ever you want to call it).

    In basic you have a C++ problem. You are not able to connect anything to this:
    @
    void MainWindow::on_pushButton_OpenWindow_clicked()
    {
    //this->close ();
    /Cheating: Closes the old (first) mainwindow which does not update the label text, to avoid having two mainwindows (one working, one not)/

    ExternalDialog *mDialog;
    mDialog=new ExternalDialog (this);
    
    mDialog->show();
    

    }
    @
    The mDialog is created on the HEAP (that is the correct approach! Keep it that way), but you do not store the ExternalDialog anywhere. So how is anyone connected to that??
    The solution is to place a member variable of type ExternalDialog in your mainWindow class, create it in the constructor (or set a singleShot timer and do so in a seperate slot to speed up start up of the program). Then when the dialog is created, connect signals/slots from MainWindow to the Dialog. To make sure the dialog is not shown, call Dialog->hide() on it.
    Then when needed, set the Dialog->exec() and all should work.

    Greetz, Jeroen

    1 Reply Last reply
    0
    • P Offline
      P Offline
      pepita
      wrote on last edited by
      #3

      Hi Jeroen;

      Thanks for the welcome, and for the quick response.
      I think I'm following you.
      But, and not having it tried yet... Won't the Dialog window flash (open and close quickly) on start-up?
      Or can I create it and set it up without showing and then hidding?

      Sorry if I am misunderstanding something...
      I will try your suggestions now, and let you know my progress.

      Thanks again.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pepita
        wrote on last edited by
        #4

        Hi again,

        This is what I've done so far, without any success, and getting a segfault crash when clicking the open-window-button (and therefore, getting desperate and angry with myself)

        As I know I have a lack of clear concepts concerning c++ and classes, I ask you to be patient with me ;)

        Here go my modifications in the mainwindow.h and mainwindow.cpp files (the other three hasn't change):

        mainwindow.h:
        @
        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H

        #include <QMainWindow>
        #include "externaldialog.h"

        namespace Ui
        {
        class MainWindow;
        }

        class MainWindow : public QMainWindow
        {
        Q_OBJECT

        public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();

        void setupExternalDialog (ExternalDialog *externaldialog)
        { mDialog = externaldialog; }
        

        signals:
        void send_fromMain (const QString);

        private slots:
        void on_pushButton_OpenWindow_clicked();
        void on_pushButton_SetText_clicked();

        void rcv_ext2Main (const QString &extText);
        void rcv_main2Main (const QString &mainText);
        

        private:
        Ui::MainWindow *ui;
        ExternalDialog *mDialog;
        };

        #endif // MAINWINDOW_H
        @

        mainwindow.cpp:
        @
        #include <QDebug>

        #include "ui_mainwindow.h"
        #include "externaldialog.h"
        #include "mainwindow.h"

        MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent), ui(new Ui::MainWindow)
        {
        setupExternalDialog (mDialog);
        ui->setupUi(this);

        QObject::connect (this, SIGNAL (send_fromMain (QString)), this, SLOT(rcv_main2Main(QString)));
        
        qDebug () << "mainwindow.cpp";
        

        }

        MainWindow::~MainWindow()
        {
        delete ui;
        }

        void MainWindow::on_pushButton_OpenWindow_clicked()
        {
        //ExternalDialog *mDialog;
        //mDialog=new ExternalDialog (this);
        mDialog->exec();
        }

        void MainWindow::on_pushButton_SetText_clicked()
        {
        emit send_fromMain (ui->lineEdit_MainInput->text ());
        qDebug () << "Sent from mainwindow";
        }

        void MainWindow::rcv_ext2Main (const QString &extText)
        {
        qDebug () << "Received from externaldialog: " + extText;

        ui->label_ShowText->setText(extText);
        

        //HERE IS THE PROBLEM: label_ShowText from mainwindow does not update its text
        }

        void MainWindow:: rcv_main2Main (const QString &mainText)
        {
        ui->label_ShowText->setText(mainText);
        qDebug () << "Received from mainwindow: " + mainText;
        }
        @

        As a good point, must be said that I learn fast, so once I get this solved, it will take time to bother you again ;)

        Thanks for the help;

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #5

          Hi and welcome to devnet,

          This time your dialog doesn't exists, that's why it crashes.

          @
          setupExternalDialog (mDialog); << You are just assigning mDialog to itself
          @

          @
          mDialog = new ExternalDialog(this); << There you are constructing it
          @

          Then just connect mDialog and you should be good to go.

          I'd recommend reading the tutorials and examples from Qt's documentation. You'll learn a lot from them.

          Hope it helps

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • P Offline
            P Offline
            pepita
            wrote on last edited by
            #6

            Hi SGaist;

            Thank you for your time.

            I have read a lot of Qt documentation, in the Qt project web and googleing, and also learned a lot from other threads in this forum and in others. I've also followed some video tutorials...

            But because of this tutorials, since I started, I've always been creating the QDialog in the on_pushButton_clicked() function, not in the constructor of the class. This was my first error. The next one was to connect the main window and the dialog in the constructor of the second, instead of in the constructor of the main window.

            So finally everything is working thanks to your tips guys, so grateful.

            This is the mainwindow.cpp with the changes you two have advised (is compiling and working as desired):

            @
            #include <QDebug>

            #include "ui_mainwindow.h"
            #include "externaldialog.h"
            #include "mainwindow.h"

            MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent), ui(new Ui::MainWindow)
            {
            ui->setupUi(this);
            extDialog = new ExternalDialog (this);

            QObject::connect (this, SIGNAL (send_fromMain (QString)), this, SLOT(rcv_main2Main(QString)));
            
            QObject::connect (extDialog, SIGNAL (send_fromExt (QString)), this, SLOT(rcv_ext2Main(QString)));
            
            qDebug () << "mainwindow.cpp";
            

            }

            MainWindow::~MainWindow()
            {
            delete ui;
            }

            void MainWindow::on_pushButton_OpenWindow_clicked()
            {
            extDialog->show();
            }

            void MainWindow::on_pushButton_SetText_clicked()
            {
            emit send_fromMain (ui->lineEdit_MainInput->text ());
            qDebug () << "Sent from mainwindow";
            }

            void MainWindow::rcv_ext2Main (const QString &extText)
            {
            ui->label_ShowText->setText(extText);
            qDebug () << "Received from externaldialog: " + extText;
            }

            void MainWindow:: rcv_main2Main (const QString &mainText)
            {
            ui->label_ShowText->setText(mainText);
            qDebug () << "Received from mainwindow: " + mainText;
            }
            @

            I'll keep learning from Qt docs and from all of you in the forums.

            Thanks again both for the help.

            Best regards.

            PS: Just for curiosity, as you've seen, I'm retrieving information with QDebug just to know where the programm is in every step, and I get the following in the console:

            "Received from externaldialog: 88"
            Sent from externaldialog

            So, is it entering first in the SLOT and then in the SIGNAL?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #7

              Since you're running this in only one thread, the connection between signals and slots is direct. Here is a very simplified version of what happens when on_pushButton_SetText_clicked is called:

              emit send_fromMain

              ui->label_ShowText->setText(mainText);

              qDebug () << "Received from mainwindow: " + mainText;

              qDebug () << "Sent from main window";

              Hope it helps

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • N Offline
                N Offline
                Npsad
                wrote on last edited by
                #8

                Hey anyone help me out!!
                Say there is main window and dialog window after dialogue window execution main window having tablewidget needs to be get updated. Here in my case the problem is it is creating one more new main window with updated data and old main window still running in background.......

                jsulmJ 1 Reply Last reply
                0
                • N Npsad

                  Hey anyone help me out!!
                  Say there is main window and dialog window after dialogue window execution main window having tablewidget needs to be get updated. Here in my case the problem is it is creating one more new main window with updated data and old main window still running in background.......

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  @Npsad Well, why do you create a new main window instead of updating the existing one?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  N 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @Npsad Well, why do you create a new main window instead of updating the existing one?

                    N Offline
                    N Offline
                    Npsad
                    wrote on last edited by
                    #10

                    @jsulm well that's what I want to update old main window itself how to update it instead of updating it in new main window.

                    jsulmJ 1 Reply Last reply
                    0
                    • N Npsad

                      @jsulm well that's what I want to update old main window itself how to update it instead of updating it in new main window.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @Npsad Sorry, but as long as you do not show your code nobody will be able to say you what you're doing wrong.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      N 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @Npsad Sorry, but as long as you do not show your code nobody will be able to say you what you're doing wrong.

                        N Offline
                        N Offline
                        Npsad
                        wrote on last edited by
                        #12

                        @jsulm sorry for that can you please share me a code of how to update old UI itself

                        jsulmJ 1 Reply Last reply
                        0
                        • N Npsad

                          @jsulm sorry for that can you please share me a code of how to update old UI itself

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          @Npsad Are you aware that you are asking a question nobody can answer? I have no idea what you want to update. Show your code then we can tell you what is wrong.

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          N 1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @Npsad Are you aware that you are asking a question nobody can answer? I have no idea what you want to update. Show your code then we can tell you what is wrong.

                            N Offline
                            N Offline
                            Npsad
                            wrote on last edited by
                            #14

                            @jsulm okk that code is in middle of application
                            So am passing sample code where say here main window is having line edit and dialog window on click mainw window line edit should get updated with value hope it is clear....

                            MainWindow::MainWindow(QWidget *parent) :
                                QMainWindow(parent),
                                ui(new Ui::MainWindow)
                            {
                                ui->setupUi(this);
                            }

                            MainWindow::~MainWindow()
                            {
                                delete ui;
                            }

                            void MainWindow::on_pushButton_clicked()
                            {
                                Dialog* obj = new Dialog(this);
                                obj->show();
                            }
                            void MainWindow::updatemthd(QString Upd)
                            {

                            qDebug()<<Upd;
                                ui->lineEdit->repaint();
                                ui->lineEdit->setText(Upd);
                            }

                            Dialog::Dialog(QWidget *parent) :
                                QDialog(parent),
                                ui(new Ui::Dialog)
                            {
                                ui->setupUi(this);
                            }

                            Dialog::~Dialog()
                            {
                                delete ui;
                            }

                            void Dialog::on_pushButton_clicked()
                            {
                                QString Upd="updated";
                                MainWindow* n= new MainWindow();
                                n->updatemthd(Upd);
                            }

                            J.HilkJ jsulmJ 2 Replies Last reply
                            0
                            • N Npsad

                              @jsulm okk that code is in middle of application
                              So am passing sample code where say here main window is having line edit and dialog window on click mainw window line edit should get updated with value hope it is clear....

                              MainWindow::MainWindow(QWidget *parent) :
                                  QMainWindow(parent),
                                  ui(new Ui::MainWindow)
                              {
                                  ui->setupUi(this);
                              }

                              MainWindow::~MainWindow()
                              {
                                  delete ui;
                              }

                              void MainWindow::on_pushButton_clicked()
                              {
                                  Dialog* obj = new Dialog(this);
                                  obj->show();
                              }
                              void MainWindow::updatemthd(QString Upd)
                              {

                              qDebug()<<Upd;
                                  ui->lineEdit->repaint();
                                  ui->lineEdit->setText(Upd);
                              }

                              Dialog::Dialog(QWidget *parent) :
                                  QDialog(parent),
                                  ui(new Ui::Dialog)
                              {
                                  ui->setupUi(this);
                              }

                              Dialog::~Dialog()
                              {
                                  delete ui;
                              }

                              void Dialog::on_pushButton_clicked()
                              {
                                  QString Upd="updated";
                                  MainWindow* n= new MainWindow();
                                  n->updatemthd(Upd);
                              }

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #15

                              @Npsad

                              is this your first time programming?


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              N 1 Reply Last reply
                              0
                              • J.HilkJ J.Hilk

                                @Npsad

                                is this your first time programming?

                                N Offline
                                N Offline
                                Npsad
                                wrote on last edited by
                                #16

                                @J.Hilk yup can you please give me a solution for this

                                1 Reply Last reply
                                0
                                • N Npsad

                                  @jsulm okk that code is in middle of application
                                  So am passing sample code where say here main window is having line edit and dialog window on click mainw window line edit should get updated with value hope it is clear....

                                  MainWindow::MainWindow(QWidget *parent) :
                                      QMainWindow(parent),
                                      ui(new Ui::MainWindow)
                                  {
                                      ui->setupUi(this);
                                  }

                                  MainWindow::~MainWindow()
                                  {
                                      delete ui;
                                  }

                                  void MainWindow::on_pushButton_clicked()
                                  {
                                      Dialog* obj = new Dialog(this);
                                      obj->show();
                                  }
                                  void MainWindow::updatemthd(QString Upd)
                                  {

                                  qDebug()<<Upd;
                                      ui->lineEdit->repaint();
                                      ui->lineEdit->setText(Upd);
                                  }

                                  Dialog::Dialog(QWidget *parent) :
                                      QDialog(parent),
                                      ui(new Ui::Dialog)
                                  {
                                      ui->setupUi(this);
                                  }

                                  Dialog::~Dialog()
                                  {
                                      delete ui;
                                  }

                                  void Dialog::on_pushButton_clicked()
                                  {
                                      QString Upd="updated";
                                      MainWindow* n= new MainWindow();
                                      n->updatemthd(Upd);
                                  }

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #17

                                  @Npsad said in [SOLVED] QLabel in main window is not updated/refreshed when modified in second window (QDialog):

                                  MainWindow* n= new MainWindow();

                                  Why do you create a new MainWindow instance here?
                                  This needs to be done in a different way: your dialog should NOT access MainWindow directly (it should not even know anything about MainWindow - software design). Instead it should emit a signal, this signal is connected in MainWindow to a slot and that slot then updates the UI.

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  N 1 Reply Last reply
                                  1
                                  • jsulmJ jsulm

                                    @Npsad said in [SOLVED] QLabel in main window is not updated/refreshed when modified in second window (QDialog):

                                    MainWindow* n= new MainWindow();

                                    Why do you create a new MainWindow instance here?
                                    This needs to be done in a different way: your dialog should NOT access MainWindow directly (it should not even know anything about MainWindow - software design). Instead it should emit a signal, this signal is connected in MainWindow to a slot and that slot then updates the UI.

                                    N Offline
                                    N Offline
                                    Npsad
                                    wrote on last edited by
                                    #18

                                    @jsulm thanks for that in actual application I tried with that it's working..... thanks

                                    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