different behaviours using a Qpainter with Drawrects
-
Hi,
I'd like to produce the image of a matrix made of square with different colors. These colors are coming from a QVector<QRgb>, but even for tests when I "force" the yellow color this isn't working.The red is define After the yellow.
This problem is obvious when below I set all squares except the last 1 , which I try to define it as the only
one as Red but this ends like this :I do it that way :
uint MatxRows= MatxCols = 8 ; uint GridWith = 40, GridHeight = 40 ; QImage imageTest(GridWith, GridHeight, QImage::Format_RGB32); imageTest.fill(Qt::gray); QPainter p; p.begin(&imageTest); // Draw the grid p.setPen(QPen(QColor(Qt::black))); QVector<QLine> GridLines ; uint step= GridWith/MatxCols ; // Vertical Lines for (uint ind=0; ind < MatxCols ; ind++) GridLines.append( QLine( step+(ind*step),0,step+(ind*step),GridWith )); // Horizontal Lines for (uint ind=0; ind < MatxRows ; ind++) GridLines.append( QLine( 0, step +(ind*step),GridHeight,step + (ind*step) )); p.drawLines(GridLines); uint Current_Row = 0, Current_Col=0 ; QVector<QRect> MatxRects ; // p.setPen(QPen(QColor(Qt::red))); for ( uint BtnID = 0; BtnID < (MatxRows*MatxCols) -1; BtnID++ ) { // Orignally colors are coming from a Qvector<QRgb> of the same size (8x8) // p.setBrush(QBrush(QColor(CurrentGUIMatrix->GetButtonColor(BtnID)))); **p.setBrush(QBrush(QColor(Qt::yellow)));** // force the color for test Current_Row= BtnID/8 ; Current_Col = BtnID -(8*Current_Row); MatxRects.append( QRect(Current_Col*step, Current_Row*step, step-1,step-1)) ; / } uint BtnID = 18 ; **p.setBrush(QBrush(QColor(Qt::red)));** Current_Row= BtnID/8 ; Current_Col = BtnID -(8*Current_Row); MatxRects.append( QRect(Current_Col*step, Current_Row*step, step-1,step-1)) ; p.drawRects(MatxRects) ; p.end();
-
Hi,
You may have misunderstood how drawRects work. You pass to that function a list of rectangles and only rectangles. Changing the brush while you create your rectangle list doesn't have any influence on the outcome. They are not linked at all.
If you want your grid of changing colour rectangle then call drawRect in your loop.
-
Docs say:
void QPainter::drawRects(const QRectF * rectangles, int rectCount)
Draws the first rectCount of the given rectangles using the current pen and brush.void QPainter::setBrush(const QBrush & brush)
Sets the painter's brush to the given brush.Maybe the latter would be clearer if it said "Set's the painter's current brush to the given brush."
Combine those two, and your code behavior seems well explained to me.
-
@Asperamanca said in different behaviours using a Qpainter with Drawrects:
Docs say:
void QPainter::drawRects(const QRectF * rectangles, int rectCount)
Draws the first rectCount of the given rectangles using the current pen and brush.void QPainter::setBrush(const QBrush & brush)
Sets the painter's brush to the given brush.Maybe the latter would be clearer if it said "Set's the painter's current brush to the given brush."
Combine those two, and your code behavior seems well explained to me.
You have understood my question, as indeed you need to have spent some time experimenting the differences to get this point.
Thanks to you too. Your answer is very important to me as it help to understand the problem rather than get help and repeat it another day.
Give a man a fish, and you feed him for a day; show him how to catch fish, and you feed him for a lifetime.