和定时器搭配
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout,this, [=]()mutable {n += 1;qDebug()<<n<<endl;});
timer->start(1000);
和按钮信号搭配
clicked是带参数的信号
connect(&b, &QPushButton::clicked, [=] (bool value) { qDebug() << value; } );
用宏包成员信号和槽
connect(m_pBtn,SIGNAL(sigClicked(bool)),this,SLOT(onClicked(bool)));
无需参数的写法:(没有重载用第一个,如果有重载,用第2个)
1、connect(m_pBtn,&MyButton:: sigClicked,this,&Widget:: onClicked);
2、connect(m_pBtn, static_cast<void (MyButton:: *)(bool)>(&MyButton:: sigClicked), this, &Widget:: onClicked);或者connect(m_pBtn, QOverload<bool>:: of(&MyButton:: sigClicked),this,&Widget:: onClicked);
connect(m_pBtn, QOverload<bool>:: of(&MyButton:: sigClicked), [=](bool check){undefined /* do something.. */ });
设置第5个参数的用法: auto type = static_cast<Qt::ConnectionType>(Qt::UniqueConnection|Qt:: QueuedConnection); auto func = [=] () {qDebug("Hello from lambda slot");};
w.connect(&w, &QPushButton::clicked, &a, func, type);
disconnect: QObject::disconnect(lineEdit,&QLineEdit::textChanged,label, &QLabel::setText); disconnect(myObject,0, myReceiver,0); disconnect(myObject,&MyObject::mySignal(),0,0); disconnect(myObject,0,0,0);
|