[英]qlistview with checkboxes using custom model
I have subclassed filesystemmodel to include checkboxes in listview , which is working fine. 我有子类化的filesystemmodel,以在listview中包括复选框,它工作正常。 My problem is whenever I click an item the text of that item disappears and when i click another item the text of previously selected item becomes visible. 我的问题是,每当我单击一个项目时,该项目的文本就会消失,而当我单击另一个项目时,先前选择的项目的文本将变为可见。 Can anyone please tell me the reason behind it. 谁能告诉我背后的原因。
Here is the code i implemented. 这是我实现的代码。
Please tell me what i am missing here, Thanks 请告诉我我在这里想念的东西,谢谢
#include "custommodel.h"
#include <iostream>
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);
}
Not sure whether it's relevant, but I found the following note at: http://doc.trolltech.com/4.6/qt.html#ItemFlag-enum 不确定是否相关,但是我在以下位置找到了以下说明: http : //doc.trolltech.com/4.6/qt.html#ItemFlag-enum
"Note that checkable items need to be given both a suitable set of flags and an initial state, indicating whether the item is checked or not. This is handled automatically for model/view components, but needs to be explicitly set for instances of QListWidgetItem, QTableWidgetItem, and QTreeWidgetItem." “请注意,必须给可检查的项目同时提供一组合适的标志和初始状态,以指示是否检查了该项目。这是针对模型/视图组件自动处理的,但需要为QListWidgetItem的实例进行显式设置, QTableWidgetItem和QTreeWidgetItem。”
As far as I can make out, your code looks correct -- but maybe try setting the ItemIsUserCheckable flag on the base QFileSystemModel class (in your custom constructor), and see if the inherited data() and setData() methods work with role=Qt::CheckStateRole. 据我所知,您的代码看起来正确-但也许可以尝试在基本QFileSystemModel类上(在您的自定义构造函数中)设置ItemIsUserCheckable标志,并查看继承的data()和setData()方法是否与role =一起使用Qt :: CheckStateRole。 If you need to maintain a list of what's currently checked for some other reason, then go ahead and do so in your derived setData(), but still call QFileSystemModel::setData() as well. 如果您需要维护由于其他原因当前正在检查的内容的列表,请继续执行此操作,并在派生的setData()中执行此操作,但仍要调用QFileSystemModel :: setData()。
Meanwhile, I'm looking for why my QTreeView doesn't update the timestamp when I modify a file (unless I quit and restart my app, kind of defeats the purpose!) ... looks like the dataChanged() signal isn't getting emitted. 同时,我正在寻找为什么我在修改文件时QTreeView不更新时间戳(除非我退出并重新启动我的应用程序,否则会破坏目的!)...看起来dataChanged()信号不是被发射。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.