Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to make a graphic item paint itself AND its children ?



  • Hello,
    I was wondering if there was a way to make the paint method of the QGraphicsItem ( virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) ) also paint all the children of the item ?

    This would correspond to the behavior of the QGraphicsView::render method, but for the QGraphicsItem…

    Thank you in advance for your answers !


  • Qt Champions 2017

    This should straight away work. Items are painted by the view, starting with the parent items and then drawing children, in ascending stacking order.

    Did you face or saw any problem. Do let me know. I can look at it.



  • Hello, thanks of your answer.
    The thing is that I want to export items painting them (via a VExport class and its paintOnDevice method that was created in my code) in an image or pdf, using the QGraphicsItem::paint method (see code below). But of course the result in the image or pdf is the object alone, when I would like the image to include all the children.

    I made another method paintChildrenOnDevice that call the paint method of the children, but I was wondering if there was an easier way to do it, using Qt possibilities…

    @void VExport::paintOnDevice( QPaintDevice * device , QGraphicsItem * item , float xScaleFactor , float yScaleFactor, float dx, float dy )
    {
    QPainter painter;
    painter.begin( device );
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);

    QStyleOptionGraphicsItem option;
    option.exposedRect = item->boundingRect();
    painter.scale( xScaleFactor , yScaleFactor );
    painter.translate( -item->boundingRect().topLeft() );
    painter.translate( dx, dy);
    item->paint( &painter , &option , 0 );
    QList<QGraphicsItem*> list = item->childItems();
    paintChildrenOnDevice(&painter, option, list);
    painter.end();
    };@

    thanks,



  • Hi,

    why QGraphicsView::render() does not fit your needs? you can control the source rect which should be your item's rect and in case you have to render an item overllaping others just hide the other ones before the rendering ...


Log in to reply