Qt 阻止调用显示对话框?
我正在研究 Qt 和 QML。
所以我想要一个阻塞调用来显示对话框并检索用户输入。保证该调用不会在当前 UI 线程上进行。
我有使用 QML 创建的 QDeclarativeView 对象。我可以使用 show 方法()显示它。但现在我想阻塞直到用户点击“确定”/“取消”,此时我将从对象中提取信息并将信息返回给调用者。
所以问题是:
- 这是使用 QT 的合理方法吗?
- 如果是,我该如何使当前线程阻塞?
I am mucking about with Qt and QML.
So I would like to have a blocking call to display a dialog and retrieve user input. The call is guaranteed not to be on the current UI thread.
I have QDeclarativeView object that I have created using QML. I can display it using the show method(). But now I want to block until the user hits OK/Cancel, at which point I will extract the info from the object and return the information to the caller.
So the questions are:
- Is this a reasonable way to use QT
- If so how do I make the current thread block?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用连接到 QDialog::exec() 插槽的 Qt::BlockingQueuedConnection。
http://doc.qt.io/qt-5/qt.html #ConnectionType-enum
Use a Qt::BlockingQueuedConnection connected to the QDialog::exec() slot.
http://doc.qt.io/qt-5/qt.html#ConnectionType-enum
如果您从 QDialog 开始,则可以使用 exec() 方法进行阻塞,直到对话框关闭。您可以将声明性视图放入对话框中。
至于这是否是 Qt 的合理使用,这取决于您的需求。在许多情况下,用户更喜欢非模式对话框,这样他们可以在对话框打开的情况下继续执行其他操作。阻塞函数通常不是呈现这种非模态窗口的最方便的方式。通常,“正确”的做法是将信号连接到处理程序,该处理程序在对话框关闭时执行。
If you start with a QDialog, you can use the exec() method to block until the dialog is dismissed. You can put your Declarative View onto the dialog.
As far as if this is a reasonable use of Qt, it depends on your needs. In many cases, users will prefer nonmodal dialogs where they can continue doing other things with the dialog open. A blocking function is uaually not the most convenient way to present such a nonmodal window. Normally, the "correct" thing to do is just connect a signal to a handler which executes whenever your dialog is dismissed.