第三代软件开发-文件Model
关键字:
Qt
、 Qml
、 关键字3
、 关键字4
、 关键字5
项目介绍
重要说明☝
☀该专栏在第三代软开发更新完将涨价
文件Model
这个就是想做一个通用一点的model,因为我需要知道我的文件夹下的mp4 和pdf 文件,这两个除了类型不同,其他功能都相同,所以就有了这个,代码如下
头文件
#ifndef XXXX_FILEMODEL_H
#define XXXX_FILEMODEL_H
#include <QAbstractListModel>
class XXXX_FileModel : public QAbstractListModel
{
Q_OBJECT
enum FileType
{
Name,
};
Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged FINAL)
public:
explicit XXXX_FileModel(QObject *parent = nullptr);
// Header:
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) override;
// Basic functionality:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
// Fetch data dynamically:
bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
bool canFetchMore(const QModelIndex &parent) const override;
void fetchMore(const QModelIndex &parent) override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QHash<int,QByteArray> roleNames() const override;
// Editable:
bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole) override;
Qt::ItemFlags flags(const QModelIndex& index) const override;
// Add data:
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
// Remove data:
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
QString path() const;
void setPath(const QString &newPath);
signals:
void pathChanged();
protected:
void updateFile();
private:
QString m_path = "/home";
QStringList m_fileList;
};
#endif // XXXX_FILEMODEL_H
源文件
#include "XXXX_filemodel.h"
#include "qdebug.h"
#include "qdir.h"
XXXX_FileModel::XXXX_FileModel(QObject *parent)
: QAbstractListModel(parent)
{
updateFile();
}
QVariant XXXX_FileModel::headerData(int section, Qt::Orientation orientation, int role) const
{
// FIXME: Implement me!
}
bool XXXX_FileModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
{
if (value != headerData(section, orientation, role)) {
// FIXME: Implement me!
emit headerDataChanged(orientation, section, section);
return true;
}
return false;
}
int XXXX_FileModel::rowCount(const QModelIndex &parent) const
{
// For list models only the root node (an invalid parent) should return the list's size. For all
// other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
if (parent.isValid())
return 0;
// FIXME: Implement me!
return m_fileList.count();
}
bool XXXX_FileModel::hasChildren(const QModelIndex &parent) const
{
// FIXME: Implement me!
}
bool XXXX_FileModel::canFetchMore(const QModelIndex &parent) const
{
// FIXME: Implement me!
return false;
}
void XXXX_FileModel::fetchMore(const QModelIndex &parent)
{
// FIXME: Implement me!
}
QVariant XXXX_FileModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
// FIXME: Implement me!
if(m_fileList.count() > 0)
return m_fileList.at(index.row());
return QVariant();
}
QHash<int, QByteArray> XXXX_FileModel::roleNames() const
{
QHash<int,QByteArray> roles;
roles.insert(FileType::Name,"name");
return roles;
}
bool XXXX_FileModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (data(index, role) != value) {
// FIXME: Implement me!
emit dataChanged(index, index, {role});
return true;
}
return false;
}
Qt::ItemFlags XXXX_FileModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; // FIXME: Implement me!
}
bool XXXX_FileModel::insertRows(int row, int count, const QModelIndex &parent)
{
beginInsertRows(parent, row, row + count - 1);
// FIXME: Implement me!
endInsertRows();
return true;
}
bool XXXX_FileModel::removeRows(int row, int count, const QModelIndex &parent)
{
beginRemoveRows(parent, row, row + count - 1);
// FIXME: Implement me!
endRemoveRows();
return true;
}
QString XXXX_FileModel::path() const
{
return m_path;
}
void XXXX_FileModel::setPath(const QString &newPath)
{
if (m_path == newPath)
return;
m_path = newPath;
emit pathChanged();
updateFile();
}
void XXXX_FileModel::updateFile()
{
QDir folder(m_path);
m_fileList.clear();
m_fileList = folder.entryList(QDir::Files);
foreach(QString file, m_fileList)
{
qDebug() << file;
}
}