0

I'm using Qt5.5, I want to create an offscreen image then copy specific parts of the offscreen image back to onscreen (visible) area.

Can anyone point me to a good example on how to create an offscreen image of a specific size, draw something on it, then copy a specific part of it (rectangle) from the offscreen image to the visible area.

SPlatten
  • 5,334
  • 11
  • 57
  • 128

1 Answers1

1

I think you can create a QPixmap and then draw your image using a QPainter built on it...

Something like:

  QPixmap pix(500,500);    
  QPainter paint(&pix);
  paint.setPen(QPen(QColor(255,34,255,255)));
  paint.drawRect(15,15,100,100);

Then, you can draw a QPixmap on the screen as usual (in QML or Widget-based application).

thuga
  • 12,601
  • 42
  • 52
Morix Dev
  • 2,700
  • 1
  • 28
  • 49
  • Thank you, I haven't come across QPixmap before, will investigate now. – SPlatten Mar 11 '16 at 12:27
  • 2
    @SPlatten - if you are using a secondary thread to draw to the image, you better use `QImage` instead `QPixmap`, as the latter can only be drawn onto from the main thread on some platforms. – dtech Mar 11 '16 at 12:31
  • Thank you, I am creating a compass strip that will show various indicators on it, but the idea is to create the entire strip in the pixmap, then copy only the visible relevant part to the visible area. – SPlatten Mar 11 '16 at 12:40
  • 1
    @SPlatten - for custom graphics you really need to consider QML instead of the old QWidgets stack, it is so much easier and faster to work with. Widgets really only shine when creating the "classic desktop GUI applications". – dtech Mar 11 '16 at 12:43
  • @ddriver, I haven't looked at QML but will investigate, I'm developing a GUI for a military application. – SPlatten Mar 11 '16 at 12:45
  • Your answer had a memory leak. I fixed it for you, but do be careful in the future. If there is no good reason to initialize something using `new`, then you probably shouldn't do it. – thuga Mar 11 '16 at 13:27
  • @ddriver, when you say QML is faster, do you mean for the developer? As I can't really see how loading a file and interpreting it can be faster than code. – SPlatten Mar 11 '16 at 18:49
  • 2
    @SPlatten - it is much faster to develop and prototype with it. No need to recompile to test, errors are usually caught and produce meaningful messages rather than crashes. It is also faster in term of graphics framerate. Yes, interpreting does come at a cost, but in my experience it is usually negligible. And in Qt 5.8 the QML compiler will become part of the free Qt, so it will soon be possible to finally compile all the QML to C++ code and avoid the interpreting altogether. BTW interpreting allows for a lot of cool stuff when combined with code generation at runtime. – dtech Mar 11 '16 at 19:16
  • 1
    And by "soon" I mean hopefully by the end of the year. Digia promise to release 5.8 this year, but they do have the tendency to lag 2-3 months behind schedule. And by "much faster to develop and prototype with it" I mean like 5-10 times faster, measuring my own experience with widgets, graphicsview and qml. – dtech Mar 11 '16 at 19:19
  • @ddriver, Thank you I will definately investigate, what I want to do is render to an offscreen QPixmap then copy sections of the offscreen image to the visible area, can QML be used with an offscreen context? – SPlatten Mar 11 '16 at 19:50
  • 1
    @SPlatten - you will rarely paint with QML, it is just a different way of doing things - you declare the structure of what you want rather than drawing it imperatively as with QPainter, that being said it has a Canvas element, which you can paint onto traditionally even if it is not visible on screen, and then paint a portion of the offscreen canvas to an onscreen canvas. In QML objects usually paint themselves, and the scenegraph takes care of what is on screen, what is not and what's the most efficient way to draw things. – dtech Mar 11 '16 at 20:10