What's the best Area to draw QRects and QPixmaps to?
-
I'm building a tool which will take a Screenshot of the whole screen, and then open a new fullscreen window which just displays the screenshot.
Now I want to draw a bunch of rectangles onto that window. (but not onto the actual pixmap!)
I've been experimenting with a QGraphicsView and QGraphicsScene, until I found out that everytime I add a new item (QGraphicsScene::addRect()) the QGraphicsScene tries to make room for that via collision detection, looks like this at first:
but after adding some rectangles the image moves:
So I am searching for a way to efficiently draw rectangles onto the screen (and be able to remove them again) in Qt Widgets.
Thanks!
-
@ofmrew said in What's the best Area to draw QRects and QPixmaps to?:
In sounds like you want to do something similar to double buffering: draw the pixmap and then draw the graphical objects. To remove the graphical objects, just redraw the pixmap. I use that approach for rubberband lines and ruberband rectangles.
I like that idea, how do I do that?
-
@mrousavy Try this: 1. Create pixmap. 2. In paintEvent() draw the pixmap. 3. Continuing in paintEvent() draw your rectangle, etc. That is it. If you are using QtCreator, you will need to create a canvas class that is a subclass of QWidget. Drag-and-Drop a Widget onto the UI and then promote it to your canvas class. The paintEvent() is add to the canvas class.
-
@ofmrew said in What's the best Area to draw QRects and QPixmaps to?:
@mrousavy Try this: 1. Create pixmap. 2. In paintEvent() draw the pixmap. 3. Continuing in paintEvent() draw your rectangle, etc. That is it. If you are using QtCreator, you will need to create a canvas class that is a subclass of QWidget. Drag-and-Drop a Widget onto the UI and then promote it to your canvas class. The paintEvent() is add to the canvas class.
But then I'll be redrawing the whole QPixmap everytime I move the mouse, that's very expensive isn't it..?
-
@ofmrew said in What's the best Area to draw QRects and QPixmaps to?:
@mrousavy Absolutely not, I did this back in the 80386 days, it is a BitBlit or just a byte copy operations, unless there is a size mismatch , but even then it is trivial. Do you need an example or do you have it?
I think I've got it but it's really lagging as soon as I draw it on screens with sizes like 2560x1440 - My approach before was way faster :/