Write to QPlainTextEdit from another class
-
Dear all,
I try to write text to a QPlaintTextEdit in my main window from a dialog. I can access the write()-function in my mainWindow from the dialog (via the mainWindow), but I'm not able to write something to the GUI. My code:
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include "dialog.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_callDialog_clicked() { Dialog *dlg = new Dialog(this); dlg->exec(); } void MainWindow::write(QString testmessage) { qDebug() << testmessage; ui->plainTextEdit->appendPlainText(testmessage); }
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); void write(QString testmessage); private slots: void on_pushButton_callDialog_clicked(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
dialog.cpp
#include "dialog.h" #include "ui_dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); } Dialog::~Dialog() { delete ui; } void Dialog::on_pushButton_clicked() { MainWindow *window = new MainWindow;; window->write("testMessage"); }
dialog.h
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include "mainwindow.h" namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = nullptr); ~Dialog(); private slots: void on_pushButton_clicked(); private: Ui::Dialog *ui; }; #endif // DIALOG_H
I can print the message with qDebug() to the console from the dialog, but not to the mainWindow. Obviously I do something wrong, can someone point me to the right direction?
Thanks in advance!
-
@cheops
Hi
I made you a sample to study. added comments to the key parts.
ask if doubts. It does what you ask. open dialog. dialog sends text to plainText in mainwindow.project:
https://www.dropbox.com/s/w4jxdskeco345xk/TalkToTextEdit.zip?dl=0 -
Hi
Its a classic brain error :)You create a new MainWindow and talk to.
That is not the one you see on screen.MainWindow *window = new MainWindow;; // new one, so not the one where you start dialog from.
window->write("testMessage");What to do then.
Add a slot in Mainwindow that writes to the plain edit
add a signal to the dialog
then connect MainWindow and Dialog in main window where you create the dialog
then in dialog you can do
emit yourNewSignal("the text");
and then it works :)
-
@mrjj To be honest, I'm a bit confused on how to use signals/slots above the border of different classes... can you explain a bit more in detail, maybe with some lines of example code? I searched the net, but I didnt find a detailed explanation about this specific topic...
-
@cheops
Hi
I made you a sample to study. added comments to the key parts.
ask if doubts. It does what you ask. open dialog. dialog sends text to plainText in mainwindow.project:
https://www.dropbox.com/s/w4jxdskeco345xk/TalkToTextEdit.zip?dl=0 -
@cheops
You are welcome.
This signal to slot can be used in almost all cases where one window and other window has to talk.
What you can send as a parameter is not limited to simple things, even full clas is possible.
Also the dialog didn't need to know anything about main window.That is a good design as you could hook any other window up to same signal and get the text without changing anything in dialog.
So if you Design with signal and slot, you get nice decoupled code that is resilient to change.