Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Dirty pixels after dragging parent QGraphicsItem.

[Solved] Dirty pixels after dragging parent QGraphicsItem.

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rhadel
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rhadel
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved