A
A
Anton Misyagin2020-03-21 14:42:03
Qt
Anton Misyagin, 2020-03-21 14:42:03

How to take parameter from C++ to QML on QT?

Good afternoon, I am writing the first application on Qt. The task is to read the ini file and pass the value to the interface part.
Link to my project
To begin with, we omit reading the ini file, and simply pass the parameter from c ++ to qml
The ApplicationSettings class inherited from QObject is created, which shares two parameters:
settings.h

#include <QObject>
#include <QColor>
class ApplicationSettings : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QColor backgroundColor READ bColor)
    Q_PROPERTY(int justNum READ num)
public:
    QColor bColor();
    int num();
};

settings.cpp
#include "settings.h"
QColor ApplicationSettings::bColor()
{
    return Qt::red;
}
int ApplicationSettings::num()
{
    return 50;
}

Registering a new type:
main.cpp
qmlRegisterType<ApplicationSettings>("ApplicationSettings", 1, 0, "ApplicationSettings");

And then I read the field values ​​from qml:
mode1.qml
import QtQuick 2.9
import QtQuick.Controls 2.2
import ApplicationSettings 1.0

ApplicationWindow {
    ApplicationSettings {
        id: settings
    }

    title: qsTr("ncf05m")
    width: 600
    height: 1066
    color: settings.backgroundColor
    visible: true

    Text {
        id: element
        x: settings.num
        y: settings.num
        text: qsTr("Text")
        font.pixelSize: 12
    }
}

The application does not compile and errors are given:
ncf05m/settings.h:6: error: error: undefined reference to 'vtable for ApplicationSettings'
It is not clear what is required of me, help me take the first steps

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jacob E, 2020-03-24
@sunnmas

Add new classes through the QtCreator dialog so you don't forget to pull files into the project. You forgot to add a header, so nothing came up.
Personally, I prefer to pass parameters through https://doc.qt.io/qt-5/qtqml-cppintegration-contex...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question