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
    #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