[SOLVED] QLabel in main window is not updated/refreshed when modified in second window (QDialog)
-
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_OBJECTpublic:
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_OBJECTpublic:
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. -
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. -
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.
-
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_OBJECTpublic:
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;
-
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.