64,654
社区成员
发帖
与我相关
我的任务
分享
//MyClass.h
#include <QObject>
class MyClass : public QObject
{
Q_OBJECT
Q_INVOKABLE QString GetPath1();
Q_INVOKABLE QString GetPath2();
public:
MyClass();
private:
QString m_Pathdir;
QString m_Filename1;
};
//main.cpp
#include "myclass.h"
int main(int argc, char* argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<MyClass>("Qt.MyClass", 1, 0, "MyClass");
QQmlApplicationEngine engine;
engine.load(QStringLiteral("path.qml"));
return app.exec();
}
//path.qml
import Qt.MyClass 1.0
Window
{
id: root
visible: true
MyClass
{
id: myClass
}
property string pathdir: myClass.GetPath1()
property string fileName2: myClass.GetPath2()
}
// initialize MyClass m_Pathdir BY HOW?
// initialize MyClass m_Filename1 BY HOW?
qmlRegisterType<MyClass>("Qt.MyClass", 1, 0, "MyClass");
QQmlApplicationEngine engine;
engine.load(QStringLiteral("path.qml"));
逻辑之前被赋值?
#include "myclass.h"
MyClass::MyClass()
{
m_Pathdir = "file:///home/tt1/workspace/prd3/"; // not initialize here , how can I initialize it in main()?
m_Filename1 = "20170504102730_0.png"; // not initialize here , how can I initialize it in main()?
}
QString MyClass::GetPath1()
{
return m_Pathdir;
}
QString MyClass::GetPath2()
{
return m_Filename1;
}
//......
//MyClass.h
#include <QObject>
class MyClass : public QObject
{
Q_OBJECT
Q_INVOKABLE QString GetPath1();
Q_INVOKABLE QString GetPath2();
public:
MyClass();
private:
QString m_Pathdir;
QString m_Filename1;
};
class MyClass : public QObject
{
Q_OBJECT
Q_PROPERTY(QString mypath READ GetPath1 WRITE setMember3 )
public:
MyClass();
Q_INVOKABLE QString GetPath1();
Q_INVOKABLE QString GetPath2();
Q_INVOKABLE void setMember3( const QString& mem3 ); //This is Empty function
public slots:
void cppSlot(int number);
private:
QString m_Pathdir;
QString m_Filename1;
};