Qt 如何在值发生变化时从 int 获取信号?

Qt How can I get a signal from int when the value changes?

所以假设我是这样的 QDialog class

class DiagExample : public QDialog
{
    Q_OBJECT

public:
    DiagExample(QWidget *parent);

private:
    int myIntValue = 0;

    QPushButton *AddToValue;
    QPushButton *MinusToValue;
    QLabel *counter;

};

实现看起来像这样

DiagExample::DiagExample(QWidget *parent) : QDialog(parent)
{
    setWindowTitle("Example Diag");

    QVBoxLayout *layout = new QVBoxLayout(this);
   
    AddToValue = new QPushButton(tr("Add"));
    MinusToValue = new QPushButton(tr("Minus"));
    counter = new QLabel;

    connect(AddToValue, &QPushButton::released, [=](){myIntValue++;});
    connect(MinusToValue, &QPushButton::released, [=](){myIntValue--;});

    layout->addWidget(AddToValue);
    layout->addWidget(MinusToValue);
    layout->addWidget(counter);

    // below is what I want to achieve but have no idea how
    // essentially when value of the int is changed, text of
    // counter (QLabel) will be set to the new value

    connect(myIntValue, ???, [=](int newValue){ 
        counter->setText(QString::number(newValue);});
}

我知道我可以直接从 QPushButton::released -> QLabel 上的 setText 开始,但是我的代码最终会有很多输入进入计数器,所以从可读性和简单性的角度来看,我宁愿有这种范例 --- INPUT_WIDGET -> myIntValue -> setText(NEW VALUE OF myIntValue).

你必须自己定义它,类似的东西可以工作:

class DiagExample : public QDialog
{
    Q_OBJECT

public:
    DiagExample(QWidget *parent);

private:
    int myIntValue = 0;

    QPushButton *AddToValue;
    QPushButton *MinusToValue;
    QLabel *counter;

private slots:
    void onPlusOne();
    void onMinusOne();

signals:
    void intValueChanged(const QString& newVal);
};

然后在你的 cpp 文件中:

DiagExample::DiagExample(QWidget *parent) : QDialog(parent)
{
    setWindowTitle("Example Diag");

    QVBoxLayout *layout = new QVBoxLayout(this);
   
    AddToValue = new QPushButton(tr("Add"));
    MinusToValue = new QPushButton(tr("Minus"));
    counter = new QLabel;

    connect(AddToValue, &QPushButton::released, &DiagExample::onPlusOne);
    connect(MinusToValue, &QPushButton::released, &DiagExample::onMinusOne);
    connect(this, &DiagExample::intValueChanged, counter, &QLabel::setText);

    layout->addWidget(AddToValue);
    layout->addWidget(MinusToValue);
    layout->addWidget(counter);

    // below is what I want to achieve but have no idea how
    // essentially when value of the int is changed, text of
    // counter (QLabel) will be set to the new value

    connect(myIntValue, ???, [=](int newValue){ 
        counter->setText(QString::number(newValue);});
}

void DiagExample::onPlusOne()
{
    myIntValue++;
    emit intValueChanged(QString::number(myIntValue));
}

void DiagExample::onMinusOne()
{
    myIntValue--;
    emit intValueChanged(QString::number(myIntValue));
}

在 Qt 中执行此类操作的标准方法:

class DiagExample : public QDialog
{
    Q_OBJECT

    Q_PROPERTY(intValue READ intValue NOTIFY onIntValueChange)

public:
    DiagExample(QWidget *parent);

    int intValue() const;

signals:
    void onIntValueChange(int);

private:
    int myIntValue = 0;

    QPushButton *AddToValue;
    QPushButton *MinusToValue;
    QLabel *counter;
};

现在在 cpp 中:

DiagExample::DiagExample(QWidget *parent) : QDialog(parent)
{
    setWindowTitle("Example Diag");

    QVBoxLayout *layout = new QVBoxLayout(this);
   
    AddToValue = new QPushButton(tr("Add"));
    MinusToValue = new QPushButton(tr("Minus"));
    counter = new QLabel;

    connect(AddToValue, &QPushButton::released, [=](){
       myIntValue++;
       emit onIntValueChange(myIntValue);
    });
    connect(MinusToValue, &QPushButton::released, [=](){
       myIntValue--;
       emit onIntValueChange(myIntValue);
    });

    layout->addWidget(AddToValue);
    layout->addWidget(MinusToValue);
    layout->addWidget(counter);

    // below is what I want to achieve but have no idea how
    // essentially when value of the int is changed, text of
    // counter (QLabel) will be set to the new value

    connect(this, &DiagExample::onIntValueChange, [=](int newValue){ 
        counter->setText(QString::number(newValue);});
}

int DiagExample::intValue() const {
    return myIntValue;
}

注意只有QObject可以发出信号!