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

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

  • Moderators

    hi, this is QT 101

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

  • 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

  • Moderators


    #include <QApplication>
    #include "mainwindow.h"
    int main(int argc, char *argv[])
        QApplication a(argc, argv);
        MainWindow mainw;;
        return a.exec();
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    class MainWindow : public QMainWindow
        explicit MainWindow(QWidget *parent = nullptr);
    public slots:
        void okClicked();
        void CancleClicked();
    #endif // MAINWINDOW_H
    #include "mainwindow.h"
    #include <QDebug>
    #include "dialog.h"
    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
        Dialog *dia = new Dialog();
        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";
    #ifndef DIALOG_H
    #define DIALOG_H
    #include <QDialog>
    namespace Ui {
    class Dialog;
    class Dialog : public QDialog
        explicit Dialog(QWidget *parent = 0);
        void btnOkClicked();
        void btnCancelClicked();
    #endif // DIALOG_H
    #include "dialog.h"
    #include "QVBoxLayout"
    #include "QPushButton"
    Dialog::Dialog(QWidget *parent) :
        QPushButton *btnOk = new QPushButton("Ok");
        QPushButton *btnCancel = new QPushButton("Cancel");
        QVBoxLayout * layout = new QVBoxLayout();
        connect(btnOk, &QPushButton::clicked, this, &Dialog::btnOkClicked);
        connect(btnCancel, &QPushButton::clicked, this, &Dialog::btnCancelClicked);

  • thanks it is working..!

  • 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";

    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 ??

  • Moderators

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

Log in to reply