1
回答
qt的槽手动触发和通过信号触发有什么不同吗?

一个两个线程通信的程序,有点不懂。

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    Dummy c;

    MyObject d;
    MyObject d2;
    QThread b;
    QThread b2;
    d.moveToThread(&b);
    d2.moveToThread(&b2);

    //QObject::connect(&c,SIGNAL(sig()),&d,SLOT(begin()));
    d.begin();
    
    QObject::connect(&d,SIGNAL(sendData(QString)),&d2,SLOT(receiveData(QString)),Qt::QueuedConnection);

    b.start();
    b2.start();
    //c.emitsig();

    return a.exec();
}



线程d把数据传给d2,

如果写成

d.begin()

窗口会阻塞,但是我已经把d移到了单独的线程啊?

如果是

QObject::connect(&c,SIGNAL(sig()),&d,SLOT(begin()));
就不会有问题。

是因为connect默认使用了?

Qt::QueuedConnection
#ifndef MYOBJECT_H
#define MYOBJECT_H

#include <QObject>
#include <QDebug>

class MyObject : public QObject
{
    Q_OBJECT
public:
    explicit MyObject(QObject *parent = 0);
    
signals:
    void sendData(QString s);
public slots:
    void begin();
    void receiveData(QString s);

};

class Dummy:public QObject
{
    Q_OBJECT
public:
    Dummy(QObject* parent=0):QObject(parent) {}
public slots:
    void emitsig()
    {
        emit sig();
    }
signals:
    void sig();
};

#endif // MYOBJECT_H



#include "myobject.h"

MyObject::MyObject(QObject *parent) :
    QObject(parent)
{
}

void MyObject::begin()
{
     QString c="i am data";
     while(1){
         emit sendData(c);
     }

}

void MyObject::receiveData(QString s){
     qDebug()<<s;
}





举报
thinking空
发帖于4年前 1回/580阅
顶部