Search code examples
c++qtqwidget

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 I click Right mouse button on widget Input dialog appear on screen. After I click any button on dialog it closed and mousePressEvent call again and again and show dialog. If I click Left mouse button or Ctrl+Left Mouse button on widget all work fine. This bug apper only on Mac OS (under Windows work fine).

Please help me to avoid this bug.


Solution

  • Those static/synchronous dialog functions always seemed a bit dubious to me -- they are implemented by recursively re-invoking the Qt event loop routine from within the getText() call, and so it's easy to get "interesting" re-entrancy issues when you use them. (For example, if some event in your program were to delete the MyWidget object before the user had dismissed the QInputDialog, then after QInputDialog::getText() returned, the program would be executing from within the mousePressEvent() method of a deleted MyWidget object, which is a situation that is just begging to cause undefined behavior)

    In any case, my recommended fix is to avoid the static/synchronous getText() call and use signals instead, something like 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 was %i, user entered text [%s]\n", result, t.toUtf8().constData());
              dialog->deleteLater();  // can't just call delete because (dialog) is what is calling this function (via a signal)!
              dialog = NULL;
           }
        }
    
    private:
        QInputDialog * dialog;
    };