Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to add image from database to my listmodel.
Forum Updated to NodeBB v4.3 + New Features

How to add image from database to my listmodel.

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 2 Posters 462 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Subuday
    wrote on last edited by
    #1

    Hi, I have a list of mentors and I want to add their avatars to list. But I don't know how to load from database images and display them. Also, I couldn't find how to send image from database to qml.

    Sorry, if if here's the obvious solution, I'm new

    Here my list model

    #ifndef MENTORLISTMODEL_H
    #define MENTORLISTMODEL_H
    
    #include <QAbstractListModel>
    #include <QStringList>
    #include "mentor.h"
    
    class MentorListModel: public QAbstractListModel
    {
        Q_OBJECT
    
    public:
        enum Roles {
           NameRole = Qt::UserRole + 1,
           AboutRole,
           LangRole,
        };
    
        MentorListModel(QObject *parent = 0);
    
        virtual int rowCount(const QModelIndex &parent) const;
        virtual QVariant data(const QModelIndex &index, int role) const;
        virtual QHash<int, QByteArray> roleNames() const;
    
        Q_INVOKABLE void add(const Mentor &unit);
        void clear();
    
    private:
        QList<Mentor> m_data;
    };
    
    #endif // MENTORLISTMODEL_H
    
    #include "mentorlistmodel.h"
    
    MentorListModel::MentorListModel(QObject *parent):
        QAbstractListModel(parent)
    {
    
    }
    
    
    int MentorListModel::rowCount(const QModelIndex &parent) const
    {
        if (parent.isValid()) {
            return 0;
        }
    
        return m_data.size();
    }
    
    QVariant MentorListModel::data(const QModelIndex &index, int role) const
    {
        if (!index.isValid()) {
            return QVariant();
        }
    
        switch (role) {
        case NameRole:
            return m_data.at(index.row()).firstName + " " + m_data.at(index.row()).lastName;
        case AboutRole:
            return m_data.at(index.row()).about;
        case LangRole: {
            QString langs;
            QStringList::const_iterator it;
            for (it = m_data.at(index.row()).langs.begin(); it != m_data.at(index.row()).langs.end(); ++it) {
                langs += *it + " ";
            }
            return langs;
        }
        default:
            return QVariant();
        }
    }
    
    QHash<int, QByteArray> MentorListModel::roleNames() const
    {
        QHash<int, QByteArray> roles = QAbstractListModel::roleNames();
        roles[NameRole] = "name";
        roles[AboutRole] = "about";
        roles[LangRole] = "langs";
    
        return roles;
    }
    
    void MentorListModel::add(const Mentor &unit)
    {
        beginInsertRows(QModelIndex(), m_data.size(), m_data.size());
        m_data.append(unit);
        endInsertRows();
    }
    
    void MentorListModel::clear()
    {
        beginRemoveRows(QModelIndex(), 0, m_data.size());
        endRemoveRows();
        m_data.clear();
    }
    
        ListView {
             id: mentorsList
             model: filterModel
             height: 400
             width: 300
             anchors.top: searchField.bottom
             spacing: 5
             cacheBuffer: 100
    
            delegate: Rectangle {
                 width: parent.width
                 radius: 5
                 anchors.horizontalCenter: parent.horizontalCenter
                 height: 200
                 color: Qt.lighter("#6bdce4", 0.8)
                 Text {
                     id: nameTxt
                     text: name
                     font.pointSize: 12
                     color: "#FFFFFF"
                     anchors.left: parent.left
                     anchors.leftMargin: 20
                     anchors.verticalCenter: parent.verticalCenter
                 }
    
                 Text {
                     id: aboutTxt
                     text: about
                     anchors.top: nameTxt.bottom
                 }
    
                 Text {
                     id: langsTxt
                     text: langs
                     anchors.top: aboutTxt.bottom
                 }
    
              }
         }
    
    raven-worxR 1 Reply Last reply
    0
    • S Subuday

      Hi, I have a list of mentors and I want to add their avatars to list. But I don't know how to load from database images and display them. Also, I couldn't find how to send image from database to qml.

      Sorry, if if here's the obvious solution, I'm new

      Here my list model

      #ifndef MENTORLISTMODEL_H
      #define MENTORLISTMODEL_H
      
      #include <QAbstractListModel>
      #include <QStringList>
      #include "mentor.h"
      
      class MentorListModel: public QAbstractListModel
      {
          Q_OBJECT
      
      public:
          enum Roles {
             NameRole = Qt::UserRole + 1,
             AboutRole,
             LangRole,
          };
      
          MentorListModel(QObject *parent = 0);
      
          virtual int rowCount(const QModelIndex &parent) const;
          virtual QVariant data(const QModelIndex &index, int role) const;
          virtual QHash<int, QByteArray> roleNames() const;
      
          Q_INVOKABLE void add(const Mentor &unit);
          void clear();
      
      private:
          QList<Mentor> m_data;
      };
      
      #endif // MENTORLISTMODEL_H
      
      #include "mentorlistmodel.h"
      
      MentorListModel::MentorListModel(QObject *parent):
          QAbstractListModel(parent)
      {
      
      }
      
      
      int MentorListModel::rowCount(const QModelIndex &parent) const
      {
          if (parent.isValid()) {
              return 0;
          }
      
          return m_data.size();
      }
      
      QVariant MentorListModel::data(const QModelIndex &index, int role) const
      {
          if (!index.isValid()) {
              return QVariant();
          }
      
          switch (role) {
          case NameRole:
              return m_data.at(index.row()).firstName + " " + m_data.at(index.row()).lastName;
          case AboutRole:
              return m_data.at(index.row()).about;
          case LangRole: {
              QString langs;
              QStringList::const_iterator it;
              for (it = m_data.at(index.row()).langs.begin(); it != m_data.at(index.row()).langs.end(); ++it) {
                  langs += *it + " ";
              }
              return langs;
          }
          default:
              return QVariant();
          }
      }
      
      QHash<int, QByteArray> MentorListModel::roleNames() const
      {
          QHash<int, QByteArray> roles = QAbstractListModel::roleNames();
          roles[NameRole] = "name";
          roles[AboutRole] = "about";
          roles[LangRole] = "langs";
      
          return roles;
      }
      
      void MentorListModel::add(const Mentor &unit)
      {
          beginInsertRows(QModelIndex(), m_data.size(), m_data.size());
          m_data.append(unit);
          endInsertRows();
      }
      
      void MentorListModel::clear()
      {
          beginRemoveRows(QModelIndex(), 0, m_data.size());
          endRemoveRows();
          m_data.clear();
      }
      
          ListView {
               id: mentorsList
               model: filterModel
               height: 400
               width: 300
               anchors.top: searchField.bottom
               spacing: 5
               cacheBuffer: 100
      
              delegate: Rectangle {
                   width: parent.width
                   radius: 5
                   anchors.horizontalCenter: parent.horizontalCenter
                   height: 200
                   color: Qt.lighter("#6bdce4", 0.8)
                   Text {
                       id: nameTxt
                       text: name
                       font.pointSize: 12
                       color: "#FFFFFF"
                       anchors.left: parent.left
                       anchors.leftMargin: 20
                       anchors.verticalCenter: parent.verticalCenter
                   }
      
                   Text {
                       id: aboutTxt
                       text: about
                       anchors.top: nameTxt.bottom
                   }
      
                   Text {
                       id: langsTxt
                       text: langs
                       anchors.top: aboutTxt.bottom
                   }
      
                }
           }
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Subuday said in How to add image from database to my listmodel.:
      how are the images stored in the database?
      what is the binary format?
      what is the type of DB column?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved