Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Calling functions between classes
Qt 6.12 LTS is out! See what's new in the release blog

Calling functions between classes

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 5 Posters 5.7k 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
    #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
                                  • 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

                                    • Login

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