简体   繁体   中英

Refactoring Qt Quick Cpp class into header and .cpp

I have a Cpp Class which I want to refactor into a header and .cpp file. No problem normally, but when I try to do this Qt Quick one I can't get it to compile. It's fine if I put all of it in the header file, but otherwise I get various different errors depending on how I try to do it. Is there a proper way. I think it has to do with the Q_INVOKABLE bit, but not sure.

Here is my code...

#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

Thanks for any pointers.

This compiles but I'm not sure why it does or why it didn't:

//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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM