[Solved] Dirty pixels after dragging parent QGraphicsItem.



  • Hi,

    I've created a custom QGraphicsItem class that draws a QImage in its paint event. It has 2 attributes that are also custom QGraphicsItem class, they both draw 2 lines:

    @
    void CustomItemParent::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
    Q_UNUSED (option);
    Q_UNUSED (widget);
    if (!this->scene()) return; //Do nothing if scene is null.
    if (!painter) return;

    //Setting colors and all this stuff
    QPen pen;
    QBrush brush;
    brush.setColor(Qt::gray);
    pen.setColor(Qt::black);
    painter->setPen(pen);
    

    // Draw stuff
    @

    And children:

    @
    void CustomItemChild::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    Q_UNUSED(option);
    Q_UNUSED(widget);

    if (!painter) return;
    
    //Draw axon.
    QPointF initPoint(posx1, posy1);
    QPointF endPoint (posx1 + width - 5, posy1);
    painter->drawLine(initPoint, endPoint);
    
    QPointF initPoint2 = QPointF(posx1, posy1 + height);
    QPointF endPoint2 = QPointF (posx1 + width, posy1 + height); //Coordinates can be negative.
    painter->drawLine(initPoint2, endPoint2);
    

    //More painting stuff
    @

    Parent object is marked as movable with @this->setFlags(QGraphicsItem::ItemIsMovable);@. When I try to drag the object in scene, I can't appreciate the movemento of their children objects. I can see some dirty and random pixels drawn instead. It seems I can't draw the lines while they are being dragged in scene, just some pixels that they leave in their way. Only when scene->update() is called (if resize the main window, for example), I can view again the children lines in its right place.

    Any idea? How can I drag children items in a correct way (visible while dragging, and visible once I drop the item)?

    Note: children objects can be drawn in a negative coords (init point, f. ex. (-100, 20)).

    Thank you.



  • Maybe a good solution is:

    1. First, draw a Rectangle as first step when enter in paint event. This rectangle determines the local coordinates of the following items.
    2. Draw inside the main QGraphicItem and its children.
    3. My conclusion: the first item that you draw in paint event of your custom QGraphicsItem class, is the item that determines the local coordinates size. So, items drawn after have its coordinates in relation of this first item. Is it correct?

    ----EDIT----
    Just calling:

    scene()->update();

    solves the problem.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.