Qt - connect的多种写法

转载自:https://blog.csdn.net/liukang325

加:在main函数中写一个定时器,并启动

1 QTimer timer;
2 QObject::connect(&timer, &QTimer::timeout, [&]() {
3     qDebug() << "Time COOKOO *** write: " << QDateTime::currentDateTime().toString("ss.zzz");   //秒.毫秒
4 });
5 timer.start(500);

 

1、最基本的,4个参数

1 //阻塞
2 QEventLoop loop;
3 QObject::connect(this, SIGNAL(getRtmpCodeFinished()), &loop, SLOT(quit()));
4 loop.exec();

 

1 QPointer<QAction> sysTrayReplayBuffer;
2 ...
3 connect(sysTrayReplayBuffer.data(), &QAction::triggered, this, &HBasic::ReplayBufferClicked);

 

2、lamda表达式

注:

[a,&b] a变量以值的方式呗捕获,b以引用的方式被捕获;
[this] 以值的方式捕获 this 指针;
[&] 以引用的方式捕获所有的外部自动变量(包括this);
[=] 以值的方式捕获所有的外部自动变量(包括this);
[] 不捕获外部的任何变量。

1 HLiveSettings liveSetting(this);
2 auto getRtmpCodeSlots = [&](QString rtmpStr) {
3     QDBG << rtmpStr;
4     ...
5 };
6 connect(&liveSetting, &HLiveSettings::getRtmpCode, getRtmpCodeSlots);

 

1 auto clickedSlots = [this]() {
2     ...
3 };
4 connect(ui->pushbutton, &QPushButton::clicked, clickedSlots);

 

1 m_downLoadManagerForImage = new DownLoadManager(this);
2 connect(m_downLoadManagerForImage, &DownLoadManager::FileDownloadFinished, [=]()
3 {
4     QDBG << "FileDownloadFinished!";
5     addPendantSource("test");
6 });

 

posted @ 2020-11-09 11:47  Citrusliu  阅读(847)  评论(0编辑  收藏  举报