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. QListView with custom model and styleditem delegate
Qt 6.11 is out! See what's new in the release blog

QListView with custom model and styleditem delegate

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 2.8k 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.
  • V Offline
    V Offline
    vmjafar
    wrote on last edited by VRonin
    #1

    Hi,

    i have qlistview with a model and styled item delegate ,in my delegate i have overrieded sizehint() method,in which one parameter is qmodel index .is there any way to get my model object from qmodel index.my code is as follows

    //list model for qlistview
    class MyListViewModel : public QAbstractListModel
    {
    	Q_OBJECT
    
    public:
    
    	MyListViewModel(std::vector<MyModel*>* modelList, QObject* parent = 0);
    	int rowCount(const QModelIndex &parent = QModelIndex()) const;
    	QVariant data(const QModelIndex &index, int role) const;
    
    
    	std::vector<MyModel*>* m_pModelList;
    };
    
    //MyModel.cpp
    
    class MyModel
    {
    public:
    	MyModel();
    	MyModel(string name);
    	~MyModel();
    	string name;
    };
    
    //delegate
    
    class MyDelegate : public QStyledItemDelegate
    {
    	Q_OBJECT
    public:
    	//enum datarole { Title = Qt::UserRole + 100, Start, Action, Stop };
    
    	MyDelegate();
    	~MyDelegate();
    
    	void paint(QPainter *painter,
    		const QStyleOptionViewItem &option,
    		const QModelIndex &index) const;
    
    	QSize sizeHint(const QStyleOptionViewItem &option,
    		const QModelIndex &index) const;
    
    	static QSize iconSize;
    	static int padding;
    };
    
    //Mydelegate.cpp
    
    
    QSize Mydelegate::sizeHint(const QStyleOptionViewItem &  option,
    	const QModelIndex & index) const
    {
    }
    void Mydelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    }
    

    i want to know about how can i get MyModel object from qmodelindex inside sizeHint() and paint() method in MyDelegate

    Jafar VM

    VRoninV 1 Reply Last reply
    0
    • VRoninV VRonin

      qDebug() << index.model()->rowCount();

      V Offline
      V Offline
      vmjafar
      wrote on last edited by
      #5

      @VRonin Thanks for ur support ,its working

      1 Reply Last reply
      1
      • V vmjafar

        Hi,

        i have qlistview with a model and styled item delegate ,in my delegate i have overrieded sizehint() method,in which one parameter is qmodel index .is there any way to get my model object from qmodel index.my code is as follows

        //list model for qlistview
        class MyListViewModel : public QAbstractListModel
        {
        	Q_OBJECT
        
        public:
        
        	MyListViewModel(std::vector<MyModel*>* modelList, QObject* parent = 0);
        	int rowCount(const QModelIndex &parent = QModelIndex()) const;
        	QVariant data(const QModelIndex &index, int role) const;
        
        
        	std::vector<MyModel*>* m_pModelList;
        };
        
        //MyModel.cpp
        
        class MyModel
        {
        public:
        	MyModel();
        	MyModel(string name);
        	~MyModel();
        	string name;
        };
        
        //delegate
        
        class MyDelegate : public QStyledItemDelegate
        {
        	Q_OBJECT
        public:
        	//enum datarole { Title = Qt::UserRole + 100, Start, Action, Stop };
        
        	MyDelegate();
        	~MyDelegate();
        
        	void paint(QPainter *painter,
        		const QStyleOptionViewItem &option,
        		const QModelIndex &index) const;
        
        	QSize sizeHint(const QStyleOptionViewItem &option,
        		const QModelIndex &index) const;
        
        	static QSize iconSize;
        	static int padding;
        };
        
        //Mydelegate.cpp
        
        
        QSize Mydelegate::sizeHint(const QStyleOptionViewItem &  option,
        	const QModelIndex & index) const
        {
        }
        void Mydelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
        {
        }
        

        i want to know about how can i get MyModel object from qmodelindex inside sizeHint() and paint() method in MyDelegate

        Jafar VM

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #2

        http://doc.qt.io/qt-5/qmodelindex.html#model

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        V 1 Reply Last reply
        2
        • VRoninV VRonin

          http://doc.qt.io/qt-5/qmodelindex.html#model

          V Offline
          V Offline
          vmjafar
          wrote on last edited by
          #3

          @VRonin
          could you please share any sample code related to my scenario?

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #4

            qDebug() << index.model()->rowCount();

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            V 1 Reply Last reply
            2
            • VRoninV VRonin

              qDebug() << index.model()->rowCount();

              V Offline
              V Offline
              vmjafar
              wrote on last edited by
              #5

              @VRonin Thanks for ur support ,its working

              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