进行网络相关编程时,需要使用信号和槽,碰到一个={}作为槽函数的语句,感到非常有意思。
connect(tcpSocket, &QTcpSocket::readyRead,
[=](){
QByteArray array = tcpSocket->readAll();
qDebug()<<array<<endl;
}
);
static(qmetaobject-connection.html) QObject::connect(const QObject *sender
, PointerToMemberFunction signal, Functor functor)
测试的简单例子
#ifndef CONNECTION_H
#define CONNECTION_H
#include <QObject>
#include<QDebug>
class Connection:public QObject
{
Q_OBJECT
public:
Connection();
void test_emit_signal();
public slots:
void test_slot();
signals:
void test_signal();
};
#endif
#include "connection.h"
Connection::Connection()
{
connect(this,&Connection::test_signal,[=](){
qDebug()<<"in slot"<<endl;
}
);
this->test_emit_signal();
}
void Connection::test_slot(){
qDebug()<<"signal received !"<<endl;
}
void Connection::test_emit_signal(){
emit test_signal();
}
#include <QCoreApplication>
#include"connection.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Connection *tmp=new Connection();
return a.exec();
}
- 值得注意的是用这种方式处理时,信号需要以指针形式书写
|