- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想为 QBrush
的颜色设置动画。有关更多详细信息,请参见下面的代码
那是我的 .h 文件
class Cell : public QObject, public QGraphicsRectItem
{
Q_OBJECT
Q_PROPERTY(QBrush brush READ brush WRITE set_Brush)
public:
QBrush _brush() const;
void set_Brush(const QBrush&);
Cell(QGraphicsItem *parent = 0); //конструктор
}
那是我的 .cpp 文件
Cell::Cell(QGraphicsItem *parent)
: QObject(), QGraphicsRectItem(parent)
{
this->setRect(0, 0, Scale, Scale);
}
QBrush Cell::_brush() const
{
return this->brush();
}
void Cell::set_Brush(const QBrush & brush)
{
this->setBrush(brush);
}
这就是动画:
QPropertyAnimation* animation = new QPropertyAnimation(selectedCell, "brush");
animation->setDuration(10000);
animation->setStartValue(QBrush(QColor(255,0,0)));
animation->setEndValue(QBrush(QColor(0,255,0)));
animation->start();
但是它不起作用,没有任何反应,画笔的颜色和以前一样。我应该怎么做才能修复它?
最佳答案
QT 不知道如何在开始 QBrush
和结束 QBrush
之间执行转换。 QBrush
具有比颜色更多的属性,您可以设想一个颜色保持不变而只有图案发生变化的动画。因此,没有默认支持这种转换。正如@fscibe 所暗示的,您可以编写自己的方法来执行转换,在该转换中您指定要如何从一个 QBrush
转换到另一个。
粗略的例子:
QVariant myColorInterpolator(const QBrush &start, const QBrush &end, qreal progress)
{
QColor cstart = start.color();
QColor cend = end.color();
int sh = cstart.hsvHue();
int eh = cend.hsvHue();
int ss = cstart.hsvSaturation();
int es = cend.hsvSaturation();
int sv = cstart.value();
int ev = cend.value();
int hr = qAbs( sh - eh );
int sr = qAbs( ss - es );
int vr = qAbs( sv - ev );
int dirh = sh > eh ? -1 : 1;
int dirs = ss > es ? -1 : 1;
int dirv = sv > ev ? -1 : 1;
return QBrush(QColor::fromHsv( sh + dirh * progress * hr,
ss + dirs * progress * sr,
sv + dirv * progress * vr), progress > 0.5 ? Qt::SolidPattern : Qt::Dense6Pattern );
}
这会执行颜色过渡,但也会在过渡过程中更改图案。
那么这里将是您代码中此转换的虚拟应用程序:
int main(int argc, char** argv)
{
QApplication app(argc,argv);
QGraphicsView *view = new QGraphicsView;
QGraphicsScene *scene = new QGraphicsScene;
QDialog *dialog = new QDialog;
QGridLayout *layout = new QGridLayout;
layout->addWidget(view);
view->setScene(scene);
scene->setSceneRect(-500,-500,1000,1000);
dialog->setLayout(layout);
dialog->show();
Cell *selectedCell = new Cell;
scene->addItem(selectedCell);
qRegisterAnimationInterpolator<QBrush>(myColorInterpolator);
QPropertyAnimation* animation = new QPropertyAnimation(selectedCell, "brush");
animation->setDuration(10000);
animation->setStartValue(QBrush(Qt::blue));
animation->setEndValue(QBrush(Qt::red));
animation->start();
return app.exec();
}
显然这是一个虚拟示例,对于颜色更改,它正在重新发明轮子,正如您从 fscibe 答案中看到的那样,但这只是为了向您展示定义您自己的过渡方法,例如QBrush
您可以做的不仅仅是改变颜色。
关于c++ - 如何动画QBrush的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25514812/
下面的代码有什么区别, QGraphicsScene * scence = new QGraphicsScene(); QBrush *brush = new QBrush((QColor(
我想通过图形框架类在屏幕上绘制 1 个数字。我希望 '1' 的填充方法类似于 (来源:qt-project.org) 但是我绘制的“1”的画笔就像下面代码中的黄色 SolidBrush(丑陋的粗体黄色
void Widget::paintEvent(QPaintEvent *) { QPainter painter(this); QBrush br; br.setColor(
我正在尝试将 Qt4 中 QMdiArea 小部件的背景 QBrush 设置为系统颜色的渐变。 这是我现在的一些代码: QPrios::QPrios(int &argc, char **argv):
如何使用 RBG 值为 QBrush 设置自定义颜色?然后在 GraphicsView 上用画笔画一个矩形。 到目前为止我已经尝试过: QBrush *goldBrush = new QBrush(Q
我正在尝试使用 QImage 为 QT 渲染自定义图像,但到目前为止我的代码没有任何进展。我不想从文件加载图像,我想使用 QImage 类和 QBrush 类以及这需要的其他内容。我不太擅长在 API
目前我用它来创建一个 QBrush: QBrush *goldBrush = new QBrush(QColor(212,175,55)); scene->addRect(0,415,20,50,no
我是一名优秀的程序员,十分优秀!