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 });

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM