[QT] MVC - View fragt zuviele Items ab
-
Mahlzeit, ich bin gerade dabei, ein bischen mit Qt zu spielen.
Ein ListView soll die einzelnen Seiten eines PDF-Dokumentes anzeigen.
Das Model rendert auf Anfrage mittels poppler.
Jetzt fragt aber das View direkt alle Items ab sobald ihm ein Model zugewiesen wurde, auch wenn nicht alle Items sichtbar sind. Das hebelt den ganzen Mechanismus aus und ist natürlich total nervig bei großen Dateien.#ifndef DOCUMENTMODEL_HPP #define DOCUMENTMODEL_HPP #include <QAbstractListModel> #include <QImage> #include <poppler-qt4.h> class DocumentModel : public QAbstractListModel { Q_OBJECT public: explicit DocumentModel(QString path, double scaleFactor, int physicalDpiX, int physicalDpiY, QObject *parent = 0); ~DocumentModel(); int rowCount(const QModelIndex &parent = QModelIndex()) const; QVariant data(const QModelIndex &index = QModelIndex(), int role = Qt::DisplayRole) const; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; private: Poppler::Document* m_document; mutable QImage** m_pages; int m_scaleFactor; int m_physicalDpiX; int m_physicalDpiY; signals: public slots: }; #endif // DOCUMENTMODEL_HPP
#include "documentmodel.hpp" #include <QIcon> DocumentModel::DocumentModel(QString path, double scaleFactor, int physicalDpiX, int physicalDpiY, QObject *parent) : QAbstractListModel(parent), m_document(0), m_pages(0), m_scaleFactor(scaleFactor), m_physicalDpiX(physicalDpiX), m_physicalDpiY(physicalDpiY) { m_document = Poppler::Document::load(path); m_pages = new QImage*[m_document->numPages()]; for(int i = 0; i < m_document->numPages(); i++) m_pages[i] = 0; } DocumentModel::~DocumentModel() { for(int i=0; i < m_document->numPages(); i++) delete m_pages[i]; delete[] m_pages; delete m_document; } int DocumentModel::rowCount(const QModelIndex &parent) const { return m_document->numPages(); } QVariant DocumentModel::data(const QModelIndex &index, int role) const { if(index.isValid()) switch(role) { case Qt::DecorationRole: { if(!m_pages[index.row()]) { m_pages[index.row()] = new QImage( m_document->page(index.row())->renderToImage( m_scaleFactor * m_physicalDpiX, m_scaleFactor * m_physicalDpiY)); } return (QVariant) QIcon(QPixmap::fromImage(*m_pages[index.row()])); } case Qt::DisplayRole: return QString("Page %1").arg(index.row() + 1); default: return QVariant(); } else return QVariant(); } QVariant DocumentModel::headerData(int section, Qt::Orientation orientation, int role) const { if(role == Qt::DisplayRole) { if(orientation == Qt::Horizontal) return QVariant("Pages"); else return QVariant(QString("Page %1").arg(section)); } else return QVariant(); }
Hat jemand irgendwelche Tipps, wie nur die Items gerendert werden, die tatsächlich auch gerendert werden müssen?
-
ja und zwar in dem du das model so implementierst das es nur anzahl von "ELementen" als verfügbar zurückliefert, welche sichtbar sind.