QStandardItem subclassing?
-
wrote on 31 Jul 2012, 07:20 last edited by
Hi Beka,
The post is more than 1 year old. So for the solution you can post your code.
-
wrote on 31 Jul 2012, 07:44 last edited by
I need to override method of QStandardItem operator<(), but thats not the problem, problem is i dont know how to use myItem in models
-
wrote on 31 Jul 2012, 07:49 last edited by
Did you try just adding your own item instance instead of the standard one? What happened?
-
wrote on 31 Jul 2012, 08:01 last edited by
I have subbclassed QStandardItemModel already, so I just try to set in constructor something like this: this->setItemPrototype(new myItem());
-
wrote on 31 Jul 2012, 08:04 last edited by
You normally should not need to subclass QStandardItemModel itself, you should be able to use it without subclassing.
Anyway, your call to setItemPrototype should work. However, did you think to reimplement the QStandardItem::clone() method to return an instance of your own class?
-
wrote on 31 Jul 2012, 08:07 last edited by
Yes, I did it, and i debug if it is called, and it works, but my method operator<() is never called
-
wrote on 31 Jul 2012, 08:19 last edited by
I just tried to create two new myItems and compare them, it works, but thats predictable
but when I called for example myModel->item(r,c)->operator <(other myItem) it isnt called -
wrote on 31 Jul 2012, 08:21 last edited by
Could you show us the signature for that function from your header? Did you happen to forget the const perhaps?
-
wrote on 31 Jul 2012, 08:34 last edited by
@ bool operator <(const myItem &other) const;
virtual myItem *clone() const;@and, after i setItemPrototype, will it take care of retrieving myItem insted of QStandardItem? or i must reimplement or functions which i am working with?
-
wrote on 31 Jul 2012, 08:37 last edited by
Are you sure about that space between operator < ?
And yes, thanks to polymorphism, your own function should be called if it actually is an object of your own type. How do you actually set your items? The prototype is just for editing and things like that. How do you fill your model?
-
wrote on 31 Jul 2012, 08:40 last edited by
no, i delete it
i fill it with functions setItem or setData
-
wrote on 31 Jul 2012, 08:43 last edited by
Show, not tell. It is hard to figure out where the mistake in your code is if you don't show your actual code...
-
wrote on 31 Jul 2012, 08:46 last edited by
yes i know, but the code is really big...
@
myModel->setItem( row, column, new myItem(QIcon(icon), "some text") );
myModel->setData( myModel->index(row, column), "text" );
@ -
wrote on 31 Jul 2012, 08:49 last edited by
So, extract the relevant sections yourself, or create a small, self-contained example that shows the problem. Also, when posting code, please use @ tags around your code section (I already updated your post in this case) to improve readability.
Your code, as far as I can oversee it, looks fine to me. I have no idea why it should fail.
-
wrote on 31 Jul 2012, 08:52 last edited by
Okay thank you, i try to override some other methods on model, which can be the problem, and if it wont wors, i paste the code
-
wrote on 31 Jul 2012, 09:03 last edited by
I have one more question, if I override operator<() will it be called by QStandardItem::sort() ?? because i was said to override operator<() to sort like we want, but would be it ever called from sort() ??
-
wrote on 31 Jul 2012, 09:04 last edited by
It should be called from there yes. It is the whole point of the function being there and being virtual.
-
wrote on 31 Jul 2012, 10:03 last edited by
I dont know how to reimplement method item, that it will return myItem, i looked into the sourcecodes, but i dont know what to do... a just think about copy data from myModel->item(r,c); but the method return the pointer, and i will then dont return the exact pointer
@QStandardItem *QStandardItemModel::item(int row, int column) const
{
Q_D(const QStandardItemModel);
return d->root->child(row, column);
}@and i think about the reimplementing the setItem, because i dont know either how to do it :(
i dont know what to do... sources doesnt help me much, that are so many functions i cant use
16/24