首页 > 解决方案 > 如何在 QStyledItemDelegate 中设置 QStyleOptionProgressBar 的方向

问题描述

我正在使用 Qt 5.15.2。我有一个QTableView从派生的类加载数据QAbstractTableModel。这很好用。现在我想在一列的每一行中添加一个进度条。为此,我从以下位置派生了一个类QStyledItemDelegate

class ProgressBarDelegate : public QStyledItemDelegate {
    Q_OBJECT

public:
    ProgressBarDelegate(QObject *parent = 0) : QStyledItemDelegate(parent) {}

    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
        if (index.data().canConvert<int>()) {
            int progress = qvariant_cast<int>(index.data());

            QStyleOptionProgressBar progressBarOption;
            progressBarOption.rect = option.rect;
            progressBarOption.minimum = 0;
            progressBarOption.maximum = 100;
            progressBarOption.progress = progress;

            QStyle *style = QApplication::style();

            style->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);

        } else {
            QStyledItemDelegate::paint(painter, option, index);
        }
    }
};

它几乎可以工作,问题是进度条从下到上,如下所示:

ProgressBarDelegate 垂直方向

缩放 ProgressBarDelegate 垂直方向

如何从左到右更改方向?

顺便说一句,这是将进度条填充到 中的最佳方法QTableView吗?

标签: c++qt

解决方案


缺少的行是:

progressBarOption.state = QStyle::StateFlag::State_Horizontal;

推荐阅读