[SOLVED] QListView remains empty



  • 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();
    }
    @


  • Lifetime Qt Champion

    Hi,

    What is your DicomImageModel for a class ?

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



  • 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...


  • Lifetime Qt Champion

    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



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.