qt 使用 qthread 发出的奇怪行为

发布于 2024-12-21 04:21:02 字数 1732 浏览 3 评论 0原文

struct C : public QObject
{
    Q_OBJECT

public:

    C()
    {
        qDebug()<<"C()";

        //connect(this,SIGNAL(cs()),this,SLOT(cl2()),Qt::QueuedConnection);
    }

    ~C(){qDebug()<<"~C()";}

signals:

    void cs();

public slots:

    void cl()
    {
        //it seems this signal will be emited only when the following loop finished
        //the output is :
        // cl2 ......... cl2 obj ..........obj
        // which means that the sub-thread not work simultaneously with the main thread
        // but if i move the bold line to cl2 function  and add 
        // connect(this,SIGNAL(cs()),this,SLOT(cl2()),Qt::QueuedConnection); to constructor
        // its output will be:
        // cl2 ... cl2 ..obj ..obj... cl2...obj...... 
        // which mean that main thread and sub-thread work concurrently.
        // any idea about it?  

        emit cs(); 
        for (int i=0;i<=1000000;++i)
            qDebug()<<"cl2";
    }

    void cl2()
    {
        //for (int i=0;i<=1000000;++i)
        //    qDebug()<<"cl2";
    }
};

struct Obj : public QObject
{
    Q_OBJECT

public:

    Obj(){qDebug()<<"Obj()";}

    ~Obj(){qDebug()<<"~Obj()";}

public slots:

    void ol()
    {
        for (int i=0;i<=10000000;++i)
            qDebug()<<"Obj";
    }
};

int main(int argc,char* argv[])
{
        QApplication app(argc, argv);

        QThread th;

        C * c=new C;

        Obj *o=new Obj;

        c->moveToThread(&th);

        th.start();

        QObject::connect(c,SIGNAL(cs()),o,SLOT(ol()),Qt::QueuedConnection);

        c->cl();

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

        app.exec();
}
struct C : public QObject
{
    Q_OBJECT

public:

    C()
    {
        qDebug()<<"C()";

        //connect(this,SIGNAL(cs()),this,SLOT(cl2()),Qt::QueuedConnection);
    }

    ~C(){qDebug()<<"~C()";}

signals:

    void cs();

public slots:

    void cl()
    {
        //it seems this signal will be emited only when the following loop finished
        //the output is :
        // cl2 ......... cl2 obj ..........obj
        // which means that the sub-thread not work simultaneously with the main thread
        // but if i move the bold line to cl2 function  and add 
        // connect(this,SIGNAL(cs()),this,SLOT(cl2()),Qt::QueuedConnection); to constructor
        // its output will be:
        // cl2 ... cl2 ..obj ..obj... cl2...obj...... 
        // which mean that main thread and sub-thread work concurrently.
        // any idea about it?  

        emit cs(); 
        for (int i=0;i<=1000000;++i)
            qDebug()<<"cl2";
    }

    void cl2()
    {
        //for (int i=0;i<=1000000;++i)
        //    qDebug()<<"cl2";
    }
};

struct Obj : public QObject
{
    Q_OBJECT

public:

    Obj(){qDebug()<<"Obj()";}

    ~Obj(){qDebug()<<"~Obj()";}

public slots:

    void ol()
    {
        for (int i=0;i<=10000000;++i)
            qDebug()<<"Obj";
    }
};

int main(int argc,char* argv[])
{
        QApplication app(argc, argv);

        QThread th;

        C * c=new C;

        Obj *o=new Obj;

        c->moveToThread(&th);

        th.start();

        QObject::connect(c,SIGNAL(cs()),o,SLOT(ol()),Qt::QueuedConnection);

        c->cl();

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

        app.exec();
}

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

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

发布评论

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

评论(1

初相遇 2024-12-28 04:21:02

这两个方法都在主线程中执行:
cl()在主线程中执行,因为它是从主线程调用的,ol()由于o在主线程中执行在主线程 && 队列连接

当您将代码更改为带注释的版本时,cl2() 的执行将移至th 线程。

Both methods are executed in main thread:
cl() is executed in main thread, because it is called from main thread, ol() is executed in the main thread due to o in the main thread && QueuedConnection.

When you change your code to version, that is commented, execution of cl2() moves to th thread.

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