PDA

View Full Version : Subclasing QAbstractItemModel Issue



wolfi3b
23rd September 2010, 13:13
Hi,
im working on one application and I need to subclass QAbstractItemModel. I googled which methods I need to reimplement to make it work. Basic Idea is I need to reimplement mainly data methode (the others just to do the same as usualy), to choose what is returned. Problem is, if I return anything else than simple QVariant(), for example if I return 1 (I also tryed conversion of everything I tryed straight to QVariant befor returning) result looks like this:
http://lh4.ggpht.com/_BoJz15FOeMM/TJsuMHQMizI/AAAAAAAAABc/l4SpEfKSFWc/s144/checkbox.jpg
Im not able to figure out from where these checkboxes appeared there. I can return anything, and still, here they are. Its different when I return like 0 or 1. On 0 its unchecked on 1 its checked. But still, I cant figure it out.

I know maybe I just forgot something basic, or Im just blind to see the problem and it can be something realy simple, but I tryed for some time and Im out of ideas.
Can please anybody help me with this? I would realy appreciate any help.

PS: I put this class in simple project, sou if needed, you can try to run it and see what it is doing.

ChrisW67
24th September 2010, 08:21
The check boxes will be shown by the standard delegate if the flags() method returns Qt::ItemIsUserCheckable (possibly also Qt::ItemIsTristate) among the flags. It also seems that if data() returns anything other than an empty QVariant() for the Qt::CheckStateRole the check boxes are displayed also. Your implementation of data() does not distinguish on role and returns QVariant(1) for all roles.

wolfi3b
24th September 2010, 19:29
Thank you for your answer. I lknow its returning 1 all time, I meant it just like example. What I didnt know was, why these check boxes will be there. Can I ask you where exactly did you get this info that these checkboxes are there by default? Can you post here a link? Im goint to try it right now, but still Im quite disapointed I didnt found it by myself in documentation :)

EDIT: Ok, the problem was realy the flags and role. Thank you for help and plesase if its possible can you still put here that link? I know I have to be blind. I suppose its gona be in bold somewhere nobody can miss it :p

wysota
24th September 2010, 19:55
The link is called "experience".

wolfi3b
24th September 2010, 21:22
Nice, I hope I can get this source as well :-)
One more question. Lets say I want to set all cells content to specific picture using this methode. Again its just example to figure out how exactly its working. I tryed it like this:


QVariant TabsModel::data ( const QModelIndex & index, int role) const
{
if (!index.isValid())
return QVariant();

else if(role == Qt::DisplayRole)
{
QPixmap img("C:/Tab/box.png");;
return QVariant::fromValue(img); // I tryed straight return img; as well
}
else return QVariant();
}


There is no warning no error. but problem is when window show, there is no picture in any cell.

Lykurg
24th September 2010, 22:00
I guess your problem is more on the receiver side than on the sender:
#include <QtGui>

QVariant image()
{
return QPixmap("/usr/share/httpd/icons/tex.png");
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QVariant v = image();
QPixmap p = v.value<QPixmap>();
QLabel l;
l.setPixmap(p);
l.show();

return app.exec();
}

wysota
24th September 2010, 22:15
You should read the docs on the model-view approach and especially the part where they say about data roles.

wolfi3b
24th September 2010, 22:21
On reciever side? Hmm...Its reimplementing data methode, Im not sure what probelme on reciever side it can be....I thought if I do it this way, using should be just like normal. Or I have to change something more than I did?

Lykurg
24th September 2010, 22:29
Sorry I did not read careful enough and thought - since returning a pixmap as a QVariant is just fine - you have trouble to convert the variant back and so you are unable to paint the pixmap.

wolfi3b
24th September 2010, 22:46
hmmm...but problem? I just read about subclasing to check it from here
http://doc.qt.nokia.com/4.6/model-view-creating-models.html

As I understand, it should be enough to create methods which I have. I supposed if I created data methode, AbstractItemModel should be able to work with default mehtods for finish it.

ChrisW67
25th September 2010, 06:01
The link is called "experience".

I used the Suck-it-and-see design pattern with this one ;)

wysota
25th September 2010, 08:53
I'll be gentle and I will stuff the answer right in front of your nose this time: you are using a wrong role.

wolfi3b
25th September 2010, 10:23
Thanks your for patience. I just figured it out and its working now. I know sometimes Im little bit blockheaded(hope its the right term in english :) ).

One more time thank you guys, you didnt give up with me.