将只有插槽的QGLWidget移动到不同的线程
Move QGLWidget with only slots to different thread
我有一个QT程序,它从Kinect接收QBytearray,然后在QGLWidget中将它们处理为屏幕上的可见图像。这个QGLWidget在GUI线程中运行,就像GUI的其他部分一样。激发Kinect图像的类在另一个线程中运行。所有的混合都是通过信号和时隙来完成的。
有时GUI线程会锁定,然后OpenGLWidget也会锁定,我想解决这个问题。要做到这一点,我需要一个QT插槽在不同的线程中运行,然后运行GUI的其余部分。我知道这是可能的,但我看到的所有例子都使用run方法,它在不同的线程中开始,然后自己运行。
但我使用插槽来接收图像,这意味着它不会一直运行,而是只有在有图像可供渲染的情况下。我可以创建一个线程并将这个槽放在线程中吗?
在最近的一个项目中,我通过剃掉一些牦牛来"解决"这个问题,呃,写一些路由包装器。
class FooClass : public QThread
{
Q_OBJECT
/* ... */
public slots:
/* to thread loopback signals */
void setBufferCount(
unsigned int buffercount ) {
QMetaObject::invokeMethod(this, "_priv_loslot_setBufferCount",
Qt::QueuedConnection,
Q_ARG(unsigned int, buffercount) );
}
private slots:
/*
* private loopback slots
* ^^^^ ^^ ^^^^
*
* These slots are connected to the coresponding frontend
* signals. Calling a frontend signal will send a invocation
* between the threads, calling the loopback slots within
* the worker thread.
*/
void _priv_loslot_setBufferCount(unsigned int buffercount);
/* ... */
}
通过调用或连接到公共插槽,通过在Qt::QueuedConnection上调用invokeMethod的内部包装实现了这一点。
您可以创建助手类并将逻辑移动到那里。所以Widget类将在主线程中,它应该发出信号,这些信号将在另一个线程中的helper类中处理。
相关文章:
- 如何检索指向在单独线程上运行的函数的移动指针?
- 从从可调用参数创建的线程对象参数移动构造 C++11 线程
- 标准::atomic_应该如何...<std::shared_ptr>用于线程安全类的复制和移动操作?
- 将函数移动到线程(使用 lambda)时内存泄漏
- Qt 通过将对象移动到线程来跨线程发出信号
- std::move在将std::string移动到另一个线程时出错
- 使用第二个线程快速更新 GUI 窗口会通过调整窗口大小或移动窗口而崩溃
- 线程安全unique_ptr移动
- 将执行从一个线程移动到另一个线程,以实现任务并行性并在将来调用
- 在 C++03 中将 std::string 移动到 boost::线程中
- 调用移动到Qt中不同线程的QObject函数?
- 在将对象移动到另一个线程后,不能简单地删除该对象
- 安全地移动在访问其他成员的成员线程中运行 lambda 的对象
- QThread - 使用 moveToThread 将类成员移动到线程
- 移动到线程会导致问题
- Qt 线程,来自 QObject 的析构函数的崩溃移动
- 为什么 c++ 线程是可移动的,但不可复制
- 锁定线程安全队列的移动构造函数的右值参数?
- std::线程移动构造函数
- 在Win32上,如何将线程移动到另一个CPU核心