在示例代码中:
class MyWidget : public QWidget
{
Q_OBJECT
protected:
void mousePressEvent(QMouseEvent *event)
{
qDebug() << event;
event->accept();
QInputDialog::getText(NULL, "", "");
}
};
当我点击小部件上的鼠标右键时,屏幕上会出现输入对话框。在我点击对话框上的任何按钮后,它关闭了,mousePressEvent一次又一次地调用并显示对话框。如果我在小部件上单击鼠标左键或Ctrl+鼠标左键,一切都可以正常工作。这个错误只出现在Mac操作系统上(在Windows下运行良好)。
请帮我避免这个错误。
那些静态/同步对话框函数对我来说总是有点可疑——它们是通过从getText()调用中递归地重新调用Qt事件循环例程来实现的,因此在使用它们时很容易出现"有趣"的重入问题。(例如,如果程序中的某个事件在用户关闭QInputDialog之前删除了MyWidget对象,那么在QInputDialog::getText()返回后,程序将从已删除的MyWidget的mousePressEvent()方法中执行,这种情况只会导致未定义的行为)
无论如何,我建议的解决方案是避免静态/同步getText()调用,而是使用信号,比如
#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;
};