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
Forum Updated to NodeBB v4.3 + New Features

C++ use constants that defined in qml singleton file

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 454 Views 1 Watching
  • 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 21 Aug 2022, 09:42 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

    O 1 Reply Last reply 21 Aug 2022, 22:02
    0
    • D DhanyaRaghav
      21 Aug 2022, 09:42

      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

      O Offline
      O Offline
      ODБOï
      wrote on 21 Aug 2022, 22:02 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 24 Aug 2022, 08:02 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

        3/3

        24 Aug 2022, 08:02

        • Login

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