Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. different behaviours using a Qpainter with Drawrects

different behaviours using a Qpainter with Drawrects

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.9k Views 3 Watching
  • 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 Offline
    D Offline
    doubitchou
    wrote on last edited by doubitchou
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      2
      • SGaistS SGaist

        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.

        D Offline
        D Offline
        doubitchou
        wrote on last edited by
        #3

        @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
        0
        • A Offline
          A Offline
          Asperamanca
          wrote on last edited by
          #4

          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
          2
          • A Asperamanca

            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 Offline
            D Offline
            doubitchou
            wrote on last edited by
            #5

            @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
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved