当变量在 QML 中更改其值时如何在 Qt 中执行函数?

How to execute a function in Qt when a variable changes its value in the QML?

error: no matching function for call to 'SimulationMode::connect(QString&, const char*, SimulationMode* const, const char*)'
QObject::connect (m_standingAgvID, SIGNAL (f(int)), this, SLOT (d(int)));

这里,m_standingAgvID是一个QString变量的对象,它也用在Q_PROPERTY.

f(int) 和 d(int) 已在 cpp 代码的相应信号和槽区域中定义和声明。

正在考虑:

QString does not emit signals. Perhaps you should tell us what you are trying to achieve. – cmannett85

QString itself is not an QObject, it cannot connect signals and slots. – Tay2510

我有一个简单的变量 int 类型,它是 class 成员,我已将其设为 Q_PROPERTY.

此变量将在 QML 中设置。当它的值改变时,我想在 Qt.

中调用一个信号

就这些了。

使用您关于 ints 的示例,可以这样完成:

class foo : public QObject
{
    Q_OBJECT
    Q_PROPERTY( int value READ getValue WRITE setValue NOTIFY valueChanged )
public:
    explicit foo( QObject* parent = nullptr ) :
        QObject{ parent }, i_{ 0 } {}
    virtual ~foo() {}

    int getValue() const { return i_; }
public slots:
    void setValue( int value )
    {
        if ( value != i_ ) {
            i_ = value;
            emit valueChanged( i_ );
        }
    }
signals:
    void valueChanged( int value );
private:
    int i_;
};

简而言之,您必须手动发出有关成员的更改通知信号。