PDA

View Full Version : QGraphicsItems somtimes not diplaying



JonathanForQT4
1st June 2007, 15:01
Hello all, I have a rather weird problem.

I have a class started in QThread which sends via signals/slots to another class information for a QGraphicsItems which are then placed on the QGraphicsScene. The problem arises when I send too many of these items....sometimes some items simply don't get diplayed; it is completely random which ones don't get drawn on the scene...and it is always just a couple that don't get displayed...the majority 98% or so always do...

here is my code in the slot that catches the signal from the thread.

void myClass::getItem(...){
m_items.push_back(new itemOnScene(...));
m_mainScene->addItem(items.back());
m_items.back()->setPos(QPointF(0, 0));
m_items.back()->setVisible(true);//defualt is true anyways...just to make sure...
m_items.back()->update();//just to make sure...
}

if I add Sleep(10); after the update call all items are displayed 100% of the time...what's going on with QT? A signal being sent too close after a previous one gets lost? What's going on here? Does this have to do with how items are painted?

Thanks for your help in advance...a truly baffling problem...

Jonathan

Edit://even works all the time with Sleep(1);

wysota
1st June 2007, 16:09
Could you verify that the slot is called each time an item should be added? It'd help to see if the problem is within the slot or outside it.

marcel
1st June 2007, 18:38
If you add a lot of items in a short period of time, then don't call update for each and every one of them. Call update once, after you finished adding all the items.

This most likely is some update problem, rather than a signal/slot problem.

aamer4yu
3rd June 2007, 07:10
By the way u are adding the items at (0,0) . That might be a problem in updating as marcel told. Try positioning them at different positions and see if the problem exists.

JonathanForQT4
4th June 2007, 11:12
I should add that I move it to 0, 0 at first because I get another signal sent to a slot which then sets the bounding rect to where the rectangle should be painted.

When I create the qgraphicsitem I do not know where it should be placed..this comes afterwards due to the class (which is the qthread) communicating with another program.

So the sequence of events is:

code posted above.

m_items.back()->setRect(rect_passed_in_through_signal);

Within the QGraphicsItem (note:rect_passed_in_through_signal == m_rect) :

QRectF item::boundingRect() const{

return QRectF(m_rect);
}

void item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){

...

painter->drawRect(m_rect);
...
}

marcel: calling update afterwards does not help.
wysota: each slot is called for sure, when I trace through the program while debugging the problem does not happen.
aamer4yu: why would this help anything?

JonathanForQT4
4th June 2007, 11:22
could it be that the boundingRect() or Paint() within the qgraphicsitem is called before the signal with setRect is called...?

I'm guessing it has to do with m_rect in the Qgraphicsitem not getting set at the right time somehow...because sometimes you can see the item on the scene, but when you zoom in it's not actually there...which would seem like it's getting painted there but the bounding rect is false or something....any ideas?

aamer4yu
4th June 2007, 11:50
I hope that will help because u are using threads...
see u are adding too many items at once... and it will depend how the paint of the items will be called. U said the problem is not there when u add sleep... so i guessed that might be the problem
Also try not to draw until u are not passed the rectangle... u can keep some extra variable for that check.

JonathanForQT4
4th June 2007, 12:51
tried changing the setPos...in the same slot that catches the signal with the rect....and it's not working.

I've tried setting the pos to lowerleft, upperleft.....I don't see the rect anywhere now.....but am still trying things

wysota
4th June 2007, 13:21
Could you explain what the thread does? Maybe some relevant code would clear up things a little bit...

JonathanForQT4
4th June 2007, 16:49
what's going on in the thread is that the program is getting the rectangles to be displayed on the scene and sends them via signal/slot...if you need to know more read my other threads, I think I explain it more in detail there.

The thing to note is that I can get a notice that there is an object of type x and thereafter then get a notice of type y object....but the rect for object x comes sometime after y, therefore the different methods.

In any case, I have been doing some futzing around and found out that when the boundrect function is called and the m_rect has not been set....the object is then not seen...

so just before I set the m_rect I now call prepareGeometryChange() and everything works fine :)

edit://damnit, why can't I add thanks to myself :P