[Solved] Subclassed QStandardItem - setFlags() doesn't work
-
Hello! I have CollectionItem which subclasses QStandardItem, and when I try to setFlags() on it - nothing changes. Here's code:
@
CollectionItem* item = new CollectionItem("Your collection is empty! Visit settings", "", "", 0, false);
qDebug() << item->flags();
item->setFlags(0);
qDebug() << item->flags();
appendRow(item);QStandardItem *i = new QStandardItem();
qDebug() << i->flags();
i->setFlags(0);
qDebug() << i->flags();
@Output:
@
QFlags(0x1|0x2|0x4|0x8|0x20)
QFlags(0x1|0x2|0x4|0x8|0x20)
QFlags(0x1|0x2|0x4|0x8|0x20)
QFlags()
@ -
Could you post how your CollectionItem works?
-
@
class CollectionItem : public QStandardItem
{
public:
enum Roles {
NameRole = Qt::UserRole+1,
PathRole,
HashRole,
SizeRole,
SizeTextRole,
SubdirRole,
SubdirTextRole
};enum Type
{
Collection = QStandardItem::UserType + 1
};CollectionItem();
explicit CollectionItem(const QString name, const QString path, const QString hash, qint64 size, bool subdir);
QVariant data(int role) const;QHash<int, QByteArray> roleNames() const; /* getters of variables */ int type() const { return Type::Collection; }
private:
/* variables */
};@Basically I've changed CollectionModel to subclass QStandardItem today - because I needed to make an item nonselectable - before it was just QObject. I use it in Model as QList<CollectionItem> and access it mainly via Model::data (in for example ListView delegate).
-
Hmmm... weird! I thought that perhaps you had incorrectly overridden something relevant, but that doesn't seem to be the case.
-
I've found out that the problem is data() function - I have
@
QVariant CollectionItem::data(int role) const
{
switch(role)
{
case Qt::DisplayRole:
return name();case NameRole:
return name();case PathRole:
return path();case HashRole:
return hash();case SizeRole:
return size();case SizeTextRole:
return sizeText();case SubdirRole:
return subdir();case SubdirTextRole:
return subdirText();default:
return QVariant();
}
}@When I delete it, everything works. My guess is that flags are somehow returned? by this function.
Edit:
Yeah, I've solved it by adding:
@
default:
return QStandardItem::data(role);
@
instead of returning empty QVariant(). Anyways, it'd be nice to mention it in documentation. -
Good catch. It could be that the flags are stored in a custom role internally. I did that for a similar class I wrote once too... However, it is a bit weird that reimplementing the data() method would influence the flags() function. Please report a bug in the bugtracker on this issue.
-
Mattrick, could you post what you include to make 'type()' work? I subclassed QStandardItem myself, adding constructor, copyconstructor, clone, my own variables etc. but when overriding type, I get the error 'Type' is not a class or namespace, although I declared it similar to your post: enum Type is placed in the headerfile of the class as well as the function 'type()'.
[quote author="mattrick" date="1333989208"]@
class CollectionItem : public QStandardItem
{
public:
...
enum Type
{
Collection = QStandardItem::UserType + 1
};
...
int type() const { return Type::Collection; }
...
};@
...[/quote]my own header looks like this:
@#include <QStandardItem>class myStandardItem : public QStandardItem
{
//Q_OBJECT
public:
myStandardItem();
myStandardItem(QStandardItem *originalItem);
myStandardItem(myStandardItem item);
enum Type {Collection = QStandardItem::UserType + 1};
myStandardItem clone() const;QStandardItem *original; int type() const {return Type::Collection;}
signals:
public slots:
private:
};@