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. Write to QPlainTextEdit from another class
Forum Updated to NodeBB v4.3 + New Features

Write to QPlainTextEdit from another class

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 747 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.
  • C Offline
    C Offline
    cheops
    wrote on 12 Jun 2020, 09:09 last edited by
    #1

    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!

    1 Reply Last reply
    0
    • C cheops
      12 Jun 2020, 19:17

      @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...

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 12 Jun 2020, 19:54 last edited by
      #6

      @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.

      alt text

      project:
      https://www.dropbox.com/s/w4jxdskeco345xk/TalkToTextEdit.zip?dl=0

      C 1 Reply Last reply 12 Jun 2020, 20:11
      0
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 12 Jun 2020, 09:52 last edited by mrjj 6 Dec 2020, 09:53
        #2

        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 :)

        C 1 Reply Last reply 12 Jun 2020, 10:03
        1
        • M mrjj
          12 Jun 2020, 09:52

          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 :)

          C Offline
          C Offline
          cheops
          wrote on 12 Jun 2020, 10:03 last edited by
          #3

          @mrjj Ups, maybe I should think a bit more before asking... ;-)

          thanks for your answer!

          M 1 Reply Last reply 12 Jun 2020, 18:16
          1
          • C cheops
            12 Jun 2020, 10:03

            @mrjj Ups, maybe I should think a bit more before asking... ;-)

            thanks for your answer!

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 12 Jun 2020, 18:16 last edited by
            #4

            @cheops
            All good. It's a classic thing , i see often here so
            it very easy to make it seems.
            So no worries. Please mark as solved if you get it working or ask more if
            you need help in using signals and slots.

            C 1 Reply Last reply 12 Jun 2020, 19:17
            0
            • M mrjj
              12 Jun 2020, 18:16

              @cheops
              All good. It's a classic thing , i see often here so
              it very easy to make it seems.
              So no worries. Please mark as solved if you get it working or ask more if
              you need help in using signals and slots.

              C Offline
              C Offline
              cheops
              wrote on 12 Jun 2020, 19:17 last edited by
              #5

              @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...

              M 1 Reply Last reply 12 Jun 2020, 19:54
              0
              • C cheops
                12 Jun 2020, 19:17

                @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...

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 12 Jun 2020, 19:54 last edited by
                #6

                @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.

                alt text

                project:
                https://www.dropbox.com/s/w4jxdskeco345xk/TalkToTextEdit.zip?dl=0

                C 1 Reply Last reply 12 Jun 2020, 20:11
                0
                • M mrjj
                  12 Jun 2020, 19:54

                  @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.

                  alt text

                  project:
                  https://www.dropbox.com/s/w4jxdskeco345xk/TalkToTextEdit.zip?dl=0

                  C Offline
                  C Offline
                  cheops
                  wrote on 12 Jun 2020, 20:11 last edited by
                  #7

                  @mrjj This is really great! It was exactly, what I needed: a simple to understand example... Thanks a lot, now I think, I understand the topic!

                  I never got such a good help...

                  Greetings,
                  cheops

                  M 1 Reply Last reply 12 Jun 2020, 20:20
                  1
                  • C cheops
                    12 Jun 2020, 20:11

                    @mrjj This is really great! It was exactly, what I needed: a simple to understand example... Thanks a lot, now I think, I understand the topic!

                    I never got such a good help...

                    Greetings,
                    cheops

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 12 Jun 2020, 20:20 last edited by
                    #8

                    @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.

                    1 Reply Last reply
                    0

                    1/8

                    12 Jun 2020, 09:09

                    • Login

                    • Login or register to search.
                    1 out of 8
                    • First post
                      1/8
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved