现在我能做的:
void MainWindow::on_actionPATH_triggered() {
std::unique_ptr<QDialog> win(new QDialog());
win->exec();
}
我应该使用async
/在单独的线程中运行以避免阻塞主窗口,还是有办法订阅关闭甚至删除/释放对象?
最佳答案
您可以只使用show()
void MainWindow::on_actionPATH_triggered() {
QDialog* win = new QDialog();
//needed connect
win->setAttribute(Qt::WA_DeleteOnClose);//we don't want memory leak
win->show();
}
并使用
win->setModal(false);//but it is default option, you don't need to change it
来自doc :
By default, this property is false and show() pops up the dialog as modeless. Setting his property to true is equivalent to setting QWidget::windowModality to Qt::ApplicationModal. exec() ignores the value of this property and always pops up the dialog as modal.
Qt::WA_DeleteOnClose
将在用户关闭对话框时删除它。
您还可以将父级设置为对话框:
QDialog* win = new QDialog(this);
在这种情况下,win
将与您的 mainWindow 一起删除。
Info about Qt parent child relationship
而且你不需要这里单独的线程。
关于c++ - 如何在不阻塞主窗体的情况下创建对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27363466/