创建自定义对象访问对象的变量数据
第一种方式不推荐,我在高版本的Qt编辑器并不好用,建议使用第二种,不看第一种方式。
方式一 全局对象,上下文对象的方式访问C++端的数据(不推荐建议使用第二种方式)
pro文件需要修改:
QT += quick
# --------------------添加代码在这里-----------------------
CONFIG += qmltypes
#模块名称
QML_IMPORT_NAME = Myobj
#版本号
QML_IMPORT_MAJOR_VERSION = 1
# -------------------结束---------------------------------
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
myobject.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
DISTFILES +=
HEADERS += \
myobject.h
创建MyObject类
myobject.h
这里有个QML_ELEMENT宏,因为我们的类是一个复杂的类型,如果你看过我的第一篇并不能像我第一篇的那样直接给一个具体的数值,所以得加上这个宏和pro文件的配置,高版本的Qt我并没有查看到这个宏,估计是被弃用了,建议用第二种方式
#ifndef MYOBJECT_H
#define MYOBJECT_H
#include <QObject>
#include <QtQml>
class MyObject : public QObject
{
Q_OBJECT
QML_ELEMENT
public:
explicit MyObject(QObject *parent = nullptr);
//单例
static MyObject * getInstance();
int ivalue() const;
void setIvalue(int newIvalue);
const QString &sString() const;
void setSString(const QString &newSString);
private:
int m_ivalue;
QString m_sString;
Q_PROPERTY(int ivalue READ ivalue WRITE setIvalue NOTIFY ivalueChanged)
Q_PROPERTY(QString sString READ sString WRITE setSString NOTIFY sStringChanged)
signals:
void ivalueChanged();
void sStringChanged();
};
#endif // MYOBJECT_H
myobject.cpp文件
#include "myobject.h"
MyObject::MyObject(QObject *parent)
: QObject{parent}
{
}
MyObject *MyObject::getInstance()
{
static MyObject * obj = new MyObject();
return obj;
}
int MyObject::ivalue() const
{
return m_ivalue;
}
void MyObject::setIvalue(int newIvalue)
{
if (m_ivalue == newIvalue)
return;
m_ivalue = newIvalue;
emit ivalueChanged();
}
const QString &MyObject::sString() const
{
return m_sString;
}
void MyObject::setSString(const QString &newSString)
{
if (m_sString == newSString)
return;
m_sString = newSString;
emit sStringChanged();
}
mian.cpp文件代码:
这里是使用了一个简单的单例(跟贴近实际工作情况)去获取这个类对象,你也可以直接MyObject obj对象出来
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "myobject.h"
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
//全局对象,上下文对象
QQmlContext * context = engine.rootContext();
context->setContextProperty("MyObject",MyObject::getInstance());
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
这样就可以在QML端调用这个这个对象了
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import Myobj 1.0
Window {
id:window
width: 980
height: 680
visible: true
title: qsTr("信号测试程序软件")
MyObject {
}
}
方式二 qmlRegisterType注册方式
myobject.h文件和myobject.cpp文件一样,pro文件可以去掉添加的代码部分。
qmlRegisterType<类名>(
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "myobject.h"
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
qmlRegisterType<MyObject>("MyObj", 1, 0,"MyObject");
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
main.qml文件
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import MyObj 1.0
Window {
id:window
width: 980
height: 680
visible: true
title: qsTr("信号测试程序软件")
Button {
width:100
height: 50
onClicked: {
console.log(ivalue)
}
}
MyObject {
ivalue: 10
sString: "张三"
}
}