QT QML文本属性更新

问题描述 投票:0回答:2

我想在主函数启动时更新qml text.text属性。 timeoutupdateqml的文本值不能更新?

main.cppqml_test.init:当使用qml_test.init时,当qml的文本属性不能更新时,这是主函数。

 int main(int argc, char *argv[])
   {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);


    qmlRegisterType<qml_test>("Test", 1, 0, "Pagetest");


    qDebug() << " main thread =" << QThread::currentThread();

    qml_test qt;
    qt.init();


    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
    }

qml_test.hQml_test:qmlRegisterType使用的是qmlRegisterType。

class qml_test : public QObject
{
    Q_OBJECT
public:
    Q_PROPERTY(QString mainvoltage READ mainvoltage WRITE setmainvoltage NOTIFY mainvoltageChanged);
    explicit qml_test(QObject *parent = 0);
    ~qml_test();

    QString mainvoltage();
    void init();

signals:
    void mainvoltageChanged();

public slots:
    void setmainvoltage(QString str);
    void update();
    void timeoutupdate();

private:
    QString m_mainvoltage;
    QTimer *tt;

};  

test.cpp

qml_test::qml_test(QObject *parent): QObject(parent)
{

}

qml_test::~qml_test()
{
}

void qml_test::init()
{
    tt = new QTimer(this);
    connect(tt, SIGNAL(timeout()), this, SLOT(timeoutupdate()));
    tt->start(5000);
}

void qml_test::update()
{
   qDebug() << QThread::currentThread();
   setmainvoltage("update");
}

void qml_test::timeoutupdate()
{

    setmainvoltage("fffffff");
    //this can not use

}

QString qml_test::mainvoltage()
{
    return  m_mainvoltage;
}


void qml_test::setmainvoltage(QString str)
{

    if(m_mainvoltage == str) return;

    m_mainvoltage = str;
    emit mainvoltageChanged();
    qDebug() << QThread::currentThread();
}

main.qml

ApplicationWindow {
    id:root
    visible: true
    width: 1216
    height: 480
    title: qsTr("Hello World")

    Item
    {
        id: center_item
        width: 1216;height: 103
        Loader
        {
            //id:mainloader
            sourceComponent: center_item
            source:"qrc:/show.qml"
         }
    }

}

show.qml

Item {
    id: center_main_show
    width: 1216;height: 103

    property string name01 : "name01"


    Pagetest{
        id:pp


    }

    Image {
        id: name
        source: "qrc:/images/bottom.jpg"
    }

    Button{
        width: 100
        height: 30
        id:btnn

        text: "dddddd"
        objectName: "qml_obj_btn"
        onClicked: {
            pp.update();
        }
    }
    Item {
        id: e_method_item
        width: 76
        height: 77
        anchors.top: center_main_show
        anchors.topMargin: 70
        anchors.left: center_main_show.left
        anchors.leftMargin: 100

        Text {
            id: tt            
            anchors.top: e_method_item.top
            anchors.topMargin: 47
            anchors.left: e_method_item.left
            anchors.leftMargin: 20
            text: pp.mainvoltage
        }
    }
c++ qt qml notify
2个回答
0
投票

你给自己创建了两个实例 PageTest,一个在main.cpp中实例化,另一个在QML中实例化。

你可能想从main中设置一个上下文属性,暴露出上下文中的 PageTest 实例,使用标识符'pp'。

QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("pp", &qt);

0
投票

正如@Amfasis所说 PageTest 在QML中实例化的(pp)与 qml_test 实例化在.cpp中(qt). 所以 pp 从来没有 init() 叫。只有 qt 的。这就是为什么定时器永远不会触发更新。

如果你想在.cpp中创建实例,那么就按照@Amfasis的建议来做,并暴露出 qt 到一个上下文属性中的QML代码。然后你可以删除 PageTest 对象。

另一方面,如果你喜欢用QML创建对象,那么你需要暴露你的 init() 函数到QML中,就像这样。

class qml_test : public QObject
{
    ...
    Q_INVOKABLE void init();

然后像这样从QML中调用它。

    Pagetest{
        id:pp

        Component.onCompleted: {
            init();
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.