How to grab a qwidget /render it in a pixmap everytime it needs to be repainted?

前端 未结 3 1191
我在风中等你
我在风中等你 2021-01-06 03:27

I have a QWidget that I don\'t want to show on screen. Instead I want to get a pixmap of the widget every time it is repainted, in order to send it to another part of the ap

3条回答
  •  悲哀的现实
    2021-01-06 04:13

    As another way - you can do next check:

    void paintEvent(QPaintEvent*)
    {
      QPainter painter(this);
      // If painter redirection was turned on, the painter will *not* paint on `this`!
      // Painting code
      //...
      if ( this == qobject_cast< QWidget * >( painter.device() ) )
      {
        // Do grabbing
      }
    }
    

    paintEvent is called both for painting on widget and on grabbing. painter->device() will return not null, but instance of QWidget * object only when current painting is on widget. In case of drawing on QPixmap - it will return QPixmap *. So you need to call grabbing code only when painting is performed only on real widget.

提交回复
热议问题