[Solved]QStyleOptionProgressBar and style sheet
-
I'm painting a QProgressBar (using QStyleOptionProgressBar) into a QabstractTableModel ,the code just like this
http://qt-project.org/doc/qt-4.8/qabstractitemdelegate.htmlnow 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 ?
-
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_OBJECTpublic: 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!
-
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_OBJECTpublic: 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]
-
@francescmm Thanks man a lot :)