#include <QApplication>
#include <QStyledItemDelegate>
#include <QPainter>
#include <QTableView>
#include <QStandardItemModel>
#include <QHeaderView>
#include <QPushButton>
#include <QVBoxLayout>
#include <QProxyStyle>
class ProgressBarStyle : public QProxyStyle
{
public:
ProgressBarStyle
(QStyle *style
= nullptr
) : QProxyStyle
(style
) { qDebug() << "ProgressBarStyle constructor called";
}
{
if (element == CE_ProgressBar)
{
if (progressBarOption && progressBarOption->progress > 50)
{
progressBarOptionCopy.
palette.
setColor(QPalette::Highlight, Qt
::red);
QProxyStyle::drawControl(element, &progressBarOptionCopy, painter, widget);
return;
}
}
QProxyStyle::drawControl(element, option, painter, widget);
}
};
class ProgressBarDelegate : public QStyledItemDelegate
{
public:
ProgressBarDelegate
(QObject *parent
= nullptr
) : QStyledItemDelegate
(parent
) {}
{
int progress = index.data().toInt();
progressBarOption.rect = option.rect;
progressBarOption.minimum = 0;
progressBarOption.maximum = 100;
progressBarOption.progress = progress;
progressBarOption.
text = QString::number(progress
) + "%";
progressBarOption.textVisible = true;
}
};
int main(int argc, char *argv[])
{
layout.addWidget(&tableView);
layout.addWidget(&button);
tableView.setModel(&model);
// Set up data
for (int row = 0; row < model.rowCount(); ++row) {
for (int col = 0; col < model.columnCount(); ++col) {
model.setData(model.index(row, col), (row + col) * 2);
}
}
// Set up delegate
ProgressBarDelegate delegate;
tableView.setItemDelegate(&delegate);
// Set up style
ProgressBarStyle *style = new ProgressBarStyle;
tableView.setStyle(style);
app.setStyle(style);
// Connect button to update progress
QObject::connect(&button,
&QPushButton
::clicked,
[&model,
&tableView
]() { for (int row = 0; row < model.rowCount(); ++row) {
for (int col = 0; col < model.columnCount(); ++col) {
int progress = model.data(model.index(row, col)).toInt();
progress = (progress + 10) % 110;
model.setData(model.index(row, col), progress);
}
}
tableView.viewport()->update();
});
tableView.
horizontalHeader()->setSectionResizeMode
(QHeaderView::Stretch);
window.show();
return app.exec();
}
#include <QApplication>
#include <QStyledItemDelegate>
#include <QPainter>
#include <QTableView>
#include <QStandardItemModel>
#include <QHeaderView>
#include <QPushButton>
#include <QVBoxLayout>
#include <QProxyStyle>
class ProgressBarStyle : public QProxyStyle
{
public:
ProgressBarStyle(QStyle *style = nullptr) : QProxyStyle(style) {
qDebug() << "ProgressBarStyle constructor called";
}
void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const override
{
if (element == CE_ProgressBar)
{
const QStyleOptionProgressBar *progressBarOption = qstyleoption_cast<const QStyleOptionProgressBar *>(option);
if (progressBarOption && progressBarOption->progress > 50)
{
QStyleOptionProgressBar progressBarOptionCopy(*progressBarOption);
progressBarOptionCopy.palette.setColor(QPalette::Highlight, Qt::red);
QProxyStyle::drawControl(element, &progressBarOptionCopy, painter, widget);
return;
}
}
QProxyStyle::drawControl(element, option, painter, widget);
}
};
class ProgressBarDelegate : public QStyledItemDelegate
{
public:
ProgressBarDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
int progress = index.data().toInt();
QStyleOptionProgressBar progressBarOption;
progressBarOption.rect = option.rect;
progressBarOption.minimum = 0;
progressBarOption.maximum = 100;
progressBarOption.progress = progress;
progressBarOption.text = QString::number(progress) + "%";
progressBarOption.textVisible = true;
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
QVBoxLayout layout(&window);
QTableView tableView;
layout.addWidget(&tableView);
QPushButton button("Update Progress");
layout.addWidget(&button);
QStandardItemModel model(5, 3); // 5 rows and 3 columns
tableView.setModel(&model);
// Set up data
for (int row = 0; row < model.rowCount(); ++row) {
for (int col = 0; col < model.columnCount(); ++col) {
model.setData(model.index(row, col), (row + col) * 2);
}
}
// Set up delegate
ProgressBarDelegate delegate;
tableView.setItemDelegate(&delegate);
// Set up style
ProgressBarStyle *style = new ProgressBarStyle;
tableView.setStyle(style);
app.setStyle(style);
// Connect button to update progress
QObject::connect(&button, &QPushButton::clicked, [&model, &tableView]() {
for (int row = 0; row < model.rowCount(); ++row) {
for (int col = 0; col < model.columnCount(); ++col) {
int progress = model.data(model.index(row, col)).toInt();
progress = (progress + 10) % 110;
model.setData(model.index(row, col), progress);
}
}
tableView.viewport()->update();
});
tableView.horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
window.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks