【问题标题】:How to change the value of a property in QML from C++ class如何从 C++ 类更改 QML 中属性的值
【发布时间】:2012-09-18 16:55:35
【问题描述】:

我在简单的 Qt Quick 中有这样的代码。我想从我的类中更改 qml 元素的属性值。

myclass.h

#ifndef MYCLASS_H
#define MYCLASS_H
#include <QObject>
#include "qdebug.h"
#include <QtGui/QApplication>
#include <QDebug>
#include <QDeclarativeContext>
#include <QGraphicsObject>
#include <QVariant>

class myClass : public QObject
{
    Q_OBJECT

public:
    myClass(QObject *QMLObject) : m_QMLObject(QMLObject) {}

public slots:
   void cppSlot(int number);

protected:
    QObject *m_QMLObject;

};

#endif // MYCLASS_H

myclass.cpp

#include "myclass.h"

void myClass::cppSlot(int number) {
    qDebug() << "Called the C++ slot with" << number;
    QObject* textinput = m_QMLObject->findChild<QObject*>("textinput");

    QObject* memo = m_QMLObject->findChild<QObject*>("memo");

    QString str;

    str=(textinput->property("text")).toString();

    int a;
    a=str.toInt();
    a++;

    QString str2;
    str2=QString::number(a);

    memo->setProperty("text", str+"+1="+str2);
}

main.cpp

#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QUrl>
#include <QDebug>
#include <QDeclarativeContext>
#include <QGraphicsObject>
#include "myclass.h"

Q_DECL_EXPORT int main(int argc, char *argv[])
{
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    QmlApplicationViewer viewer;

    myClass MyClass(viewer.rootObject());


    viewer.rootContext()->setContextProperty("myObject", &MyClass);

    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setSource(QUrl("qrc:qml/qml/Example/main.qml"));


    viewer.showExpanded();

    return app->exec();
}

main.qml

import QtQuick 1.0

Rectangle {
    width: 300
    height: 300
    anchors.fill: parent


    Column {
        spacing: 5
        anchors.centerIn: parent;

       Rectangle {
           id: button

           width: 100
           height: 30

           color: "#e0b87b"

           Text {
               id: buttonLabel
               text: "Start"
               anchors.centerIn: parent;
           }

           MouseArea {
               anchors.fill: parent
               id: mouseArea

               onClicked: myObject.cppSlot(1);
           }
       }

           Rectangle {
               id: textinputRect 


               width: 100
               height: 18


               color: "#e0b87b"

               TextInput {
                   id: textinput
                   objectName: "textinput"
                   color: "#f51515";
                   selectionColor: "blue"
                   font.pixelSize: 12;
                   width: parent.width-4
                   anchors.centerIn: parent
                   focus: true
                   text:"1"
                   }
           }


           Rectangle {
               id: memoRect 


               width: 100
               height: 35


               color: "#00b87b"

               TextEdit{
                   id: memo
                   objectName: "memo"
                   wrapMode: TextEdit.Wrap
                   width:parent.width;
                   readOnly:true
               }
           }

    }

}

当我运行应用程序并单击按钮时,应用程序崩溃。我做错了什么?

【问题讨论】:

    标签: c++ qml qt-quick


    【解决方案1】:

    rootObject 还没有被实例化,试试这样:

    Q_DECL_EXPORT int main(int argc, char *argv[])
    {
        QScopedPointer<QApplication> app(createApplication(argc, argv));
        QmlApplicationViewer viewer;
        viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
        viewer.setSource(QUrl("qrc:qml/qml/Example/main.qml"));
        myClass MyClass(viewer.rootObject());
        viewer.rootContext()->setContextProperty("myObject", &MyClass);
        viewer.showExpanded();
        return app->exec();
    }
    

    这样 rootObject() 将指向正确的实例。

    【讨论】:

      【解决方案2】:

      m_QMLObject 为 NULL。在尝试将 rootObject() 传递给“myClass”对象的构造函数之前,您需要先从资源组件加载 QML,否则它将始终为 NULL。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-14
        • 2023-04-05
        • 2021-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-13
        相关资源
        最近更新 更多