使用自定义模型带有复选框的 qlistview
我对 filesystemmodel 进行了子类化,以在 listview 中包含复选框,效果很好。我的问题是,每当我单击一个项目时,该项目的文本就会消失,而当我单击另一个项目时,先前选定的项目的文本就会变得可见。谁能告诉我其背后的原因。
这是我实现的代码。
请告诉我我在这里缺少什么, 谢谢
#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);
}
I have subclassed filesystemmodel to include checkboxes in listview , which is working fine. 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定它是否相关,但我在以下位置找到了以下注释:
http://doc.trolltech.com/4.6/qt.html#ItemFlag-枚举
据我所知,您的代码看起来是正确的 - 但也许尝试在基本
QFileSystemModel
类(在您的自定义构造函数中)上设置ItemIsUserCheckable
标志,看看是否继承的 data() 和setData()
方法与role=Qt::CheckStateRole
一起使用。如果您因其他原因需要维护当前检查内容的列表,请继续在派生的
setData()
中执行此操作,但仍调用QFileSystemModel::setData() 也是如此。
Not sure whether it's relevant, but I found the following note at:
http://doc.trolltech.com/4.6/qt.html#ItemFlag-enum
As far as I can make out, your code looks correct -- but maybe try setting the
ItemIsUserCheckable
flag on the baseQFileSystemModel
class (in your custom constructor), and see if the inherited data() andsetData()
methods work withrole=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 callQFileSystemModel::setData()
as well.