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. [SOLVED] QListView remains empty
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QListView remains empty

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.1k Views 1 Watching
  • 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.
  • P Offline
    P Offline
    postb99
    wrote on last edited by
    #1

    Hello,

    Please can anyone help. My QListView remains empty, while my model is OK.
    If I set a breakpoint in "data()", it doesn't get reached.

    Thank you,

    @
    // standard constructor

    // standard destructor

    // rowCount is OK
    int DicomImageModel::rowCount(const QModelIndex & /parent/) const
    {
    int i = getImagesCount();
    xLogWarning(XLogRecord::Type_User, QString("'rowCount' called, value: %1").arg(i));
    return i;
    }

    // fixed size, do I need to add headers or would it work without?
    int DicomImageModel::columnCount(const QModelIndex & /parent/) const
    {
    return 6;
    }

    // data... don't log here
    QVariant DicomImageModel::data(const QModelIndex &index, int role) const
    {
    xLogWarning(XLogRecord::Type_User, QString("'data' called"));

     int row = index.row();
     int col = index.column();
     // generate a log message when this method gets called
     xLogWarning(XLogRecord::Type_User, QString("row %1, col%2, role %3").arg(row).arg(col).arg(role));
    
     //xLogWarning(XLogRecord::Type_User, QString(m_dicomImages[0]->getId()));
    
     if(role == Qt::DisplayRole){
         if (col == 0) return QString(m_dicomImages[row]->getId());
         //if (col == 1) return QString(m_dicomImages[row]->getDicomRef().getSeriesInstanceUID());
    
         return QString("Row%1, Column%2")
                 .arg(row + 1)
                 .arg(col + 1);
     }
    
     return QString("Row%1, Column%2")
             .arg(row + 1)
             .arg(col + 1);
    
     return QVariant();
    

    }

    // populate model, seems ok but I have no "view" of what happens.
    void DicomImageModel::addImage(const XImageObjectPtr image)
    {
    int nbRows = m_dicomImages.size();
    beginInsertRows(QModelIndex(), nbRows, nbRows);
    m_dicomImages.push_back(image);
    xLogWarning(XLogRecord::Type_User, QString(tr("Image %1 added").arg(image->getId())));
    endInsertRows();
    }

    // just a helper, m_dicomImages is a vector of shared pointers (QSharedPointer).
    int DicomImageModel::getImagesCount() const {
    return m_dicomImages.size();
    }
    @

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

      Hi,

      What is your DicomImageModel for a class ?

      Why do you return an empty QVariant for everything that is not DisplayRole ?

      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
      • P Offline
        P Offline
        postb99
        wrote on last edited by
        #3

        DicomImageModel derive from QAbstractTableModel.

        I'm a beginner, so what should I return when it's not a DisplayRole? I lately added this, in fact.

        The small plugin I use gets instantiated when I open the main GUI, then if I click on a menu item it gets enabled (visible). It contains a QListView with model previously described.

        Something is wrong with rowCount() and columnCount(), they're called at startup, when plugin and model are instantiated, then...

        When I add an image to model, rowCount() gets called, but shows "0 row", then I have "1 object in model". It's always 1 item behind.

        When I then click onto menu item link, my widget shows, but neither rowCount() nor columnCount() get called again...

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

          it should:

          @return QAbstractTableModel::data(index, role);@

          The row/column count are not constantly queried. Only when you notify the view that the model has changed.

          I would recommend that you have a look at the various Model View examples from Qt's documentation. You'll learn how things work with it

          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
          • P Offline
            P Offline
            postb99
            wrote on last edited by
            #5

            I found the problem: I was reinstantiating the model, since I fixed code but not totally by moving the model declaration to the header. Once fixed I have data as expected.

            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