How to call function which is in mainwindow.cpp from dailog.cpp in QT ?
-
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
//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() { }
-
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 resulta). 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 ??
-
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 resulta). 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 ??
@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.