title: Loading From Byte Array To Image In Qml
body: <p>i created a c++ class in Qt to read files,and convert them to byte array so i can store them in database ,<br>
  how can i deal with byte array data for example setting image source in qml ,what is the correct way of converting this byte array to related types in qml  </p>
  
  <p>here is my class:</p>
  
  <pre><code>#ifndef UFILE_H
  #define UFILE_H
  
  #include &lt;QObject&gt;
  #include &lt;QQuickItem&gt;
  class UFile : public QObject
  {
      Q_OBJECT
      Q_PROPERTY(QByteArray data READ data NOTIFY dataChanged)
      Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged)
      Q_PROPERTY(int length READ length)
  public:
      explicit UFile(QObject *parent = nullptr);
      QByteArray data() const;
      QString path() const;
      int length() const;
      void setPath(QString &amp;path);
      void readFile(QString &amp;filePath);
  signals:
      void dataChanged(QByteArray data);
      void pathChanged(QString path);
  public slots:
  private :
      QByteArray _data;
      QString _path;
  };
  
  #endif // UFILE_H
  </code></pre>
  
  <p>cpp : </p>
  
  <pre><code>#include "ufile.h"
  #include &lt;QDir&gt;
  
  UFile::UFile(QObject *parent) : QObject(parent)
  {
  
  }
  QByteArray UFile::data() const{
      return _data;
  }
  QString UFile::path() const{
      return _path;
  }
  int UFile::length() const{
      int i =_data.count();
      return i;
  }
  void UFile::setPath(QString &amp;path){
      if(_path== path){
          return;
      }
      _path = path;
      readFile(_path);
      Q_EMIT pathChanged(_path);
  }
  
  void UFile::readFile(QString &amp;filePath){
      if(filePath!= ""){
          QString _fp = filePath;
          if(_fp.startsWith("file:///"))
              _fp = QUrl(filePath).toLocalFile();
          if(_fp!= ""){
              _fp = QDir::toNativeSeparators(_fp);
              QFile file(_fp);
              if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                  return ;
              QByteArray bytes;
              bytes = file.readAll();
              _data = bytes;
          }
      }
  }
  </code></pre>
  
  <p>and in qml :</p>
  
  <pre><code>UFile{
      id:testfile
      path: "C:/icon.ico"
      onPathChanged: {
          console.log(path)
      }
      Component.onCompleted: {
          console.log(length)
      }
  }
  </code></pre>
  
  <p>i tried to load QImage and set Image source in qml with this lines but it stuck </p>
  
  <pre class="lang-cpp prettyprint-override"><code>QImage img; 
  img.loadFromData(bytes);
  </code></pre>
  
  <p>and in qml:</p>
  
  <pre><code>image.source = result of img.loadFromData
  </code></pre>