用C++11绑定信号和槽,能使代码可读性更高,灵活性更强
注:connect()中可声明连接类型,默认缺省为AutoConnection
点击滚动到 “连接类型” 介绍↓
#include
#include
class MyWindow : public QWidget
{
Q_OBJECT
public:
typedef std::function<void(bool checked)> fuc1;
MyWindow(QWidget *parent = 0);
~MyWindow();
QPushButton * btn;
QLabel* label; QVBoxLayout* Vbox;
QGridLayout* grid;
void click2(bool checked);
void set(QString s);
public slots:
void On_TestBtn_Cliked();
void set2(QString s);
};
MyWindow::MyWindow(QWidget *parent) : QWidget(parent) { label = new QLabel(QStringLiteral("你好")); btn = new QPushButton("Test"); grid = new QGridLayout(); grid->addWidget(btn,0,0); Vbox = new QVBoxLayout(); Vbox->addLayout(grid); Vbox->addWidget(label); setLayout(Vbox); fuc1 fu = std::bind(&MyWindow::click2, this, std::placeholders::_1); connect(btn, &QPushButton::clicked, this, fu); } MyWindow::~MyWindow() { } void MyWindow::set(QString s) { qDebug() <<"set:"<< QThread::currentThreadId(); label->setText(s); } void MyWindow::set2(QString s) { qDebug() << "set:" << QThread::currentThreadId(); label->setText(s); } void MyWindow::click2(bool checked) { qDebug() << "clcik2:"<new MyThread(this); th->start(); } void MyWindow::On_TestBtn_Cliked() { qDebug() << "click"; }
线程:
#pragma once
#include
#include
class MyThread : public QThread { Q_OBJECT public: QObject* window; void run(); MyThread(QObject* parent=NULL); ~MyThread(); signals: void print(QString); };
#include "MyThread.h"
#include "MainWindow.h"
MyThread::MyThread(QObject* parent):QThread(parent)
{
window = parent;
MyWindow* w = (MyWindow*)parent;
std::function<void(QString)> fu = std::bind(&MyWindow::set, w, std::placeholders::_1);
connect(this, &MyThread::print, w,fu);
}
MyThread::~MyThread()
{
}
void MyThread::run()
{
qDebug() << "Thread Begin";
int cout = 0;
while (true)
{
qDebug() << "MyThread::run:" << QThread::currentThreadId();
emit print(QString::number(cout));
Sleep(1);
cout++;
}
}
可以看到,print绑定的线程id和主线程相同,为线程安全
connect(this, &MyThread::print, w, [=](QString s) {
w->set(s);
qDebug() << "connet fuc:" << s << " " << QThread::currentThreadId();
//此时 线程id为主线程,可随意调用主线程对象的界面操作
});
void MyThread::run()
{
qDebug() << "Thread Begin";
int cout = 0;
while (true)
{
qDebug() << "MyThread::run:" << QThread::currentThreadId();
QMetaObject::invokeMethod(w, "set2", Q_ARG(QString, QString::number(cout)));
Sleep(1);
cout++;
}
}
MyThread::MyThread(QObject* parent):QThread(parent)
{
window = parent;
MyWindow* w = (MyWindow*)parent;
connect(this, &MyThread::print, w, std::bind(&QLabel::setText, w->label , std::placeholders::_1));
}
connect(this, &MyThread::print, w->label, [=](QString s)
{
//if(s.indexOf(xxxxx)) 此处可以对字符串进行过滤
qDebug() << "Bind Fuc:" << QThread::currentThreadId();
w->label->setText(s);
});
或者
connect(this, &MyThread::print, w, [=](QString s)
{
//if(s.indexOf(xxxxx)) 此处可以对字符串进行过滤
qDebug() << "Bind Fuc:" << QThread::currentThreadId();
w->label->setText(s);
});
修改界面中的槽set2为:
void MyWindow::set2(QString s, int pid)
{
if (pid ==(int)QThread::currentThreadId())
{
qDebug() << QStringLiteral(" 此类型的线程不在主线程中::" )<< s;
qDebug() << "id_1:" << pid << " id_2:" << QThread::currentThreadId();
}
}
线程:
void MyThread::run()
{
qDebug() << "Thread Begin";
int cout = 0;
int _pid;
while (true)
{
_pid = (int)QThread::currentThreadId();
qDebug() << "MyThread::run:" << _pid;
// emit print(QString::number(cout));
MyWindow* w = (MyWindow*)window;
QMetaObject::invokeMethod(w, "set2", Qt::ConnectionType::QueuedConnection,
Q_ARG(QString, "QueuedConnection"), Q_ARG(int, _pid)
);
QMetaObject::invokeMethod(w, "set2", Qt::ConnectionType::AutoConnection,
Q_ARG(QString,"AutoConnection"), Q_ARG(int, _pid)
);
QMetaObject::invokeMethod(w, "set2", Qt::ConnectionType::BlockingQueuedConnection,
Q_ARG(QString, "BlockingQueuedConnection"),Q_ARG(int, _pid)
);
QMetaObject::invokeMethod(w, "set2", Qt::ConnectionType::DirectConnection,
Q_ARG(QString, "DirectConnection"), Q_ARG(int, _pid)
);
QMetaObject::invokeMethod(w, "set2", Qt::ConnectionType::UniqueConnection,
Q_ARG(QString, "UniqueConnection"), Q_ARG(int, _pid)
);
Sleep(1);
cout++;
}
}
可见,DirectConnection连接类型的线程ID与主线程不同,与线程相同,不是线程安全,其他连接类型暂时无法找到方法测试,有例子的朋友可以跟我交流下,谢谢
是官方说明的连接类型,翻译
如果接收方住在线程发出信号,使用Qt::DirectConnection。否则,使用Qt::QueuedConnection。连接类型发送信号时决定。
解释:
如果接收方住在线程发出信号,使用Qt::DirectConnection。否则,使用Qt::QueuedConnection。连接类型发送信号时决定。
也就是说,自动判断,如果信号和槽在同一个线程,就调用Qt::DirectConnection,否则调用Qt::QueuedConnection
调用插槽立即发出信号时。槽是在信号线程中执行的。
可以理解为异步?
当槽发送给调用接收事件循环的线程时,槽在接收者的线程中执行。
也就是说,此连接类型,只管把信号发送到槽所在的线程事件中,不会等待槽所在的线程事件处理完毕
,槽所在线程事件循环当处理到此信号时,才会执行相应操作
可以理解为同步,阻塞当前线程直到同步?
当槽发送给调用接收事件循环的线程时,槽在接收者的线程中执行。
也就是说,此连接类型,不但把信号发送到槽所在的线程事件中,而且会等待槽所在的线程事件处理完毕
,槽所在线程事件循环当处理到此信号时,才会执行相应操作,信号所在的线程才会继续下一行代码
资料太少,不知道此类型的大概用途。。。
这是一个标志,可以结合上述任何一个连接类型,使用逐位或。当Qt:UniqueConnection,QObject:connect()将会失败如果连接已经存在(即如果相同的信号已经连接到同一个槽同一双对象)。这个标志是在Qt 4.6中引入的。