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. How to make a graphic item paint itself AND its children ?

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.0k 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.
  • C Offline
    C Offline
    camleroi
    wrote on last edited by
    #1

    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 !

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • C Offline
        C Offline
        camleroi
        wrote on last edited by
        #3

        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,

        1 Reply Last reply
        0
        • N Offline
          N Offline
          NicuPopescu
          wrote on last edited by
          #4

          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 ...

          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