我有一个问题对你们中的一些人来说有明显的答案,但我就是想不通。
QMessageBox http://qt-project.org/doc/qt-5/qmessagebox.html有两种显示方式,要么exec()
停止程序执行直到用户关闭消息框,要么show()
只显示框(可能在单独的线程中或以某种方式允许程序在盒子等待用户时继续)。
使用 show() 后如何删除框?
此代码立即关闭它,消息框出现纳秒,然后它就消失了:
QMessageBox *mb = new QMessageBox(parent);
mb->setWindowTitle(title);
mb->setText(text);
mb->show();
delete mb; // obvious, we delete the mb while it was still waiting for user, no wonder it's gone
这段代码做同样的事情
QMessageBox mb(parent);
mb.setWindowTitle(title);
mb.setText(text);
mb.show();
// obvious, as we exit the function mb which was allocated on stack gets deleted
这段代码也一样
QMessageBox *mb = new QMessageBox(parent);
mb->setWindowTitle(title);
mb->setText(text);
mb->show();
mb->deleteLater(); // surprisingly this doesn't help either
那么我怎样才能正确使用 show() 而不必以某种复杂的方式处理它的删除呢?是否有类似deleteOnClose()
功能会告诉它在用户关闭后自行删除?