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. Custom progress bar
Forum Updated to NodeBB v4.3 + New Features

Custom progress bar

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.3k Views 1 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.
  • erwelE Offline
    erwelE Offline
    erwel
    wrote on last edited by
    #1

    Hello,

    My experience in Qt is at this point very limited and I'm taking a dive in the deep by trying to create a custom progress bar for my application. The progress bar I'm trying to create needs to looks something like this: https://snag.gy/JYCHru.jpg

    Any hints on how to create such as custom progress bar are very appreciated.

    Thanks in advance!

    raven-worxR 1 Reply Last reply
    0
    • erwelE erwel

      Hello,

      My experience in Qt is at this point very limited and I'm taking a dive in the deep by trying to create a custom progress bar for my application. The progress bar I'm trying to create needs to looks something like this: https://snag.gy/JYCHru.jpg

      Any hints on how to create such as custom progress bar are very appreciated.

      Thanks in advance!

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @erwel
      Subclass QProgessBar and reimplement the paintEvent() handler like this:
      Untested, but it should give you an idea how to achieve it. Feel free to tweak it to your needs.

      void MyProgressBar::paintEvent( QPaintEvent* )
      {
             QPainter p( this );
             p.setPen( Qt::NoPen );
      
             qreal blockWidth = rect().width() / 10;
             qreal blockSpacing = blockWidth / 10;
             blockWidth -= blockSpacing;
      
             qreal blockHeight = rect().height();
      
             qreal percentageValue = (value() - minimum()) / (maximum() - minimum());
      
             for( int i = 0; i < 10; ++i )
             {
                     Qt::Color color = percentageValue >= (i+1)*10 ? Qt::green : Qt::white;
                     p.setBrush( color );
                     int x = i * (blockWidth + blockSpacing);
                     int y = 0;
                     p.drawRectF( QRect(x,y,blockWidth,blockHeight) );
             }
      }
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      erwelE 1 Reply Last reply
      1
      • raven-worxR raven-worx

        @erwel
        Subclass QProgessBar and reimplement the paintEvent() handler like this:
        Untested, but it should give you an idea how to achieve it. Feel free to tweak it to your needs.

        void MyProgressBar::paintEvent( QPaintEvent* )
        {
               QPainter p( this );
               p.setPen( Qt::NoPen );
        
               qreal blockWidth = rect().width() / 10;
               qreal blockSpacing = blockWidth / 10;
               blockWidth -= blockSpacing;
        
               qreal blockHeight = rect().height();
        
               qreal percentageValue = (value() - minimum()) / (maximum() - minimum());
        
               for( int i = 0; i < 10; ++i )
               {
                       Qt::Color color = percentageValue >= (i+1)*10 ? Qt::green : Qt::white;
                       p.setBrush( color );
                       int x = i * (blockWidth + blockSpacing);
                       int y = 0;
                       p.drawRectF( QRect(x,y,blockWidth,blockHeight) );
               }
        }
        
        erwelE Offline
        erwelE Offline
        erwel
        wrote on last edited by
        #3

        @raven-worx Thank you, I learned about subclassing and it is pretty easy to modify the widgets to fit my needs. Now just finding out more about painting objects and I'm good to go.

        1 Reply Last reply
        0

        • Login

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