关于 QThread 的问题

发布于 2024-10-18 07:19:36 字数 81 浏览 1 评论 0原文

如果我创建一个 QThread 并从另一个线程调用其插槽之一,那么它是在 QThread 对象的线程上下文中调用,还是从发出调用的线程上下文中调用?

If I make a QThread and call one of its slots from another thread, will it be called in the context of the thread of the QThread object, or from the context of the thread which made the call?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把昨日还给我 2024-10-25 07:19:36

如果您通过发出信号来执行插槽,那么这取决于您所拥有的信号到插槽连接的类型。通过直接连接连接到信号的槽将在发射器的线程中执行。通过排队连接连接的槽将在接收者的线程中执行。请参阅此处:http://doc.qt.nokia.com/4.7/threads -qobject.html

如果直接执行该槽,使用[QThread对象]->slot(),那么该槽将在进行调用的线程中执行。

If you execute the slot by emitting a signal, then it depends on the type of signal-to-slot connection you have. A slot connected to a signal via a direct connection would execute in the emitter's thread. A slot connected via a queued connection would execute in the receiver's thread. Please see here: http://doc.qt.nokia.com/4.7/threads-qobject.html

If the slot is executed directly, with [QThread object]->slot(), then the slot will execute in the thread that makes the call.

不奢求什么 2024-10-25 07:19:36

直接调用始终在调用线程的上下文中执行。

Direct calls are always executed in the context of the calling thread.

初熏 2024-10-25 07:19:36

由信号调用的槽将在与 QObject 关联的线程中运行。直接调用的槽将在当前线程中运行。这是一个演示的测试程序。

输出:

main() thread: QThread(0x804d470)
run() thread: Thread(0xbff7ed94)
onRunning() direct call; thread: Thread(0xbff7ed94)
onRunning() signaled; thread: QThread(0x804d470)

测试程序:

#include <QtCore>

class Thread : public QThread
{
    Q_OBJECT
public:

    void run()
    {
        qDebug() << "run() thread:" << QThread::currentThread();
        emit running();
    }

public slots:
    void onRunning()
    {
        qDebug() << "onRunning() thread:" << QThread::currentThread();
    }

signals:
    void running();
};

#include "threadTest.moc"

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    qDebug() << "main() thread:" << QThread::currentThread();

    Thread t;
    QObject::connect(&t, SIGNAL(running()), &t, SLOT(onRunning()));
    t.start();

    QTimer::singleShot(100, &app, SLOT(quit()));

    return app.exec();
}

A slot called by a signal will run in the thread for which the QObject is associated. A slot called directly will run in the current thread. Here is a test program which demonstrates.

Output:

main() thread: QThread(0x804d470)
run() thread: Thread(0xbff7ed94)
onRunning() direct call; thread: Thread(0xbff7ed94)
onRunning() signaled; thread: QThread(0x804d470)

Test program:

#include <QtCore>

class Thread : public QThread
{
    Q_OBJECT
public:

    void run()
    {
        qDebug() << "run() thread:" << QThread::currentThread();
        emit running();
    }

public slots:
    void onRunning()
    {
        qDebug() << "onRunning() thread:" << QThread::currentThread();
    }

signals:
    void running();
};

#include "threadTest.moc"

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    qDebug() << "main() thread:" << QThread::currentThread();

    Thread t;
    QObject::connect(&t, SIGNAL(running()), &t, SLOT(onRunning()));
    t.start();

    QTimer::singleShot(100, &app, SLOT(quit()));

    return app.exec();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文