Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QMessageBox not able to close.
Forum Updated to NodeBB v4.3 + New Features

QMessageBox not able to close.

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 2.4k 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.
  • A Anas_Deshmukh

    Hi everyone,

    I am using qt4.8 on ubuntu 12.04 system. In my application i need to display popup and dynamically close it from qml using qml timer signal and cpp slot mechanism. below is my demo from project.

    My issue is, i have many random scenario where i need to close my popup. "QMessageBox :: isEnabled() " satisfies in all scenario but "QMessageBox :: close() " NOT works in all scenario.

    somehow i am unable to recreate i failure scenario in demo code.

    // window.h
    #ifndef WINDOW_H
    #define WINDOW_H
    
    #include <QtCore>
    #include <QMessageBox>
    #include <QGridLayout>
    #include <QSpacerItem>
    
    class window: public QObject
    {
    
        Q_OBJECT
    public:
        QMessageBox *msgBox=new QMessageBox;
    
        int popUp(QString str);
    
    public Q_SLOTS:
        Q_INVOKABLE void closePopUp();
    
    };
    
    #endif // WINDOW_H
    
    
    
    // window.cpp
    
    #include "window.h"
    
    int window::popUp(QString str) {
    
        qDebug() << "inside :: window::popUp(QString str) with param :: " << str;
    
        this->msgBox->setInformativeText(str);
        this->msgBox->setStandardButtons(QMessageBox::Ok);
    
        QSpacerItem* horizontalSpacer = new QSpacerItem(300, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
        QGridLayout* layout = (QGridLayout*)this->msgBox->layout();
        layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
    
        this->msgBox->setWindowFlags(Qt::FramelessWindowHint);
        this->msgBox->installEventFilter(this);
    
        int ret = this->msgBox->exec();
    
        switch (ret) {
    
        case QMessageBox::Ok:
        {
            ret=1;
            break;
        }
    
        }
    
        return ret;
    }
    
    
    
    void window::closePopUp() {
        qDebug() << "inside closePopUp.";
    
        if(this->msgBox->isEnabled()) {
    // i reached here in all scenario but unable to close everytime, need suggestion here
            qDebug() << "window:: msgBox :: isEnabled";  
            this->msgBox->close();
        }
        else qDebug() << "not able to close.";
    
    }
    
    // main.cpp
    
    #include <QApplication>
    #include "qmlapplicationviewer.h"
    
    #include "window.h"
    
    #include <QDeclarativeView>
    #include <QDeclarativeContext>
    
    Q_DECL_EXPORT int main(int argc, char *argv[])
    {
        QScopedPointer<QApplication> app(createApplication(argc, argv));
    
        QmlApplicationViewer viewer;
        viewer.addImportPath(QLatin1String("modules"));
        viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
        viewer.setMainQmlFile(QLatin1String("qml/autoLogOFF/main.qml"));
        viewer.showExpanded();
    
        int retVal;
        window windowObj;
        viewer.rootContext()->setContextProperty("windowObj",&windowObj);
    
        retVal = windowObj.popUp("testing");
        qDebug() << "debug : retVal :: " << retVal;
        return app->exec();
    }
    
    
    // mian.qml
    
    import QtQuick 1.1
    
    Rectangle {
        width: 360
        height: 360
        Text {
            text: qsTr("Hello World")
            anchors.centerIn: parent
        }
    
        Timer {
            id : autologOff
            interval: 5000
            running: true; repeat: true
    
            onTriggered: {
                console.log("autologoff shoot");
                windowObj.closePopUp()
            }
        }
    
    }
    
    
    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @Anas_Deshmukh How can you reach closePopUp() if you call

    int ret = this->msgBox->exec();
    

    in popUp() as exec() is a blocking call? How/where do you call closePopUp()?

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    A 2 Replies Last reply
    0
    • A Offline
      A Offline
      Anas_Deshmukh
      wrote on last edited by
      #3
      This post is deleted!
      1 Reply Last reply
      0
      • jsulmJ jsulm

        @Anas_Deshmukh How can you reach closePopUp() if you call

        int ret = this->msgBox->exec();
        

        in popUp() as exec() is a blocking call? How/where do you call closePopUp()?

        A Offline
        A Offline
        Anas_Deshmukh
        wrote on last edited by
        #4

        @jsulm Using qml timer sigal making closePopUp() as slot.
        please do refer main.qml part.

        jsulmJ 1 Reply Last reply
        0
        • jsulmJ jsulm

          @Anas_Deshmukh How can you reach closePopUp() if you call

          int ret = this->msgBox->exec();
          

          in popUp() as exec() is a blocking call? How/where do you call closePopUp()?

          A Offline
          A Offline
          Anas_Deshmukh
          wrote on last edited by
          #5
          This post is deleted!
          1 Reply Last reply
          0
          • A Anas_Deshmukh

            @jsulm Using qml timer sigal making closePopUp() as slot.
            please do refer main.qml part.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #6

            @Anas_Deshmukh Can you check what

            this->msgBox->close();
            

            returns?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            A 1 Reply Last reply
            0
            • jsulmJ jsulm

              @Anas_Deshmukh Can you check what

              this->msgBox->close();
              

              returns?

              A Offline
              A Offline
              Anas_Deshmukh
              wrote on last edited by
              #7

              @jsulm it return true in all scenario (either successfully close or not close at all).

              JonBJ 1 Reply Last reply
              0
              • A Anas_Deshmukh

                @jsulm it return true in all scenario (either successfully close or not close at all).

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #8

                @Anas_Deshmukh
                Although I doubt it will help your behaviour --- though worth a try --- you might like to replace this->msgBox->close(); with something like this->msgBox->reject(); or this->msgBox->done(-1);, to aid your caller.

                A 1 Reply Last reply
                0
                • JonBJ JonB

                  @Anas_Deshmukh
                  Although I doubt it will help your behaviour --- though worth a try --- you might like to replace this->msgBox->close(); with something like this->msgBox->reject(); or this->msgBox->done(-1);, to aid your caller.

                  A Offline
                  A Offline
                  Anas_Deshmukh
                  wrote on last edited by
                  #9

                  @JonB hi tried but both but nothing work.

                  if(this->msgBox->isEnabled() )  // this condition is still true even after using reject and done.
                  
                  JonBJ 1 Reply Last reply
                  0
                  • A Anas_Deshmukh

                    @JonB hi tried but both but nothing work.

                    if(this->msgBox->isEnabled() )  // this condition is still true even after using reject and done.
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #10

                    @Anas_Deshmukh
                    Like I said, I did not expect this change to help your issue now. But I think you will want it when you have your code working, since you test the result returned from the dialog.

                    Never mind whether this->msgBox->isEnabled() is still true, does the dialog actually close or not?

                    A 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Anas_Deshmukh
                      Like I said, I did not expect this change to help your issue now. But I think you will want it when you have your code working, since you test the result returned from the dialog.

                      Never mind whether this->msgBox->isEnabled() is still true, does the dialog actually close or not?

                      A Offline
                      A Offline
                      Anas_Deshmukh
                      wrote on last edited by
                      #11

                      @JonB Not close.

                      JonBJ 1 Reply Last reply
                      0
                      • A Anas_Deshmukh

                        @JonB Not close.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #12

                        @Anas_Deshmukh
                        Well, I'd try inserting a single-shot timer for 1 second which will close the message box immediately before your .exec() line, to eliminate whatever might be going on with your QML timer stuff. There are plenty of examples on the web of doing that for timed closure of a dialog. And/or change your .exec() to .show() (have to adjust your calling code) and see whether that makes it work.

                        A 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @Anas_Deshmukh
                          Well, I'd try inserting a single-shot timer for 1 second which will close the message box immediately before your .exec() line, to eliminate whatever might be going on with your QML timer stuff. There are plenty of examples on the web of doing that for timed closure of a dialog. And/or change your .exec() to .show() (have to adjust your calling code) and see whether that makes it work.

                          A Offline
                          A Offline
                          Anas_Deshmukh
                          wrote on last edited by Anas_Deshmukh
                          #13

                          @JonB Hi, i fix the problem.
                          Issue i had is somehow in my project one new object were created (typical scenario), and function closePopUp() unable to access newly created instance as it always pointing previously created object.

                          thaks for your time and consideration.

                          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 Extensions
                          • Unsolved