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 use classes in Qt / QML
Forum Update on Monday, May 27th 2025

How to use classes in Qt / QML

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 3 Posters 2.7k 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.
  • H Offline
    H Offline
    hejkki
    wrote on last edited by
    #1

    How can i use "kakkanna" in qml?
    i cannot seem to get it working even after reading the docs since they seem to look a lot different.
    I see nowhere anything like this what i'm trying to do...

    when i click on the button, it will give me "qrc:///main.qml:32: ReferenceError: kusihousu is not defined"

    i tried several methods like this in main.cpp:
    @
    engine.rootContext()->setContextProperty("kusihousu", kakkanna);
    @

    here is my code:

    main.cpp:
    @#include <QApplication>
    #include <QQmlApplicationEngine>
    #include <QObject>
    #include "kakkanna.h"

    int main(int argc, char argv[])
    {
    Kakkanna
    kannakka = new Kakkanna();

    QApplication app(argc, argv);
    
    QQmlApplicationEngine engine;
    
    
    engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    
    
    return app.exec();
    

    }
    @

    kakkanna.h:
    @
    #ifndef KAKKANNA_H
    #define KAKKANNA_H

    #include <QObject>

    class Kakkanna : public QObject
    {
    Q_OBJECT
    public:
    explicit Kakkanna(QObject *parent = 0);
    int value() const {return m_value;}

    signals:
    void valueChanged(int newValue);

    public slots:
    void setValue(int value);
    void increment();
    void decrement();

    private:
    int m_value;
    };

    #endif // KAKKANNA_H
    @

    kakkanna.cpp:
    @
    #include "kakkanna.h"

    Kakkanna::Kakkanna(QObject *parent) :
    QObject(parent)
    {
    m_value = 0;
    }

    void Kakkanna::setValue(int newValue)
    {
    if (newValue != m_value)
    {
    m_value = newValue;
    emit valueChanged(newValue);

    }
    

    }

    void Kakkanna::increment()
    {
    setValue(value()+1);
    }

    void Kakkanna::decrement()
    {
    setValue(value()-1);
    }
    @

    main.qml:
    @
    import QtQuick 2.2
    import QtQuick.Controls 1.1

    ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    menuBar: MenuBar {
        Menu {
            title: qsTr("File")
            MenuItem {
                text: qsTr("Exit")
                onTriggered: Qt.quit();
            }
        }
    }
    
    Text {
        text: qsTr("Hello World")
        anchors.verticalCenterOffset: 1
        anchors.horizontalCenterOffset: 0
        anchors.centerIn: parent
    }
    
    Button {
        id: button1
        x: 185
        y: 116
        text: qsTr("Button")
        onClicked: kusihousu.increment();
    }
    

    }
    @

    1 Reply Last reply
    0
    • shavS Offline
      shavS Offline
      shav
      wrote on last edited by
      #2

      Hi,

      You need add to your kakkanna.h line with this code:
      @
      QML_DECLARE_TYPE(Kakkanna)
      @

      Than add the line to you main.cpp file before load the main QML file next code:
      @
      mlRegisterType<Kakkanna>("com.kakkanna", 1, 0, "Kakkanna");
      @

      In main QML file you need add line on the top of file:
      @
      import com.kakkanna 1.0
      @

      And now you can create and use your class in QML file:
      @
      Kakkanna {
      id: someIdHere
      }
      @

      Also you can read about QML plugins "here":http://qt-project.org/doc/qt-5/qtqml-modules-cppplugins.html.

      Mac OS and iOS Developer

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

        You can also do

        @
        engine.rootContext()->setContextProperty("kusihousu", kakkanna);
        @

        as you were doing, just do it before loading your qml
        @
        engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
        @

        Then from qml you can access the Q_PROPERTYs, Q_INVOKABLE methods or public slots with kusihousu.NAME

        1 Reply Last reply
        1
        • H Offline
          H Offline
          hejkki
          wrote on last edited by
          #4

          this site isn't working, i just posted a reply and i never saw it online. ok trying another time:

          None of this is working.
          @
          engine.rootContext()->setContextProperty("kusihousu", kakkanna);
          @

          This will give me:
          error: invalid use of incomplete type 'class QQmlContext'

          @
          QML_DECLARE_TYPE(Kakkanna)
          @

          This will give me:
          error: expected constructor, destructor, or type conversion before '(' token

          @
          mlRegisterType<Kakkanna>("com.kakkanna", 1, 0, "Kakkanna");
          @

          This will give me:
          error: 'mlRegisterType' was not declared in this scope

          i figured out this could be:

          @
          mlRegisterType<Kakkanna>("com.kakkanna", 1, 0, "Kakkanna");
          @

          This will give me:
          error: 'qmlRegisterType' was not declared in this scope

          1 Reply Last reply
          0
          • I Offline
            I Offline
            Iktwo
            wrote on last edited by
            #5

            Just include QQmlContext

            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