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. Embedding C++ Objects into QML with Context Properties

Embedding C++ Objects into QML with Context Properties

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 595 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.
  • J Offline
    J Offline
    JennyAug13
    wrote on last edited by JennyAug13
    #1

    I am trying an example code and able to execute it successfully. But iam unable to see the result. Here is my program for your reference. I am a beginner in this field, hope some changes and explaination helps me to advance and understand.

    applicationdata.cpp file

    #include "applicationdata.h"
    
    ApplicationData::ApplicationData()
    {
    
    }
    
    Q_INVOKABLE QDateTime ApplicationData::getCurrentDateTime() const
    {
        return QDateTime::currentDateTime();
    }
    
    

    applicationdata.h file

    #ifndef APPLICATIONDATA_H
    #define APPLICATIONDATA_H
    #include <QDateTime>
    #include <QObject>
    
    class ApplicationData : public QObject
    {
        Q_OBJECT
    public:
        ApplicationData();
    public slots:
        Q_INVOKABLE QDateTime getCurrentDateTime() const;
    };
    
    #endif // APPLICATIONDATA_H
    

    and main.cpp file

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQuickView>
    #include "applicationdata.h"
    #include <QQmlContext>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
        QQuickView view;
    
        ApplicationData data;
        view.rootContext()->setContextProperty("applicationData",&data);
        view.setSource(QUrl::fromLocalFile("MyItem.qml"));
    
        view.show();
        return app.exec();
    }
    

    MyItem.qml file

    import QtQuick 2.0
    
    Text { text: applicationData.getCurrentDateTime() }
    
    

    I can see a blank output when i execute it instead i want to see current date and time.

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JennyAug13
      wrote on last edited by
      #2

      Yes, got it. Changed the path to

      view.setSource(QUrl(QStringLiteral("qrc:/MyItem.qml")));
      

      It is working.

      ODБOïO 1 Reply Last reply
      2
      • J JennyAug13

        Yes, got it. Changed the path to

        view.setSource(QUrl(QStringLiteral("qrc:/MyItem.qml")));
        

        It is working.

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

        @JennyAug13 hi, you also can use QML Date object

        J 1 Reply Last reply
        2
        • ODБOïO ODБOï

          @JennyAug13 hi, you also can use QML Date object

          J Offline
          J Offline
          JennyAug13
          wrote on last edited by
          #4

          @LeLev May i ask, how to do it with qml date object

          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by
            #5

            Window {
            visible: true
            width: 640
            height: 480
            title: qsTr("Hello World")
            property date mydate : new Date();
            Component.onCompleted: {
            print(mydate.toLocaleDateString())
            }
            }

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            1 Reply Last reply
            1

            • Login

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