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 correctly override paint in QGraphicsItem?

How to correctly override paint in QGraphicsItem?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 1.8k Views
  • 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.
  • clostridium_difficileC Offline
    clostridium_difficileC Offline
    clostridium_difficile
    wrote on last edited by
    #1

    Hello. I need to rasterize QGraphicsItem. My solution for this, is to override paint method in QGraphicsItem. But what this function should do? For now, this method is not overrided but on output image I have random noise.

    1 Reply Last reply
    0
    • clostridium_difficileC Offline
      clostridium_difficileC Offline
      clostridium_difficile
      wrote on last edited by clostridium_difficile
      #2

      For now I Have

      QImage image(item->rect().size().toSize(), QImage::Format_RGB32);
      image.fill(Qt::transparent);
      QPainter painter(&image);
      painter.setRenderHint(QPainter::Antialiasing);
      QStyleOptionGraphicsItem opt;
      item->paint(&painter, &opt, nullptr);
      
      QDialog dialog;
      QLabel label;
      label.setPixmap(QPixmap::fromImage(image));
      label.setParent(&dialog);
      dialog.show();
      dialog.exec();
      

      But on the preview there is a black square rather than rasterized item.
      Edit:

      void Item::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
      {
      	painter->drawRect(boundingRect());
      }
      
      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        What exactly are you expecting ?
        Your paint method just draws a rectangle with whatever configuration painter has.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • clostridium_difficileC Offline
          clostridium_difficileC Offline
          clostridium_difficile
          wrote on last edited by clostridium_difficile
          #4

          @SGaist said in How to correctly override paint in QGraphicsItem?:

          Hi,

          What exactly are you expecting ?
          Your paint method just draws a rectangle with whatever configuration painter has.

          Yeah, I noticed that xD For now, i use this example: https://gist.github.com/dokinkon/4275597 but rendering area is not in local coordinates, but in scene coordinates. I want to render whole QGraphicsRectItem to the image without any tranformation. Do you know to do this?

          1 Reply Last reply
          0
          • clostridium_difficileC Offline
            clostridium_difficileC Offline
            clostridium_difficile
            wrote on last edited by
            #5

            nevermind. I save position and rotation at the beginning of this function, then set pos to (0, 0) and rotation to 0, render item and at the end – change it back to previous values xD

            QImage Item::ToImage()
            {
            	QPointF startpos = pos();
            	qreal startRotation = rotation();
            	setPos(0, 0);
            	setRotation(0);
            
            	QRectF r = rect();
            	QImage image(r.width(), r.height(), QImage::Format_RGB32);
            	image.fill(QColor(0, 0, 0));
            	QPainter painter(&image);
            	painter.drawRect(r);
            	scene()->render(&painter, QRect(), sceneBoundingRect());
            	painter.end();
            
            	setPos(startpos);
            	setRotation(startRotation);
            
            	return image;
            }
            
            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