[SOLVED] QLabel in main window is not updated/refreshed when modified in second window (QDialog)
-
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
-
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 externaldialogSo, is it entering first in the SLOT and then in the SIGNAL?
-
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
-
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....... -
@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);
} -
@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.