Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Solved different behaviours using a Qpainter with Drawrects

    General and Desktop
    3
    5
    1342
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      doubitchou last edited by doubitchou

      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 :

      that screenshot

      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();
      
      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        D 1 Reply Last reply Reply Quote 2
        • D
          doubitchou @SGaist last edited by

          @SGaist

          Indeed,

          this is better

          Thanks.

          PS: I didn't read that in the Qt doc, how did you found that ?

          1 Reply Last reply Reply Quote 0
          • A
            Asperamanca last edited by

            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.

            D 1 Reply Last reply Reply Quote 2
            • D
              doubitchou @Asperamanca last edited by

              @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.
              
              1 Reply Last reply Reply Quote 2
              • First post
                Last post