PDA

View Full Version : View, Scene, Item and thread??



dungsivn
19th August 2008, 02:34
Hi all,
I create a small application with QGraphicsView, QGraphicsScene, QGraphicsRectItem and thread: a view display many icons( subclass QGraphicsRectItem and QThread) in left, right, and one in center of view. When user click on a left item, the central one will move to right and the clicked one will move from left to center( animation). Each object icon subclass from QGraphicsRectItem and QThread:


class RectIcon: public QThread, QGraphicsRectItem
{
RectIcon(QString imagePath); //contain the path of image to load.

void paint(...); // draw the item
protected:
void run();


private:
void toLeft();//move the image from right or center to left
void toRight(); // change the position by rotate and transform.
void leftToCenter();
void rightToCenter();

};

RectIcon::run() // this will be called when user click on one item, this item will be move to center and the central one will to right or left.

{
while( _canRun)
{
toLeft();
toRight();
leftToCenter();
rightToCenter();
usleep( 19000);
}
}


When user click on one item( left or right), the view will specify the clicked item and call start() function, in run() function, we will translate, rotate and change the position of this item. in a short time( after running the application about 3-4 minutes), it's right- the clicked item can move to the center. after that, although the item change the position but the view is not refresh to see animation. to see the latest images, we must resize the view window.

Could you tell me why and how to resolve this problem( view can refresh)?
Thanks!

jacek
19th August 2008, 02:49
First of all you shouldn't mix GUI and threads or Bad Things(tm) will happen.

See QGraphicsItemAnimation, QTimeLine and QTimer.

dungsivn
19th August 2008, 02:58
thanks, first, we use QTimeLine to animate all items but it's not flexible, smooth and so slow with many icons( about 100 icons). So, we converted it to thread. We use QgraphicsRectItem to get the border of rect item, monitor other things( right-click to open maximize, minimize the correspoding window, change the mode of view( quick view, cascade, browser view, tile view...). I think QGraphicsRectItem is more suitable.

Any other idea? :)

wysota
19th August 2008, 07:09
You don't need 100 timelines nor 100 threads. Have a single animation controller that will periodically (QTimer) move all the animating items based on their state.

dungsivn
20th August 2008, 16:19
yes, that's right. But, we add another feature into the application: when the mouse is over one item( hover), that item icon will jump( change the y-axis position) while other items are moving. If we use timeline with jump feature, it is very slow with 100 item icons.
Please give me a solution with thread.
thanks!

wysota
20th August 2008, 19:21
Why do you think threads will help you?