Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to call function which is in mainwindow.cpp from dailog.cpp in QT ?
QtWS25 Last Chance

How to call function which is in mainwindow.cpp from dailog.cpp in QT ?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 2 Posters 2.0k 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.
  • T Offline
    T Offline
    TM9412
    wrote on last edited by
    #1

    How to call function which is in mainwindow.cpp from dailog.cpp in QT ?

    J.HilkJ 1 Reply Last reply
    0
    • T TM9412

      How to call function which is in mainwindow.cpp from dailog.cpp in QT ?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @TM9412
      hi, this is QT 101

      //in your mainwindow
      
      connect(PointerToDialogObject, &Dialog::mySignal, this, &MainWindow::mySlot);
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      2
      • T Offline
        T Offline
        TM9412
        wrote on last edited by TM9412
        #3

        can you please provide more detail

        I want to call the function from dailog.cpp file and that function which is in mainwindow.cpp file

        J.HilkJ 1 Reply Last reply
        0
        • T TM9412

          can you please provide more detail

          I want to call the function from dailog.cpp file and that function which is in mainwindow.cpp file

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @TM9412

          //main.cpp
          #include <QApplication>
          #include "mainwindow.h"
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
          
              MainWindow mainw;
              mainw.show();
          
              return a.exec();
          }
          
          //mainwindow.h
          #ifndef MAINWINDOW_H
          #define MAINWINDOW_H
          
          #include <QMainWindow>
          
          class MainWindow : public QMainWindow
          {
              Q_OBJECT
          public:
              explicit MainWindow(QWidget *parent = nullptr);
          
          
          public slots:
              void okClicked();
              void CancleClicked();
          };
          
          #endif // MAINWINDOW_H
          
          //Mainwindow.cpp
          #include "mainwindow.h"
          #include <QDebug>
          
          #include "dialog.h"
          
          MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
          {
              Dialog *dia = new Dialog();
              dia->show();
          
              connect(dia, &Dialog::btnOkClicked, this, &MainWindow::okClicked);
              connect(dia, &Dialog::btnCancelClicked, this, &MainWindow::CancleClicked);
          
              connect(dia, &Dialog::btnOkClicked, dia, &Dialog::deleteLater);
              connect(dia, &Dialog::btnCancelClicked, dia, &MainWindow::deleteLater);
          }
          
          void MainWindow::okClicked()
          {
              qDebug() << "MainWindow" << "ok clicked in Dialog";
          }
          
          void MainWindow::CancleClicked()
          {
              qDebug() << "MainWindow" << "Cancle clicked in Dialog";
          }
          
          
          //Dialog.h
          #ifndef DIALOG_H
          #define DIALOG_H
          
          #include <QDialog>
          
          namespace Ui {
          class Dialog;
          }
          
          class Dialog : public QDialog
          {
              Q_OBJECT
          
          public:
              explicit Dialog(QWidget *parent = 0);
              ~Dialog();
          
          
          signals:
              void btnOkClicked();
              void btnCancelClicked();
          };
          
          #endif // DIALOG_H
          
          
          //Dialog.cpp
          #include "dialog.h"
          #include "QVBoxLayout"
          #include "QPushButton"
          
          Dialog::Dialog(QWidget *parent) :
              QDialog(parent)
          {
          
              QPushButton *btnOk = new QPushButton("Ok");
              QPushButton *btnCancel = new QPushButton("Cancel");
          
              QVBoxLayout * layout = new QVBoxLayout();
              layout->addWidget(btnOk);
              layout->addWidget(btnCancel);
              this->setLayout(layout);
          
              connect(btnOk, &QPushButton::clicked, this, &Dialog::btnOkClicked);
              connect(btnCancel, &QPushButton::clicked, this, &Dialog::btnCancelClicked);
          }
          
          Dialog::~Dialog()
          {
          }
          
          
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • T Offline
            T Offline
            TM9412
            wrote on last edited by
            #5

            thanks it is working..!

            1 Reply Last reply
            1
            • T Offline
              T Offline
              TM9412
              wrote on last edited by TM9412
              #6

              HEY @J-Hilk

              this code is working in my QT creator and a). and b). both scenario is working.
              but when i was integrated this code in my existing application at that time i was observed the two result

              a). Whenever my mainwindow is hidden and i pressed (clicked ) on the button my application is able to generate signal
              and in the mainwindow that function was executing (calling) i.e. qDebug() << "MainWindow" << "ok clicked in Dialog";

              b).
              but i have both window together mainwindow and dialog in my application at that time button is not working i (clicked ) pressed the button not able to generate signal and not able to calling the maiwindow function. i.e same
              qDebug() << "MainWindow" << "ok clicked in Dialog";

              May be i was facing some focus issue in my application ??

              J.HilkJ 1 Reply Last reply
              0
              • T TM9412

                HEY @J-Hilk

                this code is working in my QT creator and a). and b). both scenario is working.
                but when i was integrated this code in my existing application at that time i was observed the two result

                a). Whenever my mainwindow is hidden and i pressed (clicked ) on the button my application is able to generate signal
                and in the mainwindow that function was executing (calling) i.e. qDebug() << "MainWindow" << "ok clicked in Dialog";

                b).
                but i have both window together mainwindow and dialog in my application at that time button is not working i (clicked ) pressed the button not able to generate signal and not able to calling the maiwindow function. i.e same
                qDebug() << "MainWindow" << "ok clicked in Dialog";

                May be i was facing some focus issue in my application ??

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @TM9412 very hard to tell without some code to look at,
                but you can investigate further on your own.

                Either set a breakpoint in your Dialog.cpp to see if the clicked Signal is emitted, or connect a qDebug() output inside the Dialog.cpp and see if theres any output.


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                2

                • Login

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