Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[Solved] QAbstractListModel doesn't show all items



  • Hi,

    QAbstractListModel doesnt show Item which are initially not on the screen.
    E.g. I insert 10 Items. The screen has only enough space for 8. When I scroll up, still only those 8 seen in the list. When I now increase the height of the main view. All 10 Items are visible.

    Already played with "rowCount" and "cacheBuffer", nothing helped.

    My QAbstractListModel
    @
    #include "daymodel.h"

    DayModel::DayModel(QObject *parent) : QAbstractListModel(parent)
    {
    QHash<int, QByteArray> roles;
    roles[DayIdRole] = "dayId";
    roles[DayStringRole] = "dayString";
    roles[SumRole] = "sum";
    roles[MaxReachedRole] = "maxReached";
    roles[BillsRole] = "bills";
    setRoleNames(roles);
    }

    void DayModel::addDay(Day *day)
    {
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_days.append(day);
    endInsertRows();
    }

    int DayModel::rowCount(const QModelIndex &parent) const
    {
    return m_days.count();
    }

    QVariant DayModel::data(const QModelIndex &index, int role) const
    {
    if(index.row() < 0 || index.row() > m_days.count())
    return QVariant();

    Day *day = m_days[index.row()];
    if(role == DayIdRole)
        return day->getDayId();
    if(role == DayStringRole)
        return day->getDayString();
    if(role == SumRole)
        return day->getSum();
    if(role == MaxReachedRole)
        return day->getMaxReached();
    if(role == BillsRole){
        QList<QVariant> variantList;
        foreach(QObject *bill, day->getBills()){
            variantList.append(qVariantFromValue(bill));
        }
        return QVariant::QVariant(variantList);
    }
    
    return QVariant();
    

    }
    @

    List with all Items
    @
    //List containing all items
    Item{

    id: listView
    anchors.top: summary.bottom
    anchors.right: options.left
    height: parent.height - summary.height
    width: parent.width - options.width

     ListView {
            anchors.fill: parent
         orientation: ListView.Vertical
         spacing: 5
          
            model: dataHandler.dayModel
         delegate: EntryHead{ }
     }  
    

    }
    @

    EntryHead
    @
    // import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
    import QtQuick 1.0

    Item {
    width: parent.width
    height: headerArea.height + subEntries.height

    property string dayStringVal: dayString
    property string sumVal: sum
    property string maxReachedVal: maxReached
    
    Frame {
        id: headerArea
        height: textDate.height + 5
        width: parent.width
        area.color: maxReachedVal == "yes" ?  "#FFDDDD" : "#DDFFDD"
        
        Text {
         id: textDate
            color: "white"
         font.pixelSize: 24
            anchors.left: parent.left
            anchors.leftMargin: 10
            text: "Date: " + dayStringVal + " - Sum: " + sumVal
     }
    
     Row {
      anchors.right: parent.right
            anchors.rightMargin: 10
      
            width: but_Add.width + but_Del.width
      height: parent.height
    
      Button{
                id: but_Add
                width: parent.height * 2
                height: parent.height
                text: "Add"
    
                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        mainViewBlocker.state = ""
                        formularAddItem.state = "show"
                        formularAddItem.dateKey = dayStringVal
                    }
    
                    onPressed: {
                        parent.color = "red"
                    }
                    onReleased: {
                        parent.color = "white"
                    }
                }
            }
      
      Button{
                id: but_Del
                width: parent.height * 2
       height: parent.height
                text: "Clr"
    
                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        mainScreen.clearDay(dayStringVal)
                    }
    
                    onPressed: {
                        parent.color = "red"
                    }
                    onReleased: {
                        parent.color = "white"
                    }
                }
      }
     }  
    }
    
    Column {
    
        id: subEntries
    

    anchors.top: headerArea.bottom
    width: parent.width
    spacing: 1

     Repeater {
            model: bills
         delegate: SubDataView {
          width: parent.width      
         }
     }
    }
    

    }
    @

    Thank you

    [edit: fixed typo in title, Eddy]



  • Have not yet worked with QtQuick.
    But what about something like setUpdatesEnabled or update() for your ListWidget?



  • Hi,

    the problem got solved after I declared the DataHandler class , which contains the data for the list items, as a class attribute. Instead declaring it inside a method and only fill the context property with it.

    Thank you


Log in to reply