QMessageBox not able to close.
-
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() } } }
-
@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()?
-
This post is deleted!
-
This post is deleted!
-
-
@Anas_Deshmukh
Although I doubt it will help your behaviour --- though worth a try --- you might like to replacethis->msgBox->close();
with something likethis->msgBox->reject();
orthis->msgBox->done(-1);
, to aid your caller. -
@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? -
@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. -
@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.