如何在控制台中制作一个简单的非阻塞弹出窗口 C++程序?
我试图找出在普通命令行应用程序中实现这样一个有用的函数最容易的 C++ GUI 工具包(+stdlib+libc):
void ShowStringWindow(string& s) {
// ...
}
ShowStringWindow 应该显示一个带有包含字符串 s 的可编辑框的窗口。
它应该立即返回,以便主线程可以继续。
注意:
- 如果主线程更改了显示的字符串,则字符串也应该更改。 (主动检查很好)
- 如果用户编辑字符串(并用 Enter 确认),则应该更新字符串 s 。
- 您可以假设主线程在更新时不会读取或写入该字符串。
- 接下来调用 ShowStringWindow 添加更多类似的窗口(或者如果实现起来不太困难,则向现有窗口添加更多小部件)。
- 它应该可以在 linux/ubuntu 上运行。
我将非常感谢能够工作的代码,但这并不一定有用。
I try to figure out in which C++ GUI toolkit (+stdlib+libc) it would be easiest to implement such a useful function in a normal command-line application:
void ShowStringWindow(string& s) {
// ...
}
ShowStringWindow should display a window with an editable box containing string s.
It should return immediately, so the main thread can continue.
Notes:
- If the main thread changes the string the displayed, string should change as well. (active checking is fine)
- If a user edits the string (and confirms with enter) the string s should be updated.
- You can assume that the main thread will not read or write to this string at the time of the update.
- Next calls to ShowStringWindow add more similar windows (or more widgets to existing window if that is not-too-difficult to implement).
- It should work on linux/ubuntu.
I would be very grateful for a working code, but that is not necessary to be useful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要使用 Qt 执行此操作,您需要:
多变的。
线。 (在第一次调用时)
在 QApplication 上设置为 false。 (在第一次调用时)
“exec”函数返回后,需要:
QApplication::instance()->quit();
有几种方法可以从主线程更新数据。
一种方法是使用具有排队连接类型的信号/槽。 UI 线程使用它来更新该值。减轻对主线程的影响。但是,请注意,如果值更新率太高,例如每秒 2000 次,您可能需要更改为轮询该值。为此,您可能会发现 QTimer 很有帮助。
将数据更新到主线程应该更容易一些,只需锁定互斥锁并插入值即可。此时,您可以检查我们之前从主线程获取的值是否仍然是主线程中的当前值。您确实声明它不会改变,但我不相信这一点:)
我希望这已经足够了。我们使用它为控制台程序创建基于 Qt 的错误对话框。
To do this with Qt, you need to:
variable.
thread. (on the first call)
to false on the QApplication. (on the first call)
After the "exec" function returns, you need to:
QApplication::instance()->quit();
There are a few ways you can update data from the main thread.
One way would be to use signals/slots with queued connection type. Using this, the UI thread updates the value. Easing the impact on the main thread. However, note that if the value update rate is too high, like 2000 times per second, you might want to change to polling the value. To do that, you might find QTimer helpful.
Updating data to the main thread should be somewhat easier, just lock the mutex and insert the value. At this point, you could check if the value that we previously got from the main thread is still the current value in the main thread. You did state that it doesn't change, but i'd not trust that :)
I hope this is sufficient. We used this to create a Qt based error dialog for a console program.