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. Filling a cell background in tableview based on percentage

Filling a cell background in tableview based on percentage

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 1.3k 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.
  • A Offline
    A Offline
    Abhi_oct30
    wrote on last edited by
    #1

    Hi,
    I am a beginner to QT. My intention is to fill the BackGround of a cell in TableView partially.
    For Example : I want only 50% of the cell to be coloured or 25% .It depends on some parameters like students score in his subject.

    Thanks in advance.

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @Abhi_oct30 It you are talking about QTableView then you can use QStyledItemDelegate to do custom painting or at the best you can also use a progressbar with fixed value.
      Here's an example using QAbstractItemDelegate

      157

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Abhi_oct30
        wrote on last edited by
        #3

        hi,
        thanks....my intention is partially completed.But the problem is progress bar is and the value are displayed side by side.I want the progress bar to be as the background for the value.0_1484231294431_upload-ad9092a7-4b20-4e0e-9f5e-f371754ace91

        FlotisableF 1 Reply Last reply
        0
        • A Abhi_oct30

          hi,
          thanks....my intention is partially completed.But the problem is progress bar is and the value are displayed side by side.I want the progress bar to be as the background for the value.0_1484231294431_upload-ad9092a7-4b20-4e0e-9f5e-f371754ace91

          FlotisableF Offline
          FlotisableF Offline
          Flotisable
          wrote on last edited by
          #4

          @Abhi_oct30
          I find that set progress bar's alignment to center may be what you want

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5
            #include <QStyledItemDelegate>
            #include <QPainter>
            #include <QApplication>
            class BarDelegate : public QStyledItemDelegate
            {
                Q_OBJECT
                Q_DISABLE_COPY(BarDelegate)
            public:
                BarDelegate(QObject* parent = Q_NULLPTR)
                    :QStyledItemDelegate(parent)
                {}
            
                void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE
                {
                    Q_ASSERT(index.isValid());
            
                    QStyleOptionViewItem opt = option;
                    initStyleOption(&opt, index);
                    //////////////////////////////////////////////////////////////////////////
                    // here decide how much of the background you want filled, from 0 to 1
                    // as an example here I use whatever is in Qt::UserRole as the percentage
                    const double fillPercent = index.data(Qt::UserRole).toDouble();
                    //////////////////////////////////////////////////////////////////////////
                    painter->save();
                    painter->setBrush(opt.backgroundBrush);
                    painter->setPen(Qt::NoPen);
                    painter->drawRect(QRect(opt.rect.topLeft(), QSize(static_cast<double>(opt.rect.width())*fillPercent, opt.rect.height())));
                    painter->restore();
                    opt.backgroundBrush = QBrush();
                    const QWidget *widget = option.widget;
                    QStyle *style = widget ? widget->style() : QApplication::style();
                    style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
                    
                }
            };
            

            Now you can set the background either by stylesheet or by defining the Qt::BackgroundRole data

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            1

            • Login

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