c++ - QInputDialog in mouse event -
in example code:
class mywidget : public qwidget { q_object protected: void mousepressevent(qmouseevent *event) { qdebug() << event; event->accept(); qinputdialog::gettext(null, "", ""); } }; when click right mouse button on widget input dialog appear on screen. after click button on dialog closed , mousepressevent call again , again , show dialog. if click left mouse button or ctrl+left mouse button on widget work fine. bug apper on mac os (under windows work fine).
please me avoid bug.
those static/synchronous dialog functions seemed bit dubious me -- implemented recursively re-invoking qt event loop routine within gettext() call, , it's easy "interesting" re-entrancy issues when use them. (for example, if event in program delete mywidget object before user had dismissed qinputdialog, after qinputdialog::gettext() returned, program executing within mousepressevent() method of deleted mywidget object, situation begging cause undefined behavior)
in case, recommended fix avoid static/synchronous gettext() call , use signals instead, this:
#include <qwidget> #include <qinputdialog> #include <qmouseevent> class mywidget : public qwidget { q_object public: mywidget() : dialog(null) {} ~mywidget() {delete dialog;} protected: void mousepressevent(qmouseevent *event) { event->accept(); if (dialog) { dialog->raise(); dialog->setfocus(); } else { dialog = new qinputdialog; connect(dialog, signal(finished(int)), this, slot(dialogdismissed())); dialog->show(); } } private slots: void dialogdismissed() { if (dialog) { int result = dialog->result(); qstring t = dialog->textvalue(); printf("dialog finished, result %i, user entered text [%s]\n", result, t.toutf8().constdata()); dialog->deletelater(); // can't call delete because (dialog) calling function (via signal)! dialog = null; } } private: qinputdialog * dialog; };
Comments
Post a Comment