Search code examples
c++qtbackgroundgridraster

How to make a raster in the background of a panel/tabPane/s.e


I try to create something, where you can drop different things on it, like the Qt Creator (no, i don't want to create a new one, but i need the function of that). You drag some elements and drop them anywhere in a tabPane. My problem now is how to make a grid/raster in the background. It should look similar to this:

https://i.sstatic.net/QOA4K.png

I mean those dots in the background. If I make them with two for loops, it will take hours and it's not efficient or anything else. There has to be a more efficient solution and a lot easier one.

I'm programming in c++ with Qt as a Framework. Please give me some links or anything else I can use.


Solution

  • You can:

    • limit the repainting to region that really needs to be updated as explained in QWidget::paintEvent documentation,
    • fill a container of QPoint in your loops instead of drawing the points, and draw them all with QPainter::drawPoints after the loop,
    • cache the result in a QPixmap with transparency and reuse it if the window size didn't change (example from Qt Quaterly).

    Of course, you should do some testing to see if you gain anything at all by doing any of these optimizations.