- html - 我的下拉菜单的内容关闭得太快
- c# - 使用 Html Agility Pack 从网页中的表中获取值而不使用 "SelectNode'
- html - 内容容器下的 CSS 下拉菜单
- html - 如何停止嵌套列表重叠父列表?
假设一个对象 obj
属于QThread T1
.最好在 Qhread T2
的函数,obj
无法从 T1
中“拉出”至 T2
.这是在 moveToThread()
中提到的文档:
Warning: This function is not thread-safe; the current thread must be same as the current thread affinity. In other words, this function can only "push" an object from the current thread to another thread, it cannot "pull" an object from any arbitrary thread to the current thread. There is one exception to this rule however: objects with no thread affinity can be "pulled" to the current thread.
moveToThread(nullptr)
将使一个对象可以从其他线程移动。
void FunctionRunningInT2 (QObject& obj) // `obj` belongs to thread `T1`
{
obj.moveToThread(nullptr); // line-1 no event processing for obj!?
obj.moveToThread(T2); // line-2 is it OK ???
}
signal
在
obj
上发出第 1 行和第 2 行之间?
obj.disconnect()
,之后它不接受任何信号。然而,在
disconnect()
之前未决的信号还在处理中。
moveToThread(nullptr)
是真的吗?还有?或者它也会丢弃待处理的信号?
最佳答案
据我了解,就是不能打电话moveToThread
在不同线程中的对象上。即使参数是 nullptr
,您最终会收到来自 Qt 的以下消息:
QObject::moveToThread: Current thread ( ... ) is not the object's thread ( ... )
Cannot move to target thread (0x0)
class Moveable : public QObject
{
Q_OBJECT
public:
Moveable() : QObject(nullptr) {}
public slots:
void moveMe(QThread * destination) { moveToThread(destination); }
class Mover : public QObject
{
Q_OBJECT
signals:
void moveIt(QThread *);
connect(&mover, &Mover::moveIt, &moveable, &Moveable::moveMe, Qt::QueuedConnection);
mover.moveIt(QThread::currentThread());
mover.moveIt(to_whatever_thread);
QMetaObject::invokeMethod(&moveable, "moveMe", Qt::QueuedConnection, Q_ARG(QThread*, destination_thread));
0x0
,如前所述
here :
no event processing for this object or its children can happen, as they are no longer associated with any thread.
0x0
的对象接收信号。 .
关于c++ - `moveToThread(nullptr)` 是从源线程拉出目标线程内的 QObject 的有效方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59675298/
将对象从一个线程移动到另一个线程时,moveToThread()自动将现有的直接连接重写为排队的连接? 或者换句话说:在调用 moveToThread() 之前或之后建立自动连接是否重要? ? 不幸的
我有以下类(class): class commMonitor : QObject { Q_OBJECT public: commMonitor(); ~commMonitor
是否可以将成员函数移动到新线程而不是整个类? 将其置于上下文中: class FooBar : public QObject { Q_OBJECT // some other
如果类型为 QObject 的对象被移动到一个线程 QObject::moveToThread ,对象接收到的所有信号都在该线程内处理。但是,如果直接调用插槽( object->theSlot() )
我有这样一个类: class myClass:public QThread 然后在它的构造函数中我做了: myClass::myClass(){ moveToThread(this);
我有一个类 Manager class Manager : public QThread { Q_OBJECT public: Manager(); void run(
我有一个用于 GUI 的主线程,其中运行 MainWindow 对象,在它的构造函数中,我创建了一个新的 worker 对象和一个 QThread 对象,然后我将 worker 移动到线程,问题是当打
在 QThread class 的文档中一个示例性设置的工作原理如下: public: Controller() { Worker *worker = new Worker; work
有一个类只有一个信号槽: #ifndef SOME_CLASS_H #define SOME_CLASS_H #include class SomeClass : public QObject {
我的IDE Qt 5.0.1,平台Linux 我对将小部件设置为窗口有疑问。(我的意见) 这是我的 main.cpp-> int main(int argc, char *argv[]) {
我搜索了这个站点和 QT 文档,但找不到以下问题的直接答案: 假设我有一个只有一个插槽的 worker 类: void Worker::testSlot(){ //access data and
我在 python 中使用 opencv2 和代码 import cv2 cv2.namedWindow("output", cv2.WINDOW_NORMAL) cv2.imshow("
我正在尝试使用线程创建程序:主要从一个循环开始。当测试返回 true 时,我创建一个对象并希望该对象在另一个线程中工作然后返回并开始测试。 QCoreApplication a(argc, argv)
我尝试使用来自 https://wiki.qt.io/QThreads_general_usage 的方法与 moveToThread。一切安好。但是,如果我尝试将参数添加到完成的信号中,则会出现以下
我有这样的东西: main.cpp int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); static
我刚刚开始在 Windows 上使用 Qt 并阅读了有关 moveToThread() 函数的信息。如果我这样写可以吗: class Worker : public QObject { Q_O
我试图在 Qt 中使用线程将一些工作委托(delegate)给线程,但我无法让它工作。我有一个继承 QMainWindow 的类,它有一个启动线程来工作的成员对象。该对象以 QMainwindow 作
我们什么时候在线程应用程序中使用每个函数调用。给定在同一个类中定义的两个函数 fun1() 和 fun2() 处理将数据读/写到缓冲区(队列操作)。实现多线程就这些。我们将不得不在单独的线程中运行这两
我刚开始使用多线程,在将对象移动到 QThread 时遇到了一些麻烦。我的 Controller 类实例化了 Worker,为 Worker 创建了必要的线程并将 Worker 移动到新线程。当工作线
我正在构建一个小型多线程 Web 服务器。 QTcpSockets 在主线程中获取,然后由 QtConcurrent 移交给 QThreadPool,后者最终处理数据并发出答案。 我的问题是套接字是在
我是一名优秀的程序员,十分优秀!