Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get QLabel from model class.
QtWS25 Last Chance

How to get QLabel from model class.

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 2.5k Views
  • 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.
  • T Offline
    T Offline
    tokafr
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tokafr
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kenchan
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            First things first, what are you trying to achieve ?

            A QStandardItemModel might be enough.

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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kenchan
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0

                • Login

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