Modeless dialog in Qt



  • I have a requirement ... the requirement is that on a mail window(Dialog class) there should another Message Dialog (MessageDialog class) which is moduless dialog which has a pushButton should pop-up.. The catch is that the main Dialog can be hidden but the moduless Message dialog should still be displayed and should not disappear. But on clicking OK button click in message dialog after the main window is hidden , the Message dialog should get closed.. but should not exit of the program... But the thing is after OK button click in the Message box the Message get closed and also the program completely terminates ... I do not know why this happens? Can anyone please explain why. where is that i am making a mistake

    • If the Ok button is clicked before the main Window disappers the program does not Exit ...
    • If I Do not hide the Main Dialog the behavior is fine.. That is the program does not terminate...

    dialog.h
    @#ifndef DIALOG_H
    #define DIALOG_H
    #include <QTimer>
    #include <QDialog>
    #include <QMessageBox>

    class MessageDialog : public QDialog
    {
    Q_OBJECT
    public:
    MessageDialog(QWidget *parent = 0);
    void myExec();
    void setVisible(bool visible);
    static void showMessageBox(QWidget *parent = 0);
    public slots:
    void buttonClicked();
    signals:
    void signalButtonClick();

    };

    class Dialog : public QDialog
    {
    Q_OBJECT
    QTimer timer;
    public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

    private:
    QMessageBox *msgBox;
    MessageDialog *myDailog;
    public slots:
    void slotTimerExp();
    void slotTimerExp2();
    };

    #endif // DIALOG_H
    @

    dialog.cpp
    @#include "dialog.h"
    #include "ui_dialog.h"
    #include <QApplication>
    #include <QDesktopWidget>
    #include <QPushButton>
    #include <QLabel>
    #include <QDebug>

    MessageDialog::MessageDialog(QWidget *parent) : QDialog(parent)
    {
    QPushButton *pushButton = new QPushButton("ok", this);
    connect(pushButton, SIGNAL(clicked()), this, SLOT(buttonClicked()));
    }
    void MessageDialog::setVisible(bool visible)
    {
    QDialog::setVisible(visible);
    if(visible == true)
    {
    myExec();
    }
    }

    void MessageDialog::buttonClicked()
    {
    emit signalButtonClick();
    this->close();
    }

    void MessageDialog::myExec()
    {
    QEventLoop eventLoop;
    qDebug() << "MSG Exec Called";
    eventLoop.connect(this, SIGNAL(signalButtonClick()), &eventLoop, SLOT(quit()));
    eventLoop.exec();
    qDebug() << "MSG Exec end";
    }

    void MessageDialog::showMessageBox(QWidget *parent)
    {
    MessageDialog *messageDialog = new (std::nothrow) MessageDialog(parent);

    messageDialog->show();
    qDebug() << "MessageDialog::showMessageBox deleting Ptr";
    delete(messageDialog);
    

    }

    Dialog::Dialog(QWidget *parent) :
    QDialog(parent)
    {
    timer.setSingleShot(true);
    connect(&timer, SIGNAL(timeout()), this, SLOT(slotTimerExp()));
    timer.start(1000);

    }

    Dialog::~Dialog()
    {
    qDebug() << "Dialog::~Dialog called";
    }

    void Dialog::slotTimerExp()
    {

    QTimer *timer2 = new QTimer();
    timer2->setSingleShot(true);
    connect(timer2, SIGNAL(timeout()), this, SLOT(slotTimerExp2()));
    timer2->start(1000);
    MessageDialog::showMessageBox(this);
    qDebug() << "After Show";
    

    }

    void Dialog::slotTimerExp2()
    {
    qDebug() << "slotTimerExp2 called";
    this->setVisible(false);
    }

    @

    main.cpp
    @#include <QtGui/QApplication>
    #include "dialog.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Dialog w;
    w.show();

    return a.exec(&#41;;
    

    }

    @

    Thank you


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.