Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. C++ use constants that defined in qml singleton file
QtWS25 Last Chance

C++ use constants that defined in qml singleton file

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 434 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DhanyaRaghav
    wrote on last edited by
    #1

    Hi,

    I have more than 100 constants that defined in qml and that file made singleton in qml. Now that I need some of the constant to be used in C++. How to get it?

    Thanks in advance

    ODБOïO 1 Reply Last reply
    0
    • D DhanyaRaghav

      Hi,

      I have more than 100 constants that defined in qml and that file made singleton in qml. Now that I need some of the constant to be used in C++. How to get it?

      Thanks in advance

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #2

      hi @DhanyaRaghav
      One way to do that would be to create a c++ method that takes the needed values as parameters.

      You will need to expose that class to QML to be able to call the method
      see here if you have Qt 5.7 and later https://doc.qt.io/qt-5/qtqml-cppintegration-topic.html
      or this article https://scythe-studio.com/en/blog/how-to-integrate-qml-and-c-expose-object-and-register-c-class-to-qml

      see also Overview - QML and C++ Integration

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DhanyaRaghav
        wrote on last edited by
        #3

        I was able to resolve
        //main.cpp
        I did using this lines in main.cpp
        QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/pathToThatQmlFile.qml")));
        QObject *object = component.create();
        C++ClassName::getInstance()->setObject(object);

        //C++
        As this qml file is singleton, in C++ class where constants need to be called,
        In constructor add:
        qmlRegisterSingletonType(QUrl(QStringLiteral("qrc:/pathToQmlFile.qml")), "com.Test.TestConstants", 1, 0, "TestConstants");

        void C++ClassName::setObject(QObject *newObject)
        {
        item = qobject_cast<QQuickItem *>(newObject);
        qDebug() << item->property("phi").toDouble);
        }

        //QML
        pragma Singleton

        import QtQuick 2.14

        Item
        {
        readonly property double phi: "3.142"
        }

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved