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, headerdata and data never called
Forum Updated to NodeBB v4.3 + New Features

QListView, headerdata and data never called

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.4k 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.
  • A Offline
    A Offline
    Athius
    wrote on 11 Mar 2019, 11:17 last edited by Athius 3 Nov 2019, 11:47
    #1

    Hi everyone,

    I just want to create a model that contains two custom datas (two QString) and display it in a QListView.
    I have some issue: nothing is shown when I insert a new row and a new data inside it.

    I join you my model. m_items is a QList of ImageSegmentationItem (a class with two QString).

    When I debug my code, I never go inside the headerData method neither data method.
    I don't understand what I did wrong.

    Thank you

    ImageSegmentationModel::ImageSegmentationModel(QObject *parent)
    	: QAbstractListModel(parent)
    {
    }
    
    QVariant ImageSegmentationModel::headerData(int section, Qt::Orientation orientation, int role) const
    {
    	if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    	{
    		if (section == 0)
    		{
    			return tr("Image Path");
    		}
    		else if (section == 1)
    		{
    			return tr("Segmentation Path");
    		}
    	}
    	return QVariant();
    }
    
    int ImageSegmentationModel::rowCount(const QModelIndex &parent) const
    {
    	return m_items.size();
    }
    
    int ImageSegmentationModel::columnCount(const QModelIndex &parent) const
    {
    	return 2;
    }
    
    QVariant ImageSegmentationModel::data(const QModelIndex &index, int role) const
    {
    	if (!index.isValid())
    		return QVariant();
    
    	if (role == Qt::DisplayRole)
    	{
    		if (index.column() == 0)
    		{
    			return m_items.at(index.row()).imagePath();
    		}
    		else if (index.column() == 1)
    		{
    			return m_items.at(index.row()).segmentationPath();
    		}
    	}
    
    	return QVariant();
    }
    
    bool ImageSegmentationModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
    	bool res = false;
    	if (index.isValid() && role == Qt::EditRole)
    	{
    		if (m_items.size() > index.row())
    		{
    			if (value.canConvert<QString>())
    			{
    				QString valStr = value.toString();
    				if (index.column() == 0)
    				{
    					m_items[index.row()].setImagePath(valStr);
    					res = true;
    				}
    				else if (index.column() == 1)
    				{
    					m_items[index.row()].setSegmentationPath(valStr);
    					res = true;
    				}
    			}
    			if (res)
    			{
    				emit dataChanged(index, index);
    			}
    		}
    	}
    	return res;
    }
    

    Edit: I remove the test on the parent in the rowCount method and now, the line are show but only the first column (still without the header)

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 11 Mar 2019, 12:22 last edited by
      #2

      @Athius said in QListView, headerdata and data never called:

      but only the first column

      https://doc.qt.io/qt-5/qabstractlistmodel.html#details :
      "The QAbstractListModel class provides an abstract model that can be subclassed to create one-dimensional list models."

      So why should it be called for the second column? Looks like you want QAbstractTableModel instead.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • A Offline
        A Offline
        Athius
        wrote on 11 Mar 2019, 15:17 last edited by
        #3

        @Christian-Ehrlicher said in QListView, headerdata and data never called:

        So why should it be called for the second column? Looks like you want QAbstractTableModel instead.

        Thank you.

        Because I thought that by overloading the columnCount method and always returning 2, the problem will fix but no.
        I change by a QAbstractTableModel and also by QAbstractItemModel before I understand that the problem was coming from the view: I change the QListView by a QTableView and it's all working now (I keep the model as described before).

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 11 Mar 2019, 15:54 last edited by
          #4

          @Athius said in QListView, headerdata and data never called:

          Because I thought that by overloading the columnCount method and always returning 2

          A QAbstractListModel is made for 1 column, you misuse the desired usecase. This can lead to unexpected things. If you want a table, use a table and not a chair.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1

          1/4

          11 Mar 2019, 11:17

          • Login

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