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. [Solved]QStyleOptionProgressBar and style sheet
Forum Updated to NodeBB v4.3 + New Features

[Solved]QStyleOptionProgressBar and style sheet

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 4.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.
  • R Offline
    R Offline
    redstoneleo
    wrote on 8 Jun 2013, 09:20 last edited by
    #1

    I'm painting a QProgressBar (using QStyleOptionProgressBar) into a QabstractTableModel ,the code just like this
    http://qt-project.org/doc/qt-4.8/qabstractitemdelegate.html

    now I want to set its style sheet similar to this

    ('QProgressBar::chunk {background-color: rgb(102, 204, 255);} QProgressBar {border: 2px solid rgb(0, 128, 255); border-radius: 5px; background-color: rgb(200, 200, 200);}')

    so what syntax should I use ?

    1 Reply Last reply
    0
    • F Offline
      F Offline
      francescmm
      wrote on 9 Jun 2013, 18:43 last edited by
      #2

      What I can remember when using a delegate class I couldn't use the stylesheet. To style as my own way I avoid the use of QStyleOptionProgressBar.

      I subclass QStyledItemDelegate and rewrite the paint() method as follows:

      ProgressBarDelegate.h
      @#ifndef PROGRESSBARDELEGATE_H
      #define PROGRESSBARDELEGATE_H

      #include <QStyledItemDelegate>

      class ProgressBarDelegate : public QStyledItemDelegate
      {
      Q_OBJECT

      public:
          explicit ProgressBarDelegate(QObject *parent = 0) : QStyledItemDelegate(parent)
          {}
          void paint(QPainter *painter, const QStyleOptionViewItem &option,
                     const QModelIndex &index) const;
      

      };

      #endif // PROGRESSBARDELEGATE_H
      @

      ProgressBarDelegate.cpp
      @#include <QApplication>
      #include <QPainter>
      #include <QProgressBar>
      #include "ProgressBarDelegate.h"
      #include <QFile>

      void ProgressBarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
      const QModelIndex &index) const
      {
      index.data(Qt::FontRole);
      int progress = index.data().toInt();

      if (progress >= 0)
      {
          QRect rect(option.rect.x()+5, option.rect.y()+5, option.rect.width(),
                     option.rect.height()-10);
      
          painter->save();
          painter->setRenderHint(QPainter::Antialiasing);
          painter->setBrush(QColor(75,75,75));
          painter->setPen(QColor("transparent"));
          QRect rect2(option.rect.x()+3, option.rect.y()+3, option.rect.width()-5,
                      option.rect.height()-7);
          painter->drawRoundedRect(rect2,5,5);
          painter->restore();
      
          /*
              I calculate the area to paint the simulated ProgressBar.
          */
          int progBarWidth = float((option.rect.width() * progress) / 100);
          if (progBarWidth > 5)
          {
              //Painting the ProgressBar
              painter->save();
              painter->setRenderHint(QPainter::Antialiasing);
              painter->setPen(QColor("transparent"));
              painter->setBrush(QColor(65,105,255));
              QRect rect5(option.rect.x()+3, option.rect.y()+3, progBarWidth-3,
                          option.rect.height()-7);
              painter->drawRoundedRect(rect5,5,5);
              painter->restore();
          }
          else
              progBarWidth = 5;
      
          painter->save();
          painter->setRenderHint(QPainter::Antialiasing);
          painter->setPen(QColor("transparent"));
          painter->setBrush(QColor(65,105,255));
          QRect rect3(option.rect.x()+5, option.rect.y()+3, progBarWidth-5,
                      option.rect.height()-7);
          painter->drawRect(rect3);
          painter->restore();
      
          painter->save();
          painter->setRenderHint(QPainter::Antialiasing);
          painter->setPen(QColor("white"));
          painter->setBrush(QColor("transparent"));
          QRect rect4(option.rect.x()+1, option.rect.y()+2, option.rect.width()-3,
                      option.rect.height()-5);
          painter->drawRoundedRect(rect4,5,5);
          painter->restore();
          }
      

      }
      @

      The main reason to use the [+num] operations is to set the progressbar in a specific position. I hope it would be useful for you!

      D 1 Reply Last reply 13 Apr 2019, 15:01
      0
      • R Offline
        R Offline
        redstoneleo
        wrote on 27 Oct 2013, 09:26 last edited by
        #3

        thanks very much !
        [quote author="francescmm" date="1370803385"]What I can remember when using a delegate class I couldn't use the stylesheet. To style as my own way I avoid the use of QStyleOptionProgressBar.

        I subclass QStyledItemDelegate and rewrite the paint() method as follows:

        ProgressBarDelegate.h
        @#ifndef PROGRESSBARDELEGATE_H
        #define PROGRESSBARDELEGATE_H

        #include <QStyledItemDelegate>

        class ProgressBarDelegate : public QStyledItemDelegate
        {
        Q_OBJECT

        public:
            explicit ProgressBarDelegate(QObject *parent = 0) : QStyledItemDelegate(parent)
            {}
            void paint(QPainter *painter, const QStyleOptionViewItem &option,
                       const QModelIndex &index) const;
        

        };

        #endif // PROGRESSBARDELEGATE_H
        @

        ProgressBarDelegate.cpp
        @#include <QApplication>
        #include <QPainter>
        #include <QProgressBar>
        #include "ProgressBarDelegate.h"
        #include <QFile>

        void ProgressBarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
        const QModelIndex &index) const
        {
        index.data(Qt::FontRole);
        int progress = index.data().toInt();

        if (progress >= 0)
        {
            QRect rect(option.rect.x()+5, option.rect.y()+5, option.rect.width(),
                       option.rect.height()-10);
        
            painter->save();
            painter->setRenderHint(QPainter::Antialiasing);
            painter->setBrush(QColor(75,75,75));
            painter->setPen(QColor("transparent"));
            QRect rect2(option.rect.x()+3, option.rect.y()+3, option.rect.width()-5,
                        option.rect.height()-7);
            painter->drawRoundedRect(rect2,5,5);
            painter->restore();
        
            /*
                I calculate the area to paint the simulated ProgressBar.
            */
            int progBarWidth = float((option.rect.width() * progress) / 100);
            if (progBarWidth > 5)
            {
                //Painting the ProgressBar
                painter->save();
                painter->setRenderHint(QPainter::Antialiasing);
                painter->setPen(QColor("transparent"));
                painter->setBrush(QColor(65,105,255));
                QRect rect5(option.rect.x()+3, option.rect.y()+3, progBarWidth-3,
                            option.rect.height()-7);
                painter->drawRoundedRect(rect5,5,5);
                painter->restore();
            }
            else
                progBarWidth = 5;
        
            painter->save();
            painter->setRenderHint(QPainter::Antialiasing);
            painter->setPen(QColor("transparent"));
            painter->setBrush(QColor(65,105,255));
            QRect rect3(option.rect.x()+5, option.rect.y()+3, progBarWidth-5,
                        option.rect.height()-7);
            painter->drawRect(rect3);
            painter->restore();
        
            painter->save();
            painter->setRenderHint(QPainter::Antialiasing);
            painter->setPen(QColor("white"));
            painter->setBrush(QColor("transparent"));
            QRect rect4(option.rect.x()+1, option.rect.y()+2, option.rect.width()-3,
                        option.rect.height()-5);
            painter->drawRoundedRect(rect4,5,5);
            painter->restore();
            }
        

        }
        @

        The main reason to use the [+num] operations is to set the progressbar in a specific position. I hope it would be useful for you![/quote]

        1 Reply Last reply
        0
        • F francescmm
          9 Jun 2013, 18:43

          What I can remember when using a delegate class I couldn't use the stylesheet. To style as my own way I avoid the use of QStyleOptionProgressBar.

          I subclass QStyledItemDelegate and rewrite the paint() method as follows:

          ProgressBarDelegate.h
          @#ifndef PROGRESSBARDELEGATE_H
          #define PROGRESSBARDELEGATE_H

          #include <QStyledItemDelegate>

          class ProgressBarDelegate : public QStyledItemDelegate
          {
          Q_OBJECT

          public:
              explicit ProgressBarDelegate(QObject *parent = 0) : QStyledItemDelegate(parent)
              {}
              void paint(QPainter *painter, const QStyleOptionViewItem &option,
                         const QModelIndex &index) const;
          

          };

          #endif // PROGRESSBARDELEGATE_H
          @

          ProgressBarDelegate.cpp
          @#include <QApplication>
          #include <QPainter>
          #include <QProgressBar>
          #include "ProgressBarDelegate.h"
          #include <QFile>

          void ProgressBarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
          const QModelIndex &index) const
          {
          index.data(Qt::FontRole);
          int progress = index.data().toInt();

          if (progress >= 0)
          {
              QRect rect(option.rect.x()+5, option.rect.y()+5, option.rect.width(),
                         option.rect.height()-10);
          
              painter->save();
              painter->setRenderHint(QPainter::Antialiasing);
              painter->setBrush(QColor(75,75,75));
              painter->setPen(QColor("transparent"));
              QRect rect2(option.rect.x()+3, option.rect.y()+3, option.rect.width()-5,
                          option.rect.height()-7);
              painter->drawRoundedRect(rect2,5,5);
              painter->restore();
          
              /*
                  I calculate the area to paint the simulated ProgressBar.
              */
              int progBarWidth = float((option.rect.width() * progress) / 100);
              if (progBarWidth > 5)
              {
                  //Painting the ProgressBar
                  painter->save();
                  painter->setRenderHint(QPainter::Antialiasing);
                  painter->setPen(QColor("transparent"));
                  painter->setBrush(QColor(65,105,255));
                  QRect rect5(option.rect.x()+3, option.rect.y()+3, progBarWidth-3,
                              option.rect.height()-7);
                  painter->drawRoundedRect(rect5,5,5);
                  painter->restore();
              }
              else
                  progBarWidth = 5;
          
              painter->save();
              painter->setRenderHint(QPainter::Antialiasing);
              painter->setPen(QColor("transparent"));
              painter->setBrush(QColor(65,105,255));
              QRect rect3(option.rect.x()+5, option.rect.y()+3, progBarWidth-5,
                          option.rect.height()-7);
              painter->drawRect(rect3);
              painter->restore();
          
              painter->save();
              painter->setRenderHint(QPainter::Antialiasing);
              painter->setPen(QColor("white"));
              painter->setBrush(QColor("transparent"));
              QRect rect4(option.rect.x()+1, option.rect.y()+2, option.rect.width()-3,
                          option.rect.height()-5);
              painter->drawRoundedRect(rect4,5,5);
              painter->restore();
              }
          

          }
          @

          The main reason to use the [+num] operations is to set the progressbar in a specific position. I hope it would be useful for you!

          D Offline
          D Offline
          Debjit
          wrote on 13 Apr 2019, 15:01 last edited by
          #4

          @francescmm Thanks man a lot :)

          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