将复选框信号连接到 qt 小部件中的方法

connecting a checkbox signal to a method in qt widget

比如我有三个文件 generalsetting.ui、generalsetting.h、generalsetting.cpp .

我想在单击 UI 文件中的 Notifycheckbox 时触发 enableNotification 方法。 通过将复选框的 return 值传递给函数

我试过使用

connect(ui->notifyCheckBox, &QCheckBox::toggled, this, enableNotification(&QCheckBox::toggled));

connect(ui->notifyCheckBox, &QCheckBox::isChecked, this, enableNotification(&QCheckBox::toggled));

没用

这里是 header 和来源

namespace Ui {
class GeneralSettings;
}

class GeneralSettings : public QWidget
{
    Q_OBJECT

public:
    explicit GeneralSettings(QWidget *parent = nullptr);
    ~GeneralSettings() override;

private slots:
    void enableNotification(bool enable);

private:
    Ui::GeneralSettings *ui;
    Utils *utils;
};

源文件

GeneralSettings::GeneralSettings(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GeneralSettings)
{
    ui->setupUi(this); 
    //here is where i am adding the connect function.
}

void GeneralSettings::enableNotification(bool enable)
{
    utils->settings->setValue("General/notify", enable);
}

PS:我只收录了我觉得有用的

您可以连接到 lambda:

connect(ui->yyy, &QCheckBox::toggled, [this]()
    {
        qDebug() << "sd.." << ui->yyy->isChecked();
        enableNotification(ui->yyy->isChecked());
    });

或直接,与信号相同的方式

connect(ui->yyy, &QCheckBox::toggled,this, &Foo::enableNotification);