moveToThread()和start()适用于未构造的对象
moveToThread() and than start() works with not constructed object?
我有这样的东西:
main.cpp
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
static Communication *c = new Communication();
return a.exec();
}
communication.cpp
Communication::Communication()
{
client = new EchoClient(QUrl(QStringLiteral("ws://localhost:1234")), 0);
client->moveToThread(&t);
connect(&t, SIGNAL(started()), client, SLOT(proces()));
t.start(QThread::HighestPriority);
}
怎么可能,当我调用构造函数,对象还没有创建,但将在主循环的下一次迭代?我正在努力实现这一点,首先,对象将被创建,然后移动到线程,并将并行地监听消息,同时等待用户输入。
Thx
编辑
我只是想把客户端添加到另一个线程中因为在主线程中,我想等待这样的用户输入如果我稍微增强一下
Communication::Communication()
{
client = new EchoClient(QUrl(QStringLiteral("ws://localhost:1234")), 0);
client->moveToThread(&t);
connect(&t, SIGNAL(started()), client, SLOT(proces()));
t.start(QThread::HighestPriority);
std::cout << "n***************CLIENT MENU***************n";
std::cout << "(1): SHOW
std::cout << "(2): EXITn";
std::cout << "***************************************************n";
std::cin >> m_choice;
}
问题是它将显示菜单,它将被阻止,没有收到任何消息
控制台I/O阻塞:当cin >> m_choice
在主线程中等待用户输入时,主线程不能做任何其他事情。事件循环不会运行,主线程中没有插槽会运行,等等。
您可能希望将基于控制台I/O的菜单系统移动到专用线程中,并通过信号/插槽将其与系统的其余部分连接。
相关文章:
- FLTK 2.0构建和演示,适用于VS2019的2011年左右的代码库
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- 适用于 WebView2 旧版本的示例应用程序
- 在 NVIDIA GEFORCE GTX 1050 上下载适用于 Windows 10 的 openCL 1.2
- __attribute__(优化(0))) 是否适用于"recursively"?
- 为什么 std::erase(std::erase_if) 不是适用于<algorithm>任何容器的模板?
- 使用一个参数的模板函数时出错(适用于 2)
- 使用 适用于 Android 和 iOS 的 tf-lite C++ API
- 为什么这适用于 G++ 而不是 CLANG?
- 适用于 macOS 的 Xcode 应用程序。这就是我设置从USB麦克风输入获取音频的方式。一年前工作,现在没有了。为什么
- 适用于 Linux 的 c++ 上的代理脚本
- 为什么我的 SFINAE 表达式不再适用于 GCC 8.2?
- 使输出流式处理运算符适用于 boost::variant<std::vector<int>、int、double 的正确方法是什么>
- 有没有适用于Windows.lib文件的GNU二进制文件描述符(BFD)
- 如何使此函数适用于 100 个对象 (c++)?
- 构建共享对象 - 使用 Ninja 编译适用于 OSX 和 Windows 上的 Ubuntu Crush
- FlatBuffers C++从缓冲区转换为对象,仅适用于root_type(而Java具有所有)
- 关于"distinct addresses"的规则是否适用于 new 创建的对象?
- 多态是否适用于在C中传递的c++对象引用?