How to use classes in Qt / QML



  • 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();
    }
    

    }
    @



  • 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.



  • 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



  • 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



  • Just include QQmlContext


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.