سؤال

ما أحاول القيام به: الكتابة فوق setData QFILESYSTEMMODEL وبيانات لتنفيذ التخزين المؤقت للصور في الدليل المعروض.

يمكنني استخدام QListView للاختبار الغرض.

هنا هو الرمز ذي الصلة:

صفي مع QFILESYSTEMMODEL كوالد:

.

#ifndef QPICSINFILESYSTEMMODEL_H
#define QPICSINFILESYSTEMMODEL_H

#include <QFileSystemModel>
#include <QCache>
#include <QDebug>

/* This Model holds all Picturefiles with a cached QPixmap of
 * them.
 */

class PicsInFileSystemModel : public QFileSystemModel
{
public:
    PicsInFileSystemModel();
    QVariant data (const QModelIndex & index, int role);
private:
    QCache<qint64,QPixmap> *cache; //Cache for the pictures

};

#endif // QPICSINFILESYSTEMMODEL_

.

#include "PicsInFileSystemModel.h"

PicsInFileSystemModel::PicsInFileSystemModel()
{
    QStringList myFilter;
    this->setFilter(QDir::Files | QDir::AllDirs);
    this->setRootPath(QDir::rootPath());//QDir::homePath());
    myFilter << "jpeg" << "jpg" << "png";
    //this->setNameFilters(myFilter);
}

/* Reimplement data to send the pictures to the cache */
QVariant PicsInFileSystemModel::data ( const QModelIndex & index, int role = Qt::DisplayRole ) {
    qDebug() << "index: " << index << "role: " << role;

    return ((QFileSystemModel)this).data(index,role);
}

كيف أسمي الكائن:

pics = new PicsInFileSystemModel;
form->listViewPictures->setModel(pics);
form->listViewPictures->setRootIndex(pics->index(
        "mypath"));

إذن هذا هو السؤال: في رأيي ، يجب أن أرى العديد من مخرجات التصحيح عند الوصول إلى النموذج. لكن لا يوجد شيء. هل لدى أي شخص فكرة عما أفعله خطأ؟

شكرًا!

تحرير: الإجابات تعمل. كان علي أيضًا تغيير هذا

return ((QFileSystemModel)this).data(index,role); 

داخل

QFileSystemModel::data(index,role))
هل كانت مفيدة؟

المحلول

توقيع data الطريقة هي:

QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const

طريقتك غير مؤلمة. اجعل طريقتك const ووضع علامة على المتغيرات التي تحتاج إلى تعديلها على أنها قابلة للتغيير.

نصائح أخرى

لك data لا تسمى الوظيفة أبدًا لأنها لا تتطابق مع الأصل تعريف. لم تكن إعادة تنفيذ data, ، لقد قدمت إصدارًا غير مؤكد.

يستخدم Qfileiconprovider من اجل ذلك الهدف.

Thumbnailiconprovider.h

#ifndef THUMBNAILICONPROVIDER_H
#define THUMBNAILICONPROVIDER_H

#include <QFileIconProvider>

class ThumbnailIconProvider : public QFileIconProvider
{
public:
    ThumbnailIconProvider();

    QIcon icon(const QFileInfo & info) const;
};

#endif // THUMBNAILICONPROVIDER_H

Thumbnailiconprovider.cpp

#include "thumbnailiconprovider.h"

#include <QDebug>

ThumbnailIconProvider::ThumbnailIconProvider()
{
}

QIcon ThumbnailIconProvider::icon(const QFileInfo & info) const
{
    QIcon ico(info.absoluteFilePath());
    if (ico.isNull())
        return QFileIconProvider::icon(info);
    else {
        qDebug() << "Fetch icon for " << info.absoluteFilePath();
        return ico;
    }
}

لاستخدام هذا الفئة Call Seticonprovider على النموذج الخاص بك.

QFileSystemModel * model = new QFileSystemModel(this);
model->setIconProvider(new ThumbnailIconProvider());
model->setRootPath(...);
...

لاحظ أنه يمكنك دمج التقطير بهذه الطريقة بسهولة.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top