Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    How to get QLabel from model class.

    General and Desktop
    3
    7
    2200
    Loading More Posts
    • 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
      tokafr last edited by

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • T
          tokafr last edited by

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

          1 Reply Last reply Reply Quote 0
          • K
            kenchan last edited by

            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 Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              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 Reply Quote 0
              • K
                kenchan last edited by

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

                1 Reply Last reply Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  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 Reply Quote 0
                  • First post
                    Last post