改进性能自定义进度条动画
Improve performance custom progress bar animation
我想要一个自定义进度条,它的进度通过自定义动画进行更改。我会有很多这个小部件的实例,所有这些都应该平稳快速地运行。
我的第一次尝试是使用常规的QProgressBar
,通过使用样式表对其进行自定义,然后使用QPropertyAnimation
来动画化状态的变化。
这很好,但速度非常慢。比方说,我以0%的值开始动画,然后达到50%,并希望在500毫秒的持续时间内完成。它一点也不顺利,但有三个明显可区分的步骤。如果我放弃样式表,它将足够顺利地工作。
好吧,似乎可以很好地使用QProgressBar的派生类,它比使用样式表快得多,尽管我必须自定义调整宽度和高度:
void ColorBar::paintEvent(QPaintEvent *pe)
{
QRect region = pe->rect();
QPainter painter(this);
QColor borderColor;
borderColor.setNamedColor("#a0a0a0");
QColor lightColor = QColor(255, 255, 255);
QColor darkColor = QColor(225, 225, 225);
int barHeight = static_cast<int>(height() * 1. / 4. + 0.5);
QRect drawRect(0, static_cast<int>(height() / 2. - barHeight / 2. + 0.5), width() * .9 * value() / maximum(), barHeight);
QLinearGradient g(drawRect.topLeft(), drawRect.bottomLeft());
g.setColorAt(0., lightColor);
g.setColorAt(1., darkColor);
painter.setPen(QPen(borderColor));
painter.setBrush(QBrush(g));
painter.drawRect(drawRect);
}
制作这个栏的动画是简单而快速的:
QPropertyAnimation* x = new QPropertyAnimation(percentageBar, "value");
x->setStartValue(percentageBar->value());
x->setEndValue(newValue);
x->setDuration(500);
x->start();
仍然可以获得反馈或更好的解决方案!
相关文章:
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- C++自定义比较函数
- 如何比较自定义类的std::变体
- std::设置自定义比较器
- 如何正确实现和访问运算符的各种自定义枚举器
- flutter:即使shouldRepaint()返回true,自定义画家也不会重新绘制
- 自定义先决条件对移动分配运算符有效吗
- 使用VS Code和CMake Tools运行自定义命令
- 如何创建从Maya(或类似程序)到虚幻引擎的自定义数据导出插件
- std::ranges::elements_view,用于自定义类似元组的数据
- 跟随整数索引列表的自定义类迭代器
- 参数化自定义CMake工具链
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 如何在自定义类中启用'auto loops'?
- 在鼠标光标平面上绘制或创建自定义动画系统光标
- 改进性能自定义进度条动画
- 使用 Illustrator 图像进行自定义动画,使用 C++
- . net / winforms自定义窗体边界与最小化动画
- 使用Qt动画框架自定义委托在QListView