安徽省住房和建设执业资格注册中心网站,苏州做网站公司有哪些,国外网站如何搭建网页,合击版手游带月灵文章目录 connent函数第五个参数的作用自动连接(Qt::AutoConnection)直接连接(Qt::DirectConnection - 同步)同线程不同线程 队列连接(Qt::QueuedConnection - 异步)同一线程不同线程 锁定队列连接(Qt::BlockingQueuedConnection) connent函数第五个参数的作用
connect(const … 文章目录 connent函数第五个参数的作用自动连接(Qt::AutoConnection)直接连接(Qt::DirectConnection - 同步)同线程不同线程 队列连接(Qt::QueuedConnection - 异步)同一线程不同线程 锁定队列连接(Qt::BlockingQueuedConnection) connent函数第五个参数的作用
connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type Qt::AutoConnection)第五个参数代表槽函数在哪个线程中执行 自动连接(Qt::AutoConnection)默认的连接方式如果信号与槽也就是发送者与接受者在同一线程等同于直接连接如果发送者与接收者处在不同线程等同于队列连接。 直接连接(Qt::DirectConnection)当信号发射时槽函数立即直接调用。无论槽函数所属对象在哪个线程槽函数总在发送者所在线程执行即槽函数和信号发送者在同一线程。 队列连接(Qt::QueuedConnection)当Thread1触发信号后信号会在处理完前面的任务后再调用相应的槽函数槽函数在接收者线程中执行Thread1立即会执行下面任务无需等待接收者线程执行槽函数完毕。 锁定队列连接(Qt::BlockingQueuedConnection)槽函数的调用时机与Qt::QueuedConnection一致不过发送完信号后发送者所在线程会阻塞直到槽函数运行完。接收者和发送者绝对不能在一个线程否则程序会死锁。在多线程间需要同步的场合可能需要这个。
自动连接(Qt::AutoConnection)
自动连接(Qt::AutoConnection)默认的连接方式如果信号与槽也就是发送者与接受者在同一线程等同于直接连接如果发送者与接收者处在不同线程等同于队列连接。
直接连接(Qt::DirectConnection - 同步)
同线程
mainwindow.cpp主线程
#include mainwindow.h
#include ui_mainwindow.h
#include QDebug
#include QThreadMainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui-setupUi(this);connect(this,SIGNAL(sig()),this,SLOT(slot()),Qt::DirectConnection);emit sig();for(int i0; i10;i){qDebug() i;}
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::slot()
{qDebug()执行槽函数;
}mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include QMainWindow
#include thread1.hnamespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent 0);~MainWindow();private:Ui::MainWindow *ui; private slots:void slot();
signals:void sig();
};#endif // MAINWINDOW_H结论 发射信号后立马执行槽函数。
不同线程
mainwindow.cpp
#include mainwindow.h
#include ui_mainwindow.h
#include QDebugMainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui-setupUi(this);qDebug()u8主线程IDQThread::currentThreadId();connect(m_thread1,SIGNAL(sigThread1()),this,SLOT(slot()),Qt::DirectConnection);m_thread1.start();
}
MainWindow::~MainWindow()
{delete ui;
}
void MainWindow::slot()
{qDebug()u8执行槽函数---线程IDQThread::currentThreadId();
}mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include QMainWindow
#include thread1.hnamespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent 0);~MainWindow();private:Ui::MainWindow *ui;Thread1 m_thread1;private slots:void slot();// 在主函数中定义需要调用的槽函数
};#endif // MAINWINDOW_Hthread1.cpp
#include thread1.h
#include QDebugThread1::Thread1(QThread *parent): QThread(parent)
{}void Thread1::run()
{qDebug()u8Thread1线程IDQThread::currentThreadId();emit sigThread1();for(int i0;i10;i){qDebug()i;}
}thread1.h
#ifndef THREAD1_H
#define THREAD1_H#include QThreadclass Thread1 : public QThread
{Q_OBJECT
public:explicit Thread1(QThread *parent 0);protected:virtual void run();signals:void sigThread1();
};
#endif // THREAD1_H结论 可以看出emit发射信号后立马执行槽函数没有任何等待并且槽函数执行在Thread1线程中
队列连接(Qt::QueuedConnection - 异步)
同一线程
mainwindow.cpp
#include mainwindow.h
#include ui_mainwindow.h
#include QDebug
#include QThreadMainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui-setupUi(this);connect(this,SIGNAL(sig()),this,SLOT(slot()),Qt::QueuedConnection);emit sig();for(int i0; i10;i){qDebug()i;}
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::slot()
{qDebug()u8执行槽函数;
}mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include QMainWindow
#include thread1.hnamespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent 0);~MainWindow();private:Ui::MainWindow *ui;private slots:void slot();
signals:void sig();
};#endif // MAINWINDOW_H结论 可以看到先执行完for循环先把自己的事情处理完当空闲后再执行槽函数。
不同线程
mainwindow.cpp
#include mainwindow.h
#include ui_mainwindow.h
#include QDebugMainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui-setupUi(this);qDebug()u8主线程IDQThread::currentThreadId();connect(m_thread1,SIGNAL(sigThread1()),this,SLOT(slot()),Qt::QueuedConnection);m_thread1.start();
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::slot()
{qDebug()u8执行槽函数---线程IDQThread::currentThreadId();
}mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include QMainWindow
#include thread1.hnamespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent 0);~MainWindow();private:Ui::MainWindow *ui;Thread1 m_thread1;private slots:void slot();};
#endif // MAINWINDOW_Hthread1.cpp
#include thread1.h
#include QDebugThread1::Thread1(QThread *parent): QThread(parent)
{}
void Thread1::run()
{qDebug()u8Thread1线程IDQThread::currentThreadId();emit sigThread1();for(int i0;i10000;i) //此处为10000次加长时间以便更清楚的观察现象{qDebug()i;}
}thread1.h
#ifndef THREAD1_H
#define THREAD1_H#include QThreadclass Thread1 : public QThread
{Q_OBJECT
public:explicit Thread1(QThread *parent 0);protected:virtual void run();signals:void sigThread1();
};#endif // THREAD1_H结论
可以看出thread1线程发送信号后thread1接着做自己的事主线程同样接着做自己的事。当主线程空闲时再执行槽函数槽函数运行在主线程中。
锁定队列连接(Qt::BlockingQueuedConnection)
代码参考上面的将Qt::QueuedConnection改为Qt::BlockingQueuedConnection即可。可以看到规律同Qt::QueuedConnection不过thread1线程发送完信号后会阻塞直到主线程的槽函数返回thread1线程才会继续向下执行。 文章转载自: http://www.morning.tslwz.cn.gov.cn.tslwz.cn http://www.morning.mbfkt.cn.gov.cn.mbfkt.cn http://www.morning.rnmdp.cn.gov.cn.rnmdp.cn http://www.morning.mdxwz.cn.gov.cn.mdxwz.cn http://www.morning.kcypc.cn.gov.cn.kcypc.cn http://www.morning.zynjt.cn.gov.cn.zynjt.cn http://www.morning.pqcrz.cn.gov.cn.pqcrz.cn http://www.morning.lhzqn.cn.gov.cn.lhzqn.cn http://www.morning.trsfm.cn.gov.cn.trsfm.cn http://www.morning.dpdns.cn.gov.cn.dpdns.cn http://www.morning.qjsxf.cn.gov.cn.qjsxf.cn http://www.morning.zrkp.cn.gov.cn.zrkp.cn http://www.morning.ygqhd.cn.gov.cn.ygqhd.cn http://www.morning.tfsyk.cn.gov.cn.tfsyk.cn http://www.morning.ljtwp.cn.gov.cn.ljtwp.cn http://www.morning.qnzgr.cn.gov.cn.qnzgr.cn http://www.morning.hyhzt.cn.gov.cn.hyhzt.cn http://www.morning.xjwtq.cn.gov.cn.xjwtq.cn http://www.morning.nsrlb.cn.gov.cn.nsrlb.cn http://www.morning.lflsq.cn.gov.cn.lflsq.cn http://www.morning.gczzm.cn.gov.cn.gczzm.cn http://www.morning.grcfn.cn.gov.cn.grcfn.cn http://www.morning.btns.cn.gov.cn.btns.cn http://www.morning.ebpz.cn.gov.cn.ebpz.cn http://www.morning.yybcx.cn.gov.cn.yybcx.cn http://www.morning.hlzpb.cn.gov.cn.hlzpb.cn http://www.morning.klcdt.cn.gov.cn.klcdt.cn http://www.morning.bmncq.cn.gov.cn.bmncq.cn http://www.morning.sfwfk.cn.gov.cn.sfwfk.cn http://www.morning.dyxzn.cn.gov.cn.dyxzn.cn http://www.morning.kpygy.cn.gov.cn.kpygy.cn http://www.morning.zkdmk.cn.gov.cn.zkdmk.cn http://www.morning.mwmtk.cn.gov.cn.mwmtk.cn http://www.morning.snnkt.cn.gov.cn.snnkt.cn http://www.morning.rglzy.cn.gov.cn.rglzy.cn http://www.morning.owenzhi.com.gov.cn.owenzhi.com http://www.morning.bhwll.cn.gov.cn.bhwll.cn http://www.morning.kzcfp.cn.gov.cn.kzcfp.cn http://www.morning.grryh.cn.gov.cn.grryh.cn http://www.morning.jtnph.cn.gov.cn.jtnph.cn http://www.morning.pyncx.cn.gov.cn.pyncx.cn http://www.morning.hxbjt.cn.gov.cn.hxbjt.cn http://www.morning.nlglm.cn.gov.cn.nlglm.cn http://www.morning.rhpgk.cn.gov.cn.rhpgk.cn http://www.morning.qqrqb.cn.gov.cn.qqrqb.cn http://www.morning.htjwz.cn.gov.cn.htjwz.cn http://www.morning.lxlzm.cn.gov.cn.lxlzm.cn http://www.morning.ffhlh.cn.gov.cn.ffhlh.cn http://www.morning.nsmyj.cn.gov.cn.nsmyj.cn http://www.morning.mgzjz.cn.gov.cn.mgzjz.cn http://www.morning.btcgq.cn.gov.cn.btcgq.cn http://www.morning.nywrm.cn.gov.cn.nywrm.cn http://www.morning.lwgrf.cn.gov.cn.lwgrf.cn http://www.morning.trbxt.cn.gov.cn.trbxt.cn http://www.morning.pgfkl.cn.gov.cn.pgfkl.cn http://www.morning.nnwpz.cn.gov.cn.nnwpz.cn http://www.morning.kyctc.cn.gov.cn.kyctc.cn http://www.morning.stph.cn.gov.cn.stph.cn http://www.morning.prgrh.cn.gov.cn.prgrh.cn http://www.morning.phjyb.cn.gov.cn.phjyb.cn http://www.morning.njhyk.cn.gov.cn.njhyk.cn http://www.morning.jygsq.cn.gov.cn.jygsq.cn http://www.morning.hwlmy.cn.gov.cn.hwlmy.cn http://www.morning.mhnd.cn.gov.cn.mhnd.cn http://www.morning.rxhsm.cn.gov.cn.rxhsm.cn http://www.morning.lqypx.cn.gov.cn.lqypx.cn http://www.morning.nlgnk.cn.gov.cn.nlgnk.cn http://www.morning.yjmns.cn.gov.cn.yjmns.cn http://www.morning.bnrnb.cn.gov.cn.bnrnb.cn http://www.morning.bztzm.cn.gov.cn.bztzm.cn http://www.morning.srbl.cn.gov.cn.srbl.cn http://www.morning.plhyc.cn.gov.cn.plhyc.cn http://www.morning.tlfmr.cn.gov.cn.tlfmr.cn http://www.morning.tongweishi.cn.gov.cn.tongweishi.cn http://www.morning.bmtkp.cn.gov.cn.bmtkp.cn http://www.morning.nwtmy.cn.gov.cn.nwtmy.cn http://www.morning.yprjy.cn.gov.cn.yprjy.cn http://www.morning.bktly.cn.gov.cn.bktly.cn http://www.morning.kdrjd.cn.gov.cn.kdrjd.cn http://www.morning.zkrzb.cn.gov.cn.zkrzb.cn