zoukankan      html  css  js  c++  java
  • Qt实现多线程下的信号与槽通讯

    初学QT,前期因为信号与槽只能在QT界面上面方便的使用,没有想到只要继承QObject便能使用且支持多线程操作。

    为了能够让后台自定义类能够使用信号与槽,首先在自定义类继承QObject

    1.DayouTraderSpi.h

    #include "qobject.h"
    class DayouTraderSpi : public QObject,public CThostFtdcTraderSpi
    {
    
    //宏必须申明
    Q_OBJECT
    public: DayouTraderSpi(CThostFtdcTraderApi* api) :pUserApi(api){}; ~DayouTraderSpi(); signals:
    //自定义的信号
    void signal_add_int(QString); };

    2.DayouTraderSpi.cpp

    DayouTraderSpi::~DayouTraderSpi()
    {
        //析构函数必须申明
    }
    
    //自定义函数中触发信号
    void DayouTraderSpi::ReqQryTradingAccount()
    {

    emit signal_add_int("ok");
    }

    然后在界面定义槽函数及链接信号与槽
    1.DayouOption.h

    class DayouOption : public QMainWindow
    {
        Q_OBJECT
    public:
        DayouOption(QWidget *parent = 0);
        ~DayouOption();
    private slots:
    
        void set_lineEdit_text(QString);//自定义槽函数
    };

    2.DayouOption.cpp

    DayouOption::DayouOption(QWidget *parent)
    : QMainWindow(parent),
    ui(new Ui::DayouOptionClass)
    {
        ui->setupUi(this);
                        MdApi = CThostFtdcMdApi::CreateFtdcMdApi();
        dayouMDSpi = new DayouMDSpi(MdApi);//创建回调处理类对象
        TraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi();
        dayouTraderSpi = new DayouTraderSpi(TraderApi);
    //链接信号与槽 dayouTraderSpi指针必须是QObject类型,所以dayouTraderSpi必须继承QObject。Qt::QueuedConnection是链接方式的一种。
    connect(dayouTraderSpi, SIGNAL(signal_add_int(QString)), this, SLOT(set_lineEdit_text(QString)), Qt::QueuedConnection); } DayouOption::~DayouOption() { delete ui; } //槽函数实现
    void DayouOption::set_lineEdit_text(QString str) { QMessageBox::warning(this, "查询成功", str); }

    传递消息的方式有四个取值:

    Qt::DirectConnection When emitted, the signal is immediately delivered to the slot. 假设当前有4个slot连接到QPushButton::clicked(bool),当按钮被按下时,QT就把这4个slot按连接的时间顺序调用一遍。显然这种方式不能跨线程(传递消息)。

    Qt::QueuedConnection When emitted, the signal is queued until the event loop is able to deliver it to the slot. 假设当前有4个slot连接到QPushButton::clicked(bool),当按钮被按下时,QT就把这个signal包装成一个 QEvent,放到消息队列里。QApplication::exec()或者线程的QThread::exec()会从消息队列里取消息,然后调用 signal关联的几个slot。这种方式既可以在线程内传递消息,也可以跨线程传递消息。

    Qt::BlockingQueuedConnection Same as QueuedConnection, except that the current thread blocks until the slot has been delivered. This connection type should only be used for receivers in a different thread. Note that misuse of this type can lead to dead locks in your application. 与Qt::QueuedConnection类似,但是会阻塞等到关联的slot都被执行。这里出现了阻塞这个词,说明它是专门用来多线程间传递消息的。

    Qt::AutoConnection If the signal is emitted from the thread in which the receiving object lives, the slot is invoked directly, as with Qt::DirectConnection; otherwise the signal is queued, as with Qt::QueuedConnection. 这种连接类型根据signal和slot是否在同一个线程里自动选择Qt::DirectConnection或Qt::QueuedConnection

  • 相关阅读:
    linq语法2 GLenn
    sql 拼接字符串 GLenn
    每日算法 20130225 GLenn
    linq语法1 GLenn
    每日算法 20130227 GLenn
    每日算法 20130226 GLenn
    jquery ajax 分页 GLenn
    PhoneGap应用开发对策:如何通过苹果审核?
    Xcode 4.1/4.2/4.3/4.4/4.5 + iOS 5.1.1免证书(iDP)开发+真机调试+生成IPA全攻略
    rails 散乱记录
  • 原文地址:https://www.cnblogs.com/dada-911911/p/3711456.html
Copyright © 2011-2022 走看看