Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef UFILE_H
- #define UFILE_H
- #include <QObject>
- #include <QQuickItem>
- #include <QQuickWindow>
- typedef unsigned char Byte;
- class UFile :public QObject
- {
- Q_OBJECT
- Q_PROPERTY(QByteArray data READ data NOTIFY dataChanged)
- Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged)
- public:
- explicit UFile(QObject *parent = nullptr);
- QByteArray data() const;
- QString path() const;
- void setPath(const QString &path);
- Q_INVOKABLE void readFile(const QString & filePath);
- Q_INVOKABLE Byte * toByte();
- signals:
- void dataChanged(QByteArray data);
- void pathChanged(QString path);
- private:
- QByteArray mData;
- QString mPath;
- };
- void registerFileTypes();
- #endif // UFILE_H
- #include "ufile.h"
- #include <QFile>
- UFile::UFile(QObject* p)
- {
- }
- QString UFile::path() const{
- return mPath;
- }
- void UFile::setPath(const QString &path){
- if(mPath == path) return;
- mPath = path;
- Q_EMIT pathChanged(mPath);
- readFile(mPath);
- }
- void UFile::readFile(const QString &filePath){
- QFile file(filePath);
- if (!file.open(QIODevice::ReadOnly)) return;
- QByteArray blob = file.readAll();
- mData = blob;
- Q_EMIT dataChanged(mData);
- }
- Byte* UFile::toByte(){
- Byte * pByte = reinterpret_cast<Byte*>(mData.data());
- return pByte;
- }
- void registerFileTypes(){
- qmlRegisterType<UFile>("UFile", 1, 0, "File");
- }
- File{
- path: "file:///C:/Users/Administrator/Documents/file.exe"
- onPathChanged: {
- console.log(path)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement