【问题标题】:Customized color on progressbar delegate进度条委托上的自定义颜色
【发布时间】:2012-05-17 05:36:00
【问题描述】:

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

在我的工作区对象中,它填充了 MainWindow 上的一个子视图。

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; }");

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

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

【问题讨论】:

    标签: c++ qt


    【解决方案1】:

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

    创建一个新的简单 Qt Gui 项目,打开 UI 表单编辑器并从工具窗口的“显示小部件”下添加一个进度条控件。现在在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 style).

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

    编辑:

    我同意上述方法仅适用于控件而不适用于委托。我也为代表们找到了一些东西。尝试关注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。希望这会有所帮助..

    【讨论】:

    • 我将如何与代表一起执行此操作?我使用的唯一进度条代码位于 tablevview 的一个委托中
    • 我真的没有 qprogressbar 项目或任何我可以设置样式表的东西。这让我发疯。为什么仅以编程方式更改某物的颜色如此困难?
    • @chikuba:对不起,我对代理了解不多,但是如果您想自定义 UI 中的所有进度条,那么您可以使用 QApplication::setStyleSheet(style) 而不是 ui->progressBar->setStyleSheet(style),就像我一样在我的回答中提到。样式表方法在我看来使用起来要简单得多。
    • 是的,请阅读我对帖子的编辑。我的代表不充当 progessbar,因此样式表由于某种原因不适用于它
    • 我不太清楚为什么这个答案会得到很多赞成,因为样式表在 styleItems 上不起作用。
    【解决方案2】:

    而不是pal.setColor(QPalette::Window, col);

    使用pal.setColor(QPalette::Highlight, col);

    这应该是work

    我在 Paintevent 中使用了代码

       void Dialog::paintEvent(QPaintEvent *e)
       {
        QPainter painter(this);
        connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(ShowPrintDialog()));
        QStyleOptionProgressBarV2 progressBarOption;
        progressBarOption.rect = QRect(20,20,30,30);
        progressBarOption.minimum = 0;
        progressBarOption.maximum = 100;
        progressBarOption.progress = 75;
        QPalette pal = progressBarOption.palette;
        QColor col = QColor(0,255,0);
        pal.setColor(QPalette::Highlight, col);
        progressBarOption.palette = pal;
    
    
        QApplication::style()->drawControl(QStyle::CE_ProgressBar,&progressBarOption, &painter);
        }
    

    【讨论】:

    • 是的,当其他东西工作时感觉有点奇怪,因为它们不是“设置”方法
    • 是的,唯一的区别是我将此代码放在对话框的构造函数中。
    • 我刚刚在一个对话框基础应用程序中检查了它。
    • 但我的代码被用作 tableview 的委托,我从不创建进度条对象
    • 我用 QStyleOptionProgressBarV2 做到了
    猜你喜欢
    • 2018-07-29
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 2017-11-23
    • 1970-01-01
    相关资源
    最近更新 更多