将 Qt Quick Cpp class 重构为 header 和 .cpp

Refactoring Qt Quick Cpp class into header and .cpp

我有一个 Cpp Class,我想将其重构为头文件和 .cpp 文件。通常没问题,但是当我尝试执行此 Qt Quick 时,我无法编译它。如果我将所有这些都放在头文件中就没问题,但否则我会收到各种不同的错误,具体取决于我尝试这样做的方式。有没有合适的方法。我认为这与 Q_INVOKABLE 位有关,但不确定。

这是我的代码...

#ifndef APPLICATIONDATA_H
#define APPLICATIONDATA_H

#include <QDateTime>
#include <QObject>

class ApplicationData : public QObject
{
    Q_OBJECT
public:
    ApplicationData(){}

    Q_INVOKABLE QDateTime getCurrentDateTime() const{
        return QDateTime::currentDateTime();
    }

};

#endif // APPLICATIONDATA_H

感谢您的指点。

这可以编译,但我不确定为什么会编译或为什么不会编译:

//header file

#ifndef APPLICATIONDATA_H
#define APPLICATIONDATA_H

#include <QDateTime>
#include <QObject>

class ApplicationData : public QObject
{
    Q_OBJECT
public:
    ApplicationData();  //constructor

    Q_INVOKABLE QDateTime getCurrentDateTime() const;  //function

};

#endif // APPLICATIONDATA_H


//.cpp file

#include "applicationdata.h"
#include <QDateTime>
#include <QObject>

ApplicationData::ApplicationData(){}  //constructor implementation

QDateTime ApplicationData::getCurrentDateTime() const{  //function implementation
    return QDateTime::currentDateTime();
}