How to get QLabel from model class.



  • Hello all!
    I am a beginner in Qt MVC pattern. I try to create a list view which displays Icons on Labels. But I get empty items in my view. Here is what I write in my model class.
    model.h

    @
    #define MODEL_H

    #include <QAbstractListModel>
    class QLabel;

    class Model : public QAbstractListModel
    {
    Q_OBJECT
    public:
    Model(QObject *parent = 0);

    bool setData(const QModelIndex &index, const QVariant &value, int role);
    bool insertRows(int row, int count, const QModelIndex &parent);
    void addRow(const QLabel item, QModelIndex &index);
    QList<QLabel
    > getData()const;
    int rowCount(const QModelIndex &parent) const;
    QVariant data(const QModelIndex &index, int role) const;

    private:
    QList<QLabel*> dataList;

    };

    and model.cpp
    #include <QtGui>
    #include "model.h"
    #include <iostream>
    #include <QLabel>
    #include <QTableWidgetItem>

    Model::Model(QObject *parent):QAbstractListModel(parent)
    {
    }

    QList<QLabel*> Model::getData()const
    {
    return dataList;
    }

    QVariant Model::data(const QModelIndex &index, int role) const
    {
    if(!index.isValid())
    {
    std::cout<<"errororor\n";
    return QVariant();
    }
    if(index.row() < 0 || index.row() >= dataList.size())
    {
    return QVariant();
    }

    if(role == Qt::DisplayRole || role == Qt::EditRole)
    {
    std::cout<<"dadaaaaaa\n";
    return qVariantFromValue(dataList.at(index.row()));
    }

    std::cout<<"eeerrtr\n";
    return QVariant();
    }

    int Model::rowCount(const QModelIndex &parent) const
    {
    return dataList.size();
    }

    bool Model::insertRows(int row, int count, const QModelIndex &parent = QModelIndex())
    {
    if(parent.isValid())
    {
    return false;
    }

    beginInsertRows(QModelIndex(), row, row + count - 1);
    for(int i = 0;i < count; ++i)
    {
    QPixmap pixmap("/home/omar/image.png");
    //QIcon icon (pixmap);
    QLabel *label = new QLabel("trtrtrtrtr");
    label->setPixmap(pixmap);

     dataList.insert(row,label);
    

    }

    endInsertRows();
    return true;
    }

    bool Model::setData(const QModelIndex &index, const QVariant &value, int role)
    {
    if(!index.isValid())
    {
    return false;
    }

    if(role == Qt::EditRole)
    {

    dataList.replace(index.row(),value.value<QLabel*>());//qvariant_cast<QIcon*>(value));
    emit dataChanged(index,index);
    return true;
    

    }
    return false;
    }

     void Model::addRow(const QLabel *item, QModelIndex &index)
    

    {
    insertRows(rowCount(index),1,index);
    //setData(index,item);
    }

    @
    can help me please?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Don't create widgets like that in the model, it's not its role. Since you just want to show an icon, there's the Qt::IconRole for that. If you want customized output on your QListView then use a custom QStyledItemDelegate

    Hope it helps



  • Can you show me how? I tried it but no success. do I need Model class
    at all?



  • I wonder if this might help you...
    It talks about using a Combo Box but I think you will get the idea.

    "look here...":http://qt-project.org/wiki/Combo_Boxes_in_Item_Views


  • Lifetime Qt Champion

    First things first, what are you trying to achieve ?

    A QStandardItemModel might be enough.

    @ kenchan, it's not the same use-case



  • OK sorry.
    I just thought it might help illustrate the use of the QStyledItemDelegate


  • Lifetime Qt Champion

    Indeed, that's a good point. But from tokafr's problem description and questions it seems that first the MVC basics should first be cleared


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.