c++ - 进度条委托(delegate)上的自定义颜色

标签 c++ qt

我已经尝试这样做了很长一段时间,并从我能找到的每篇论坛帖子中获取建议,但我仍然无法解决它。这是我当前的代码,我真的很想更改进度条上 block 的颜色。除颜色外,其他所有设置均有效。

在我的工作区对象中,它填满了 MainWindow 上的一个 subview 。

Workspace::Workspace( QWidget* parent) : QWidget( parent )
{
    QTableView* tableView = new QTableView();
    // ...
    tableView->setItemDelegate(new ProgressBarDelegate);
}

delegate.cpp 看起来像这样:

ProgressBarDelegate::ProgressBarDelegate( QObject* parent )
: QStyledItemDelegate(parent)
{
}

void ProgressBarDelegate::paint( QPainter *painter,
                                 const QStyleOptionViewItem &option,
                                 const QModelIndex &index) const
{
    if (index.column() == 2)
    {
        int progressPercentage = index.model()->data(index, Qt::DisplayRole).toInt();

        QStyleOptionProgressBarV2 progressBarOption;
        progressBarOption.rect = QRect(option.rect.x(), option.rect.y() + 5 , option.rect.width(), option.rect.height() / 1.5);
        progressBarOption.minimum = 0;
        progressBarOption.maximum = 100;
        progressBarOption.progress = progressPercentage;
        QPalette pal = progressBarOption.palette;
        QColor col = QColor(35, 35,25);
        pal.setColor(QPalette::Highlight, col); // or QPalette::Window doesnt matter
        progressBarOption.palette = pal;

        if(option.state & QStyle::State_Selected)
        {
        }

        QApplication::style()->drawControl( QStyle::CE_ProgressBar,
                                            &progressBarOption,
                                            painter);
    }
    else
    {
        QStyledItemDelegate::paint(painter, option, index);
    }
}

目前,无论我做什么,颜色都不会从 OSX 标准浅灰色改变。

如果需要,运行 OSX 10.6.7 和 Qt 4.8.1。谢谢!

编辑:

我能够做到以下几点:

app.setStyleSheet("QScrollBar:horizontal { border: 2px solid green;background: cyan;height: 15px;margin: 0px 20px 0 20px;}");

但是当我这样做时:

app.setStyleSheet("QProgressBar:horizontal { border: 1px solid gray; border-radius: 3px; background: white; padding: 1px; }");

进度条上没有任何变化。理论上我没有创建任何进度条对象,我只是设置了一种样式,我如何在我的委托(delegate)中查看我的数据。但是可以肯定的是,我不可能是第一个想要这样做的人吧?

此外,如果这不起作用,我该如何在表格 View 中执行此操作(具有样式化的进度条)?

最佳答案

你应该使用 Qt Style Sheet ,它允许我们自定义许多控件的 UI,以提供跨平台的独特外观和感觉。 Check this .

创建一个新的简单 Qt Gui 项目,打开 UI Form 编辑器并从工具窗口的“Display Widgets”下添加一个 Progress Bar 控件。现在在 MainWindow..

的构造函数中编写以下代码
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // Customize progress-bar's style..

    QString style = "QProgressBar {border: 2px solid grey; border-radius: 5px; text-align: center;}";
    style += "QProgressBar::chunk {background-color: #CD96CD; width: 10px; margin: 0.5px;}";

    // Assuming objectName is 'progressBar'..
    ui->progressBar->setStyleSheet(style);
}

编译运行

如果您只想更改单个 QProgressBar 控件,那么上述方法就足够了,但是如果您想在应用程序级别应用样式(比如所有 QProgressBar 控件和一些其他控件),那么正确的方法是创建一个 *.css 文件,使用 Qt Style Sheet Reference 编写样式然后在 Qt 中读取该文件并调用

QApplication::setStyleSheet(QString 样式)

此外,样式表使用与CSS相同的语法,还支持各种选择器。

编辑:

我同意上述方法仅适用于控件而不适用于委托(delegate)。我也为代表们找到了一些东西。尝试以下 paint 函数。

void ProgressBarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if (index.column() == 2)
    {
        QProgressBar renderer;
        int progressPercentage = index.model()->data(index, Qt::DisplayRole).toInt();

        // Customize style using style-sheet..

        QString style = "QProgressBar { border: 2px solid grey; border-radius: 5px; }";
        style += "QProgressBar::chunk { background-color: #05B8CC; width: 20px; }";

        renderer.resize(option.rect.size());
        renderer.setMinimum(0);
        renderer.setMaximum(100);
        renderer.setValue(progressPercentage);

        renderer.setStyleSheet(style);
        painter->save();
        painter->translate(option.rect.topLeft());
        renderer.render(painter);
        painter->restore();
    }
    else
        QStyledItemDelegate::paint(painter, option, index);
}

所以这里的重点是,我们可以直接使用控件本身作为渲染器,而不是使用 QStyleOption。希望这有帮助..

关于c++ - 进度条委托(delegate)上的自定义颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10630360/

相关文章:

qt - 通过 QProcess 运行 .sh 脚本时出错

c++ - 为什么是 "already defined"?

c++ - 为什么我得到 "Error: expected primary-expression before ' <<' token."

c++ - 加载自定义 dll + 自定义应用程序失败并显示 : error while loading shared libraries

c++ - ComboBox初始化错误 : Cannot read property 'constructor' of undefined

cocoa - 在 Qt 中订阅 Apple 事件

c++ - 如何使用socket编程获取系统日期和时间

c++ - 如何使用 Eclipse 处理 Boost.Test 输出

c++ - 为什么要覆盖 operator()?

c++ - 取消引用字符串 vector 的 .end() 时的行为