Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #ifndef UFILE_H
  2. #define UFILE_H
  3.  
  4. #include <QObject>
  5. #include <QQuickItem>
  6. #include <QQuickWindow>
  7.  
  8. typedef unsigned char Byte;
  9. class UFile :public QObject
  10. {
  11. Q_OBJECT
  12. Q_PROPERTY(QByteArray data READ data NOTIFY dataChanged)
  13. Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged)
  14.  
  15. public:
  16. explicit UFile(QObject *parent = nullptr);
  17. QByteArray data() const;
  18. QString path() const;
  19. void setPath(const QString &path);
  20. Q_INVOKABLE void readFile(const QString & filePath);
  21. Q_INVOKABLE Byte * toByte();
  22. signals:
  23. void dataChanged(QByteArray data);
  24. void pathChanged(QString path);
  25. private:
  26. QByteArray mData;
  27. QString mPath;
  28. };
  29. void registerFileTypes();
  30.  
  31. #endif // UFILE_H
  32.  
  33. #include "ufile.h"
  34. #include <QFile>
  35. UFile::UFile(QObject* p)
  36. {
  37.  
  38. }
  39. QString UFile::path() const{
  40. return mPath;
  41. }
  42. void UFile::setPath(const QString &path){
  43. if(mPath == path) return;
  44. mPath = path;
  45. Q_EMIT pathChanged(mPath);
  46. readFile(mPath);
  47. }
  48. void UFile::readFile(const QString &filePath){
  49. QFile file(filePath);
  50. if (!file.open(QIODevice::ReadOnly)) return;
  51. QByteArray blob = file.readAll();
  52. mData = blob;
  53. Q_EMIT dataChanged(mData);
  54. }
  55. Byte* UFile::toByte(){
  56. Byte * pByte = reinterpret_cast<Byte*>(mData.data());
  57. return pByte;
  58. }
  59. void registerFileTypes(){
  60. qmlRegisterType<UFile>("UFile", 1, 0, "File");
  61. }
  62.  
  63. File{
  64. path: "file:///C:/Users/Administrator/Documents/file.exe"
  65. onPathChanged: {
  66. console.log(path)
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement