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

Write to QPlainTextEdit from another class

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 692 Views
  • 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.
  • cheopsC Offline
    cheopsC Offline
    cheops
    wrote on 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
    • cheopsC cheops

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

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on 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

      cheopsC 1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #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 :)

        cheopsC 1 Reply Last reply
        1
        • mrjjM mrjj

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

          cheopsC Offline
          cheopsC Offline
          cheops
          wrote on last edited by
          #3

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

          thanks for your answer!

          mrjjM 1 Reply Last reply
          1
          • cheopsC cheops

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

            thanks for your answer!

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on 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.

            cheopsC 1 Reply Last reply
            0
            • mrjjM mrjj

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

              cheopsC Offline
              cheopsC Offline
              cheops
              wrote on 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...

              mrjjM 1 Reply Last reply
              0
              • cheopsC cheops

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

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on 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

                cheopsC 1 Reply Last reply
                0
                • mrjjM mrjj

                  @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

                  cheopsC Offline
                  cheopsC Offline
                  cheops
                  wrote on 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

                  mrjjM 1 Reply Last reply
                  1
                  • cheopsC cheops

                    @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

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 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

                    • Login

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