Qt C++11 lambda:这个连接(信号槽)是否正确?
Qt C++11 lambda: Is this connect (signal-slot) correct?
我在 Windows7.
上使用 Qt5(启用 C++11)
在我的应用程序中,我有这样的东西:
connect(ui->alarm, &QCheckBox::stateChanged, [this]{
(ui->alarm->isChecked()) ? m_timer.start() : m_timer.stop();
});
其中 alarm
是 QCheckBox
,m_timer
是 QTimer
。
我想 start/stop 根据闹钟复选框的状态,即时计时。
我测试过,它似乎有效,但我不确定它是否 100% 好......或者是否有更好的 lambda 来做?
connect(ui->alarm, &QCheckBox::stateChanged, [this](int state){
state ? m_timer.start() : m_timer.stop();
});
这样你就不需要参考ui->alarm
。
在QCheckBox::stateChanged(int state)
中,state
确实是一个
enum Qt::CheckState
Qt::Unchecked = 0 The item is unchecked.
Qt::PartiallyChecked = 1 The item is partially checked. Items in hierarchical models may be partially checked if some, but not all, of their children are checked.
Qt::Checked = 2 The item is checked.
我在 Windows7.
上使用 Qt5(启用 C++11)
在我的应用程序中,我有这样的东西:
connect(ui->alarm, &QCheckBox::stateChanged, [this]{
(ui->alarm->isChecked()) ? m_timer.start() : m_timer.stop();
});
其中 alarm
是 QCheckBox
,m_timer
是 QTimer
。
我想 start/stop 根据闹钟复选框的状态,即时计时。
我测试过,它似乎有效,但我不确定它是否 100% 好......或者是否有更好的 lambda 来做?
connect(ui->alarm, &QCheckBox::stateChanged, [this](int state){
state ? m_timer.start() : m_timer.stop();
});
这样你就不需要参考ui->alarm
。
在QCheckBox::stateChanged(int state)
中,state
确实是一个
enum Qt::CheckState
Qt::Unchecked = 0 The item is unchecked.
Qt::PartiallyChecked = 1 The item is partially checked. Items in hierarchical models may be partially checked if some, but not all, of their children are checked.
Qt::Checked = 2 The item is checked.