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. QPainter strange behavior

QPainter strange behavior

Scheduled Pinned Locked Moved Solved General and Desktop
qpainterqdialog
8 Posts 3 Posters 3.2k Views
  • 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.
  • S Offline
    S Offline
    SysTech
    wrote on 18 Mar 2016, 21:28 last edited by
    #1

    Hi All,

    I'm getting strange behavior from a QPainter.

    Without typing a huge message here is my organization:

    I have a QDialog displayed. On that dialog I have a single vertical layout.

    I created a small widget which derives from QWidet. In the paint event of this widget I graph some data. The code in the paint event is:

      QColor drawColor(255, 0, 0);
      QBrush backColor( QColor( 0, 0, 0 ) );
    
      QPainter painter(this);
      painter.save();
      painter.setRenderHint(QPainter::Antialiasing);
    
      painter.setBackground( backColor );
      painter.setBackgroundMode(Qt::OpaqueMode);
      painter.setPen(drawColor);
      painter.setBrush( backColor );
    
      QRect wr = geometry();
      painter.drawRect( wr );
    
      painter.drawText( 10, 10, QString("PAN:%1-%2").arg(GetIdAsHexString()).arg(m_framecount++) );
    
     // draw the data with painter.drawLine calls
    
      painter.restore();
    

    In my dialog I can add multiple Widgets (for multiple graphs). The code for this is:

    obj = new GraphDisplayWidget(id);
    ui->loPanVertical->addWidget( obj );
    

    The issue is with the lines to get the rectangle and draw the rectangle in black. above. The issue is that for the first GraphDisplayWidget I add the graph is drawn in red over a black background as desired. But if I add a second widget or third, the graph is fine but the background is not set to black. See this image:

    https://dl.dropboxusercontent.com/u/7578983/GraphDisplay.png

    I'm trying to figure out why. The code is identical for all three widgets. I was thinking that perhaps the vertical layout which contains the widgets is some how only allowing the background to be set once. So I tried creating vertical layouts on the fly but get the same result.

    Any ideas?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 18 Mar 2016, 21:47 last edited by
      #2

      @SysTech said:

      Yes sounds odd, but

      I was wondering :

      QRect wr = geometry();
      painter.drawRect( wr );

      This would give the x,y of the widget relative to the parent.
      So on first box which is at 0,0, it matches up with the widget inner coordinates.
      But next one is not at x,y and then its outside of the inner coordinates and hence
      dont clear at all and u see default color.

      Try with
      QRect wr(0,0, width(), height());

      S 2 Replies Last reply 18 Mar 2016, 21:52
      0
      • M mrjj
        18 Mar 2016, 21:47

        @SysTech said:

        Yes sounds odd, but

        I was wondering :

        QRect wr = geometry();
        painter.drawRect( wr );

        This would give the x,y of the widget relative to the parent.
        So on first box which is at 0,0, it matches up with the widget inner coordinates.
        But next one is not at x,y and then its outside of the inner coordinates and hence
        dont clear at all and u see default color.

        Try with
        QRect wr(0,0, width(), height());

        S Offline
        S Offline
        SysTech
        wrote on 18 Mar 2016, 21:52 last edited by
        #3

        @mrjj

        Come to think of it you might be spot on!!! Let me try that.

        1 Reply Last reply
        0
        • M mrjj
          18 Mar 2016, 21:47

          @SysTech said:

          Yes sounds odd, but

          I was wondering :

          QRect wr = geometry();
          painter.drawRect( wr );

          This would give the x,y of the widget relative to the parent.
          So on first box which is at 0,0, it matches up with the widget inner coordinates.
          But next one is not at x,y and then its outside of the inner coordinates and hence
          dont clear at all and u see default color.

          Try with
          QRect wr(0,0, width(), height());

          S Offline
          S Offline
          SysTech
          wrote on 18 Mar 2016, 21:55 last edited by
          #4

          @mrjj

          Yep that was exactly it. Thank you again!!!

          K 1 Reply Last reply 18 Mar 2016, 21:57
          1
          • S SysTech
            18 Mar 2016, 21:55

            @mrjj

            Yep that was exactly it. Thank you again!!!

            K Away
            K Away
            kshegunov
            Moderators
            wrote on 18 Mar 2016, 21:57 last edited by
            #5

            @SysTech
            One more thing, why not set the background color directly to the widget? As it's now, the Qt runtime will erase the widget once, and then hand it to you in the paint event, where you erase it a second time.

            Read and abide by the Qt Code of Conduct

            S 1 Reply Last reply 18 Mar 2016, 23:37
            0
            • K kshegunov
              18 Mar 2016, 21:57

              @SysTech
              One more thing, why not set the background color directly to the widget? As it's now, the Qt runtime will erase the widget once, and then hand it to you in the paint event, where you erase it a second time.

              S Offline
              S Offline
              SysTech
              wrote on 18 Mar 2016, 23:37 last edited by
              #6

              @kshegunov

              Ok that is a great idea... How?

              Are you suggesting I do this when created?

              K 1 Reply Last reply 19 Mar 2016, 00:04
              0
              • S SysTech
                18 Mar 2016, 23:37

                @kshegunov

                Ok that is a great idea... How?

                Are you suggesting I do this when created?

                K Away
                K Away
                kshegunov
                Moderators
                wrote on 19 Mar 2016, 00:04 last edited by kshegunov
                #7

                @SysTech
                Well, I mistakenly thought that you can set the color (not the background role), however my sentiment was (somewhat) correct. You can disable Qt filling your background by setting the auto fill property to false. Alternatively, or even a preferred way, since you're painting the whole widget you can set the Qt::WA_OpaquePaintEvent attribute, which should provide some improvement to painting as well.

                Are you suggesting I do this when created?

                You can set either the property or the attribute in the GraphDisplayWidget constructor.

                Read and abide by the Qt Code of Conduct

                S 1 Reply Last reply 19 Mar 2016, 00:12
                0
                • K kshegunov
                  19 Mar 2016, 00:04

                  @SysTech
                  Well, I mistakenly thought that you can set the color (not the background role), however my sentiment was (somewhat) correct. You can disable Qt filling your background by setting the auto fill property to false. Alternatively, or even a preferred way, since you're painting the whole widget you can set the Qt::WA_OpaquePaintEvent attribute, which should provide some improvement to painting as well.

                  Are you suggesting I do this when created?

                  You can set either the property or the attribute in the GraphDisplayWidget constructor.

                  S Offline
                  S Offline
                  SysTech
                  wrote on 19 Mar 2016, 00:12 last edited by
                  #8

                  @kshegunov

                  Awesome! I'll give it a try!

                  1 Reply Last reply
                  0

                  6/8

                  18 Mar 2016, 23:37

                  • Login

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