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. How to Expose a Qt C++ Class to QML

How to Expose a Qt C++ Class to QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 4 Posters 704 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.
  • I Offline
    I Offline
    isan
    wrote on last edited by isan
    #1

    In QT I use this method to define a class in another class

    .h:

    #ifndef RealTime_H
    #define RealTime_H
    
    #include <QDialog>
    #include "randomwalk.h"
    
    
    class RealTime : public QDialog {
        Q_OBJECT
    public:
        RealTime(QWidget *parent = 0);
        ~RealTime();
    
         // RandomWalk is a thread class
         RandomWalk *dataSource;
    
    static void OnData(void *self, double elapsedTime);
    
    #endif
    

    .cpp:

    RealTime::RealTime(QWidget *parent) :
        QDialog(parent)
    {
     // Start the random data generator
        dataSource = new RandomWalk(OnData, this);
        dataSource->start();
    }
    

    Now in QML I register c++ class and set id in .qml file

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QtQml>
    #include <realtime.h>
    #include <randomwalk.h>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); 
        QGuiApplication app(argc, argv);
    
    
        qmlRegisterType <RealTime>("realt",1,0,"RealTime");
        qmlRegisterType<RandomWalk> ("thread",1,0,"RandomWalk");
    
    
        QQmlApplicationEngine engine;
    
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    

    .qml:

    import QtQuick 2.11
    import QtQuick.Controls 2.4
    import realt 1.0
    import thread 1.0
    Page {
        id: page
        width: screen.width
        height: screen.height
        property alias button1: button1
    
        RealTime{
            id:mRealtime
        }
        
        RandomWalk{
            id:mThread
        }
      Button {
            id: button
            x: -74
            y: -110
            text: qsTr("Config 2")
            anchors.verticalCenterOffset: -6
            anchors.horizontalCenterOffset: -24
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.verticalCenter
           // onClicked: 
        }
    

    How should I define
    dataSource = new RandomWalk(OnData, this);in qml ?

    1 Reply Last reply
    0
    • I Offline
      I Offline
      isdsi
      wrote on last edited by
      #2

      This may help you.
      https://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html

      1 Reply Last reply
      0
      • I Offline
        I Offline
        isan
        wrote on last edited by
        #3

        I know it creates when we define an object,
        RandomWalk{
        id:mThread
        }
        But how do I define an onData input?

        1 Reply Last reply
        0
        • GrecKoG Online
          GrecKoG Online
          GrecKo
          Qt Champions 2018
          wrote on last edited by
          #4

          What's an onData input? What do you want to do with it?

          Have you read this page https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html ?
          I'm not sure what you need but it might be a property, a signal or a slot.

          1 Reply Last reply
          1
          • johngodJ Offline
            johngodJ Offline
            johngod
            wrote on last edited by johngod
            #5

            In your RealTime class you need to expose the RandomWalk property to qml, using setters and getters and the macro Q_PROPERTY.
            Something like this:

            class RealTime : public QDialog {
                Q_OBJECT
            	Q_PROPERTY(RandomWalk walk READ randomWalk WRITE setRandomWalk /*NOTIFY randomWalkChanged*/)
            // in line above walk is the property name that gets exposed to qml
            	
            	RandomWalk randomWalk() { return *randomWalk;}
            public slots: 
            	void setRandomWalk(RandomWalk randomWalk_){ dataSource = new RandomWalk(randomWalk_);} 
            	.......
            	RandomWalk *dataSource;
                   .........
            

            Then in qml you can do:

            RandomWalk{
                id: mThread
            }
            	
            RealTime{
                id: mRealtime
            	walk: mThread
            }
            

            Note that I havent tested the code above, so you may need to do some tunning, but hopefully you get the ideia :)

            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