PDA

View Full Version : Binding data to a a QGraphicsItem (or something like that)



been_1990
10th August 2009, 21:47
Ok, I have a QGraphicsScene full of QGraphicItem's.
What I hope to achieve is to somehow "assign" a QString to each of the QGraphicItem's when they are created.
Ex:

create QGraphicItem
assign QString "this data" to it

By doing that, when a user clicks on the respective QGraphicItem, I can call a function passing as an argument the previously assigned QString.


when I click on QGraphicItem
call useData(assigned QString)

Makes sense? Hope so.
:o

wagmare
11th August 2009, 06:51
and its working aa ..? urs seems to be useful for me ..
becuase previouly i am doing signal mapping and emitting th signal ..

hayati
11th August 2009, 09:22
why don't you use inheritance?

been_1990
11th August 2009, 16:21
How would I use innheritance? One thing I miss very much from Actionscript 3 , Is that doing this is as simple as:


var rHolder:Array = new Array();
for(var i:int=0;i<10;i++){
rHolder.push( new Object() );
rHolder[i].data= "my data goes here";
rHolder[i].info = "my info";
rHolder[i].movieClip = new card(); // card here being the movieClip class // new on AS3
}

Is there a way I can simulate that?
Another option is maybe to create a array with the info in order that the GraphicsItem's are being made:


new array;
for(...)
create QGraphicsItem;
array[i] = " QGraphicsItem just created data";

Then:

When QGraphicsItem is double clicked{
int num = this items number in order of creation;
myFunction( array[num]);
}

I dont know...

wysota
11th August 2009, 16:50
See QGraphicsItem::setData() and QGraphicsItem::data().

been_1990
11th August 2009, 18:15
Oh... So simple. Does that take a lot of data, or is it just for small numbers and\or strings? For me its just what I need.

If I want to retrive that data when the item is clicked, how is that done?I've tried this:

bool DropArea::eventFilter(QObject *object, QEvent *event){



if (event->type() == QEvent::GraphicsSceneMouseDoubleClick)
{
dblClick(object);
return true;
}
return false;
}

void DropArea::dblClick(QObject *item){
// how do I now if this object is a QGraphicsItem?
}
Thanks for the info.

wysota
11th August 2009, 21:10
It takes everything that fits into QVariant (which means almost anything).

Graphics items are not QObjects, you can't do it this way. The proper way is to subclass either the scene or the item and use its proper event handler.

been_1990
11th August 2009, 22:28
So I need to subclass void QGraphicsItem::mouseDoubleClickEvent()? Where do I find its "contents"?

wysota
11th August 2009, 22:30
So I need to subclass void QGraphicsItem::mouseDoubleClickEvent()?
You subclass a class, not a method. You reimplement a method.


Where do I find its "contents"?

What contents?

been_1990
11th August 2009, 23:34
With "contents" I meant the actuall definition of them. The Docs only have a little explanation on what it does, not the code.

You subclass a class, not a method. You reimplement a method.
Oh. Good to know. Then what do I do? Sorry. Thanks for helping. :)

wysota
12th August 2009, 00:43
With "contents" I meant the actuall definition of them. The Docs only have a little explanation on what it does, not the code.

The source code is usually not in the docs but in the.... source code. But why would you care? Just call the base class implementation when it's appropriate and the routine will be executed.


Oh. Good to know. Then what do I do?
That I do not know. But you surely use improper terms making it harder to understand you.

been_1990
12th August 2009, 02:50
Sorry for my lack of C++ jargon knowledge. :o
Well you said:

Graphics items are not QObjects, you can't do it this way. The proper way is to subclass either the scene or the item and use its proper event handler.
What did you mean? How will I " subclass either the scene or the item and use its proper event handler."?

wysota
12th August 2009, 07:42
Event handlers are methods that are called when an event occurs. Examples of such methods are QGraphicsItem::mouseMoveEvent() or QGraphicsItem::dragLeaveEvent(). And I'm sure you know what subclassing is.

been_1990
12th August 2009, 17:49
Ok, I actually searched Google for subclassing and now I understand...
When I said:

Originally Posted by been_1990 View Post
So I need to subclass void QGraphicsItem::mouseDoubleClickEvent()?
I actually meant:

So I need to reimplement void QGraphicsItem::mouseDoubleClickEvent()?
Thanks for your patience! :) You are a real help!

been_1990
12th August 2009, 18:19
Now, how do I get them QGraphicsItem that was clicked? Theres only a QGraphicsSceneMouseEvent * mouseEvent .

wysota
12th August 2009, 18:52
Please... read some Qt docs... and learn at least the basics of object oriented programming :crying:

QGraphicsItem::mousePressEvent()

QGraphicsScene::itemAt()

been_1990
12th August 2009, 20:43
Ok, sorry.. hehe

been_1990
13th August 2009, 02:39
I have a QGraphicsView subclass as my "main function"(the right name is?) I i have reimplemented QGraphicsItem::mouseDoubleClickEvent:


void QGraphicsItem::mouseDoubleClickEvent ( QGraphicsSceneMouseEvent * event )
{
qDebug() << " mouseDoubleClickEventt in Item" << event->scenePos() << this;
}

Until here everything works well, :this: returns the item I just clicked. But obviously I can't call any other function from my class DropArea from the above. How can I call a function from another class? Dont even need to give me the answer, just point me where to read on it. :)

wysota
13th August 2009, 08:33
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html