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.
  • P Offline
    P Offline
    pepita
    wrote on last edited by
    #1

    Hi everyone!

    I'm new here, and also new to Qt, so I'm learning for about one week and now I'm facing some problems, that I think are basic concepts problems, that I'm missing (also c++ concepts). So please excuse me if my code hurts your eyes, or my questions are stupid... I'm trying to do my best.

    I'm developing an easy application to train the interaction between two or more windows in Qt, say one main window and a dialog.

    I want to modify Ui elements of the main window (i.e. QLabel), acting in the dialog (i.e. QPushButton), and also viceversa. My first attempt was to create pointers to the Ui classes, to let me access from another classes to them, but without success, and plenty of segmentation fault erros. I read a lot about the correct way to do this, specially in this forum, and I changed to the signal/slot method.

    My application is based in one QMainWindow, with a QLabel, and a QPushButton, among others. With this QPushButton I open a QDialog window with a QLineEdit and a QPushButton. It's intended to, when pressing the button, send the text, written in the line edit of the dialog, to the label of the main window.

    But the QLabel does not show the text. If I cheat a bit, I achieve the result: If I "re-show" the main window once the Dialog is shown, the label gets updated. But this is bad praxis...

    So I'm asking for some help about how to do this in the correct and clean way, or if I'm missing something important in this code.

    Thank you in advance.

    mainwindow.h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui
    {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

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

    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;

    };

    #endif // MAINWINDOW_H
    @

    externaldialog.h
    @
    #ifndef EXTERNALDIALOG_H
    #define EXTERNALDIALOG_H

    #include <QDialog>

    namespace Ui
    {
    class ExternalDialog;
    }

    class ExternalDialog : public QDialog
    {
    Q_OBJECT

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

    signals:
    void send_fromExt (const QString);

    public slots:
    void on_pushButton_SendText_clicked();

    private:
    Ui::ExternalDialog *ui;
    };

    #endif // EXTERNALDIALOG_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)
    {
    ui->setupUi(this);

    //this->show ();  
    /*Cheating: Opens a new mainwindow (which does update the label text) when openning externaldialog*/
    
    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()
    {
    //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();
    

    }

    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
       if call this->show after setting the label text, also a new mainwindow appears with the text, but letting the old one also open (two mainwindows) -> more cheating*/
    

    }

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

    externaldialog.cpp
    @
    #include <QDebug>

    #include "externaldialog.h"
    #include "ui_externaldialog.h"
    #include "mainwindow.h"

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

    MainWindow *mainW = new MainWindow;
    QObject::connect (this, SIGNAL (send_fromExt (QString)), mainW, SLOT(rcv_ext2Main(QString)));
    
    qDebug () << "externaldialog.cpp";
    

    }

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

    void ExternalDialog::on_pushButton_SendText_clicked()
    {
    emit send_fromExt (ui->lineEdit_ExternalInput->text ());
    qDebug () << "Sent from externaldialog";
    }
    @

    main.cpp
    @
    #include <QDebug>
    #include <QApplication>

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

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
    }
    @

    This is compiling. To cheat like me uncomment the lines ;)
    Thanks again.

    1 Reply Last reply
    0
    • 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 Online
                    jsulmJ Online
                    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 Online
                        jsulmJ Online
                        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 Online
                            jsulmJ Online
                            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 Online
                                    jsulmJ Online
                                    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