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. Calling functions between classes
Forum Updated to NodeBB v4.3 + New Features

Calling functions between classes

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 5 Posters 2.1k Views 2 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.
  • D Offline
    D Offline
    dencla
    wrote on last edited by
    #1

    I have been trying to use a general log window for my application. I am new to QT and have had some luck calling the functions but it will not print the output to the textedit window. I have included the sample code below:

    #ifndef DIALOG_H
    #define DIALOG_H

    #include <QDialog>

    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

    #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 MainLogEdit(QString);
    

    private slots:
    void on_pushButton_clicked();

    private:
    Ui::MainWindow *ui;
    };
    #endif // MAINWINDOW_H

    #include <mainwindow.h>
    #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()
    {
    QString message;
    MainWindow *mui = (new MainWindow);
    message = "ClassComm Message";
    mui->MainLogEdit(message);

    }

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "dialog.h"

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    ui->textBrowser->append("Welcome");
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::MainLogEdit(QString message)
    {
    ui->textBrowser->append(message);
    }

    void MainWindow::on_pushButton_clicked()
    {
    QString message = "This Is a Test!!! ";

    MainLogEdit(message);
    //    this->close();
        Dialog sdialog;
        sdialog.setModal(true);
    
        sdialog.exec();
    

    }

    #include "mainwindow.h"

    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
    }

    I know there must be an easy way.

    Thanks
    Dennis Clark

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #21

      Good, then please mark the thread as solved so other forum users may know a solution has been found :-)

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mpergand
        wrote on last edited by
        #2
        void Dialog::on_pushButton_clicked()
        {
        QString message;
        MainWindow *mui = (new MainWindow);
        message = "ClassComm Message";
        mui->MainLogEdit(message);
        }
        

        You create a second MainWindow and don't show it.

        1 Reply Last reply
        1
        • D Offline
          D Offline
          dencla
          wrote on last edited by
          #3

          How does that update my log window? Sorry I'm new to this?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mpergand
            wrote on last edited by mpergand
            #4

            Try to understand what you are doing.

            In main you create a MainWindow and show it:

            int main(int argc, char *argv[])
            {
            QApplication a(argc, argv);
            MainWindow w;
            w.show();
            return a.exec();
            }
            

            then in Dialog::on_pushButton_clicked() you create another one !

            void Dialog::on_pushButton_clicked()
            {
            QString message;
            MainWindow *mui = new MainWindow;
            message = "ClassComm Message";
            mui->MainLogEdit(message);
            }
            

            add mui->show() at the end
            and you will see the message but in the wrong window :)

            1 Reply Last reply
            2
            • D Offline
              D Offline
              dencla
              wrote on last edited by
              #5

              This will make it show in my log window?

              jsulmJ 1 Reply Last reply
              0
              • D dencla

                This will make it show in my log window?

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @dencla Why do you create a new MainWindow in on_pushButton_clicked()?
                Do you understand that it is not the one you created in main()?
                And do you understand that you do not see that second MainWindow because you do not call show() on it?
                This is what @mpergand is trying to explain to you...

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dencla
                  wrote on last edited by
                  #7

                  I see it creates a new window and shows display it, but it opens multiple windows. :( Is there a way to update the current Main Window? I understand now what is happening. How do I access the my MainLogEdit() function from the dialog class with out creating mui???

                  jsulmJ 1 Reply Last reply
                  0
                  • D dencla

                    I see it creates a new window and shows display it, but it opens multiple windows. :( Is there a way to update the current Main Window? I understand now what is happening. How do I access the my MainLogEdit() function from the dialog class with out creating mui???

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @dencla said in Calling functions between classes:

                    but it opens multiple windows

                    THIS IS EXACTLY WHAT @mpergand TOLD YOU!
                    Do not create a new MainWindow in the slot.
                    If you want to send something to MainWindow from Dialog create a signal in Dialog and connect a slot to that signal in MainWindow. Then in Dialog you emit this signal passing it the message. This is how Qt applications work.
                    https://doc.qt.io/qt-5/signalsandslots.html

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • M Offline
                      M Offline
                      mpergand
                      wrote on last edited by mpergand
                      #9

                      Use signal and slot (basic Qt programming)
                      Emit a signal from your dialog that will be received in a slot of your MainWindow.

                      Signals & Slots

                      1 Reply Last reply
                      2
                      • D Offline
                        D Offline
                        dencla
                        wrote on last edited by
                        #10

                        I can see how that will work, but how do I call the MainLogEdit function in MainWindow with emit() from Dialog.

                        M 1 Reply Last reply
                        0
                        • D dencla

                          I can see how that will work, but how do I call the MainLogEdit function in MainWindow with emit() from Dialog.

                          M Offline
                          M Offline
                          mpergand
                          wrote on last edited by mpergand
                          #11

                          @dencla said in Calling functions between classes:

                          but how do I call the MainLogEdit function in MainWindow with emit() from Dialog.

                          You don't call anything, you emit a signal.

                          Have you defined a signal in Dialog.h
                          and a slot in MainWindow.h

                          Show us your code.

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            dencla
                            wrote on last edited by
                            #12

                            Yes it is at the top of the post.

                            jsulmJ JonBJ 2 Replies Last reply
                            0
                            • D dencla

                              Yes it is at the top of the post.

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #13

                              @dencla said in Calling functions between classes:

                              Yes it is at the top of the post.

                              No, it is not.
                              Again: add a signal to your dialog, add a slot to MainWindow, connect both, in your dialog you emit this signal.

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • D dencla

                                Yes it is at the top of the post.

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #14

                                @dencla
                                @jsulm referred you earlier to https://doc.qt.io/qt-5/signalsandslots.html doc page. There is a heading there A Small Example:

                                A minimal C++ class declaration might read:

                                That example shows the components you need for @mpergand's

                                Emit a signal from your dialog that will be received in a slot of your MainWindow.

                                1. Declaration of a signal method. (Dialog.h)
                                2. emit of the signal. (Dialog.cpp)
                                3. Declaration of a slot method. (MainWindow.h)
                                4. connect() of the signal-object+signal-method to the slot-object+slot-method. (MainWindow.cpp)

                                That is what you need to write. The only difference for you is where in the simple example has both signal & slot in the same class, you will have them in separate classes. 1 & 2 above are to be in your Dialog class, 3 & 4 in MainWindow class.

                                D 1 Reply Last reply
                                2
                                • JonBJ JonB

                                  @dencla
                                  @jsulm referred you earlier to https://doc.qt.io/qt-5/signalsandslots.html doc page. There is a heading there A Small Example:

                                  A minimal C++ class declaration might read:

                                  That example shows the components you need for @mpergand's

                                  Emit a signal from your dialog that will be received in a slot of your MainWindow.

                                  1. Declaration of a signal method. (Dialog.h)
                                  2. emit of the signal. (Dialog.cpp)
                                  3. Declaration of a slot method. (MainWindow.h)
                                  4. connect() of the signal-object+signal-method to the slot-object+slot-method. (MainWindow.cpp)

                                  That is what you need to write. The only difference for you is where in the simple example has both signal & slot in the same class, you will have them in separate classes. 1 & 2 above are to be in your Dialog class, 3 & 4 in MainWindow class.

                                  D Offline
                                  D Offline
                                  dencla
                                  wrote on last edited by
                                  #15

                                  @JonB That is what I am working on I have been watching videos and such to do what you are suggesting. I understand the concept, but I'm sure that it is the syntax that is throwing me. Thanks to all. SOmetimes it takes a lot to get a new idea through to an old brain.

                                  1 Reply Last reply
                                  0
                                  • D Offline
                                    D Offline
                                    dencla
                                    wrote on last edited by
                                    #16

                                    I am understanding the issue that I am having thanks to everyone's help. I am still having an issue with the dialog pushbutton emiti correctly to the Main Window. Below is the code in the MainWindow.cpp the signals and slots are in their prespective header files. The code compiles but does not generate a message in MainWindow textEdit window from the onDataChange signal, but it does for the onNewData signal. I am close but still cannot get the window to update.

                                    #include "mainwindow.h"
                                    #include "ui_mainwindow.h"
                                    #include "dialog.h"

                                    MainWindow::MainWindow(QWidget *parent)
                                    : QMainWindow(parent)
                                    , ui(new Ui::MainWindow)
                                    {
                                    Dialog test;

                                    ui->setupUi(this);
                                    ui->textBrowser->append("Welcome");
                                    
                                    QString text = "Comm Test";
                                    
                                    connect(this,&MainWindow::onNewData,
                                            this,&MainWindow::MainLogEdit);
                                    connect(&test,&Dialog::onDataChange,
                                            this,&MainWindow::MainLogEdit);
                                    

                                    }

                                    MainWindow::~MainWindow()
                                    {
                                    delete ui;
                                    }

                                    int counter;

                                    void MainWindow::MainLogEdit(QString message)
                                    {
                                    ui->textBrowser->append(message);
                                    ui->lineEdit->setText(QString::number(counter++));

                                    }

                                    void MainWindow::on_pushButton_clicked()
                                    {
                                    QString message = "This Is a Test!!! ";
                                    emit onNewData(message);
                                    Dialog sdialog;
                                    sdialog.setModal(true);

                                        sdialog.exec();
                                    

                                    }

                                    I will continue exploring what is needed. It is my guess that the Diaglog test is another instance of Dialog.
                                    Thanks again.

                                    1 Reply Last reply
                                    0
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #17

                                      Hi,

                                      At no point are you connecting the dialog to your main window.

                                      Interested in AI ? www.idiap.ch
                                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      0
                                      • D Offline
                                        D Offline
                                        dencla
                                        wrote on last edited by
                                        #18

                                        Thanks for your help that is what the issue was. I change the way the dialog was being called and made the value a member of MainWindow.

                                        1 Reply Last reply
                                        0
                                        • SGaistS Offline
                                          SGaistS Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #19

                                          So you also updated the connections ?
                                          Can you now properly communicated between the dialog and the main window ?

                                          Interested in AI ? www.idiap.ch
                                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          1 Reply Last reply
                                          0
                                          • D Offline
                                            D Offline
                                            dencla
                                            wrote on last edited by
                                            #20

                                            Yes I can. Thank you.

                                            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