表盘上的 QT 信号文档

QT Signal Documentation on Dial

我正在努力解决这个问题,我可以找到任何解决方案。所以我有 QML

的代码
Dial {
    id: volumeDial
    property int speedValue: volumeDial.value.toFixed(0)
    objectName: "speedDial"
    from: 0
    value: 42
    to: 100
    stepSize: 1
    Layout.alignment: Qt.AlignHCenter
    Layout.minimumWidth: 64
    Layout.minimumHeight: 64
    Layout.preferredWidth: 128
    Layout.preferredHeight: 128
    Layout.maximumWidth: 128
    Layout.maximumHeight: 128
    Layout.fillHeight: true
    Label {
        text: volumeDial.value.toFixed(0)
        horizontalAlignment: Text.AlignLeft
        fontSizeMode: Text.FixedSize
        color: "white"
        font.pixelSize: Qt.application.font.pixelSize * 3
        anchors.centerIn: parent
    }

我的问题是我想将 volumeDial.value 的值存储在一个 int 变量中,然后在我移动拨盘以使用新的 volumeDial.value 更新 int 变量等之后。 这是我的 .cpp 代码:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QSettings>
#include <QQuickStyle>
#include <QIcon>
#include <QDebug>
#include <QQmlProperty>
int main(int argc, char *argv[])
{
    QGuiApplication::setApplicationName("Automotive");
    QGuiApplication::setOrganizationName("QtProject");
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);

    QGuiApplication app(argc, argv);

    QIcon::setThemeName("automotive");

    QQmlApplicationEngine engine;
    engine.load(QUrl("qrc:/qml/automotive.qml"));
    if (engine.rootObjects().isEmpty())
        return -1;
    QObject *rootObject = engine.rootObjects().first();
    QObject *qmlObject = rootObject->findChild<QObject*>("speedDial");
  if(qmlObject)
  {
//      qDebug() << "Speed value:" << QQmlProperty::read(qmlObject, "speedValue").toInt();
       qDebug()<< qmlObject->property("value").toInt();
  }

    return app.exec();
}

现在我可以从 value.volumeDial 中获取初始值,但我想在我从表盘上释放左键单击后获取每个值。在 Dial Signal Documentation 中有一个名为 moved() 的函数,但我不知道如何使用它,而且我也不知道该函数是否能解决我的问题。我恳请您向我展示一些可以解决我的问题的代码。

在您当前的代码中,您有一段代码可能会导致错误,因为您不应从 C++ 访问 QML 元素,反之亦然,将 C++ 对象注入 QML,此外,它的错误是您只会得到起始值,您将不会收到更改通知。

一般来说,如果您想从 C++ 监控 QML 属性,那么您必须创建一个 QObject,其中包含您要监控的相同类型的数据 Q_PROPERTY,然后使用 setContextProperty 导出它并在QML,那么只需要监控QProperty即可。

main.cpp

#include <QGuiApplication>
#include <QIcon>
#include <QQmlApplicationEngine>
#include <QQmlContext>

#include <QDebug>

class DialObserver: public QObject{
    Q_OBJECT
    Q_PROPERTY(int speed READ speed WRITE setSpeed NOTIFY speedChanged)
    int m_speed;
public:
    using QObject::QObject;
    int speed() const{
        return m_speed;
    }
public slots:
    void setSpeed(int speed){
        if (m_speed == speed)
            return;
        m_speed = speed;
        emit speedChanged(m_speed);
    }
signals:
    void speedChanged(int speed);
};

int main(int argc, char *argv[])
{
    QGuiApplication::setApplicationName("Automotive");
    QGuiApplication::setOrganizationName("QtProject");
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);

    QGuiApplication app(argc, argv);

    QIcon::setThemeName("automotive");

    DialObserver dial_observer;

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("dial_observer", &dial_observer);
    QObject::connect(&dial_observer, &DialObserver::speedChanged, [](int speed){
        qDebug() << speed;
    });
    engine.load(QUrl("qrc:/qml/automotive.qml"));
    if (engine.rootObjects().isEmpty())
        return -1;
    return app.exec();
}

#include "main.moc"

automotive.qml

// ...
Dial {
    id: volumeDial
    <b>onValueChanged: dial_observer.speed = value.toFixed(0)</b>
    from: 0
    value: 42
    to: 100
    // ...