QPixmap variable wont update when drawing
-
So I have a QPixMap that is used to draw multiple instances of the same image with the goal of being able to change the QPixMap to then change all instances that are already being drawn.
my setup is something like this:
// in my header file nested in a QGraphicsScene child class struct _InstanceData { uint m_x; uint m_y; }; struct _PaintData { QPixmap m_ImageToDraw; const QImage* m_SourceImage; std::vector<_InstanceData> m_Instances; }; // in my header file as a member _PaintData m_PaintData; // in my source file to create a new QPixMap from a QImage void QGraphicsSceneChild::RefreshImage(const QImage* image) { if(image == m_PaintData.m_SourceImage) { m_PaintData.m_ImageToDraw = QPixmap::fromImage(*image); // a breakpoint will hit here // _PaintData.m_ImageToDraw.fill(Qt::red); // doesnt work either } update(); } // in my source file to draw each image. void QGraphicsSceneChild::drawBackground(QPainter *painter, const QRectF &view_rect) { QGraphicsScene::drawBackground(painter, view_rect); QRect tile_rect(0, 0, 128, 128); for(const _InstanceData& instance : m_PaintData.m_Instances) { tile_rect.setX(instance.m_x); tile_rect.setY(instance.m_y); painter->drawPixmap(tile_rect, m_PaintData.m_ImageToDraw); } }
Everything in regards to painting each instance does work as I expect when the QPixMap is first created. However, when I call RefreshImage(), passing in a pointer to the original QImage that gets modified in the parent window, it will not update.
I have also tried deep copying the QImage before creating the QPixMap; creating a new QPixMap and calling swap(); and even creating a new QImage and filling it before creating the QPixMap. I feel like theres something im missing and so any help would be appreciated.
thanks
-
@RetroZelda
This is only a theory! Are you sure yourupdate()
is really causing the whole scene to be repainted? I can't recall if it works that way, or only causes updated areas to be repainted. Do you need to call void QGraphicsScene::invalidate(const QRectF &rect = QRectF(), QGraphicsScene::SceneLayers layers = AllLayers)?Put a
qDebug() << view_rect
into yourdrawBackground()
, both to determine when it is being called and what rectangular area gets redrawn. -
I just attempted calling invalidate() and it gives the same results
i can also confirm that the proper areas do get redrawn in drawBackground(). I put something like this above where im drawing the QPixMaps to force a flicker on regions that get drawn.
static uint hack = 0; if(++hack % 4 != 0) return
-
@RetroZelda said in QPixmap variable wont update when drawing:
if(image == m_PaintData.m_SourceImage)
Are you sure this is correct? Why do you work with pointers at all and why do you do this comparison?
-
@Christian-Ehrlicher that line is correct. I use the pointer as a lookup as this is part of a larger chunk of code. My example was just a minimized example that reproduces the issue. The check passes as expected for what im doing, so I dont think its necessarily related to this problem(but i also am still very new to QT so I could be wrong).
essentially I have QImages that I have modifiable inside the program and when they get modified it seems like the QPixMap needs to be recreated in order to display the changes. So im currently using the address of the QImage in order to find the QPixMap that I need to update. This of course can be changed if theres a better standard for a QT codebase, I am just not aware of anything like that.
however, with or without the QImage pointer, the issue is still present that the QPixMap doesnt want to update when I attempt to set it to either the QImage that gets passed in, or through a brand new QImage on the stack or heap.
-
Please provide a minimal, compilable example - your code above looks fishy, esp. the pointer comparision but otherwise ok so there must be a problem somewhere else.