Answer the question
In order to leave comments, you need to log in
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();
};
#include "settings.h"
QColor ApplicationSettings::bColor()
{
return Qt::red;
}
int ApplicationSettings::num()
{
return 50;
}
qmlRegisterType<ApplicationSettings>("ApplicationSettings", 1, 0, "ApplicationSettings");
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
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question