700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > qlistview 自定义控件_qlistview使用自定义模型的复选框

qlistview 自定义控件_qlistview使用自定义模型的复选框

时间:2021-08-03 01:43:26

相关推荐

qlistview 自定义控件_qlistview使用自定义模型的复选框

我有文件系统模型的子类包括复选框在列表视图,这是正常工作。我的问题是,每当我点击一个项目,该项目的文本消失,当我点击另一个项目时,以前选择的项目的文本变得可见。任何人都可以告诉我背后的原因。

这是我实施的代码。

请告诉我我在这里错过了什么,

谢谢

#include "custommodel.h"

#include

using namespace std;

CustomModel::CustomModel()

{

}

QVariant CustomModel::data(const QModelIndex& index, int role) const

{

QModelIndex parent=this->parent(index);

if(role == Qt::DecorationRole)

{

if(this->filePath(parent)=="")

{

return QIcon(":/Icons/HardDisk.png");

}

else if(this->isDir(index))

{

QDir dir(this->filePath(index));

QFileInfoList files = dir.entryInfoList(QDir::NoDotAndDotDot |

QDir::Files | QDir::Dirs);

for(int file = 0; file < files.count(); file++)

if(files.count()>0)

return QIcon(":/Icons/FullFolder.png");

if(files.count()==0)

return QIcon(":/Icons/EmptyFolder.png");

}

else{

QFileInfo fi( this->filePath(index));

QString ext = fi.suffix();

if(ext=="jpeg"||ext=="jpg"||ext=="png"||ext=="bmp")

return QIcon(filePath(index));

}

}

if (role == Qt::CheckStateRole && !(this->filePath(parent)==""))

return checklist.contains(index) ? Qt::Checked : Qt::Unchecked;

return QFileSystemModel::data(index, role);

}

Qt::ItemFlags CustomModel::flags(const QModelIndex& index) const

{

return QFileSystemModel::flags(index)| Qt::ItemIsUserCheckable;

}

bool CustomModel::setData(const QModelIndex& index, const QVariant& value, int role)

{

if (role == Qt::CheckStateRole) {

if (value == Qt::Checked)

checklist.insert(index);

else

checklist.remove(index);

emit dataChanged(index, index);

return true;

}

return QFileSystemModel::setData(index, value, role);

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。