Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to add image from database to my listmodel.



  • 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
                 }
    
              }
         }
    

  • Moderators

    @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?


Log in to reply