Qt 是事件驱动的,基本上,每一个Qt程序我们都会通过QCoreApplication或其派生类的exec()函数来开启事件循环(QEventLoop):
int main(int argc, char**argv)
{
QApplication a(argc, argv);
return a.exec();
}
但是在同一个线程内,我们可以开启多个事件循环,比如通过:
这些东西都很常用,不是么?它们每一个里面都在执行这样的语句:
QEventLoop loop; //事件循环 loop.exec();
既然是同一线程内,这些显然是无法并行运行的,那么只能是嵌套运行。
如何用最小的例子来直观说明这个问题呢?
利用定时器来演示应该是最方便的。于是,很容易写出来这样的代码:
#include <QtCore>
class Object : public QObject
{
public:
Object() {startTimer(200); }
protected:
void timerEvent(QTimerEvent *) {
static int level = 0;
qDebug()<<"Enter: <<"++level;
QEventLoop loop; //事件循环
loop.exec();
qDebug()<<"Leave: "<<level;
}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Object w;
return a.exec();
}
然后我们可以期待看到:
Enter: 1 Enter: 2 Enter: 3 ...
但是,很让人失望,这并不会工作。因为Qt对Timer事件派发时进行了处理:
我们对这个例子进行一点改进:
另外,为了友好一点,使用了 QPlainTextEdit 来显示结果:
#include <QtGui>
#include <QtCore>
class Widget : public QPlainTextEdit
{
public:
Widget() {startTimer(200); }
protected:
bool event(QEvent * evt)
{
if (evt->type() == QEvent::Timer) {
qApp->postEvent(this, new QEvent(QEvent::User));
} else if (evt->type() == QEvent::User) {
static int level = 0;
level++;
this->appendPlainText(QString("Enter : %1").arg(++level));
QEventLoop loop;
loop.exec();
this->appendPlainText(QString("Leave: %1").arg(level));
}
return QPlainTextEdit::event(evt);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
这个例子确实没有什么,因为似乎没人会写这样的代码。
但是,当你调用
等函数时,实际上就启动了嵌套的事件循环,而如果不小心的话,还有遇到各种怪异的问题!
== QDialog::exec() vs QDialog::open()== 在 QDialog 模态对话框与事件循环 以及 漫谈QWidget及其派生类(四) 我们解释过QDialog::exec()。它最终就是调用QEventLoop::exec()。
QDialog::exec()这个东西是这么常用,以至于我们很少考虑这个东西的不利因素。QDialog::open()尽管被官方所推荐,但是似乎很少有人用,很多人可能还不知道它的存在。
但是Qt官方blog中:
一文介绍了 exec() 可能造成的危害,并鼓励大家使用 QDialog::open()
在Qt官方的Qt Quarterly中: * QtQuarterly30 之 New Ways of Using Dialogs 对QDialog::open()有详细的介绍
看个例子:我们通过颜色对话框选择一个颜色,
void Widget::onXXXClicked()
{
QColor c = QColorDialog::getColor();
}
void Widget::onXXXClicked()
{
QColorDialog dlg(this);
dlg.exec();
QColor c = dlg.currentColor();
}
void Widget::onXXXClicked()
{
QColorDialog *dialog = new QColorDialog;
dialog->open(this, SLOT(dialogClosed(QColor)));
}
void Widget::dialogClosed(const QColor &color)
{
QColor = color;
}
好处嘛(就摘录Andreas Aardal Hanssen的话吧):
Kde开发者官方blog中描述这个问题:
在某个槽函数中,我们通过QDialog::exec() 弹出一个对话框。
void ParentWidget::slotDoSomething()
{
SomeDialog dlg( this ); //分配在栈上的对话框
if (dlg.exec() == QDialog::Accepted ) {
const QString str = dlg.someUserInput();
//do something with with str
}
}
如果这时ParentWidget或者通过其他方式(比如dbus)得到通知,需要被关闭。会怎么样?
程序将崩溃:ParentWidget析构时,将会delete这个对话框,而这个对话框却在栈上。
简单模拟一下(在上面代码中加一句即可):
void ParentWidget::slotDoSomething()
{
QTimer::singleShot(1000, this, SLOT(deleteLater()));
...
这篇blog最终给出的结论是:将对话框分配到堆上,并使用QPointer来保存对话框指针。
上面的代码,大概要写成这样:
void ParentWidget::slotDoSomething()
{
QWeakPointer<SomeDialog> dlg = new SomeDialog(this);
if (dlg.data()->exec() == QDialog::Accepted ) {
const QString str = dlg.data()->someUserInput();
//do something with with str
} else if(!dlg) {
//....
}
if (!dlg) {
delete dlg.data();
}
}
感兴趣的可以去看看原文。比较起来 QDialog::open() 应该更值得考虑。
当程序做繁重的操作时,而又不愿意开启一个新线程时,我们都会选择调用
QCoreApplication::sendPostedEvents ()
来使得程序保持相应。这和前面提到的哪些exec()开启局部事件循环的效果其实是完全一样的。
无论是这个,还是QEventLoop::exec()最终都是调用:
QAbstractEventDispatcher::processEvents()
来进行事件派发。前面的问题也都是由它派发的事件引起的。
原文:http://www.cnblogs.com/lvdongjie/p/3758184.html