- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想用 QStateMachine 创建一个无限循环,其中我还需要动画。
QColor leastTransparent, mostTransparent = color();
leastTransparent.setAlpha(250);
mostTransparent.setAlpha(150);
QState *s1 = new QState();
s1->assignProperty(this, "color", leastTransparent);
QState *s2 = new QState();
s2->assignProperty(this, "color", mostTransparent);
QSignalTransition *transition = s1->addTransition( this, SIGNAL(triggerSignal()),s2);
QSignalTransition *transition2 = s2->addTransition(s2, SIGNAL(entered),s1);
QPropertyAnimation* animation = new QPropertyAnimation( this, "color");
animation->setDuration( 5000 );
transition->addAnimation(animation);
QPropertyAnimation* animation2 = new QPropertyAnimation( this, "color");
animation2->setDuration(10000);
transition2->addAnimation(animation2);
m_stateMachineAnimation->addState(s1);
m_stateMachineAnimation->addState(s2);
m_stateMachineAnimation->setInitialState(s1);
m_stateMachineAnimation->setGlobalRestorePolicy(QStateMachine::RestoreProperties);
m_stateMachineAnimation->start();
我在这里期望的是在“triggerSignal”之后的前 5 秒颜色会变得更加不透明。状态将是“s2”。并且比“s2”的输入信号被触发,它会在 10 秒内变得越来越透明。
但是我让 s2 立即触发,而不是在“triggerSignal”之后立即等待 5 秒,而不是立即再次触发 s1,而无需等待 10 秒。
为什么 QStateMachine 没有考虑我的持续时间。我怎样才能用QStateMachine实现这样的动画
最佳答案
您似乎希望动画创建某种中间状态。它没有这样的事情。过渡只是触发动画。您立即从 s2
过渡到 s1
,没有时间让动画完成。相反,您需要在设置属性的最终值时显式触发后续转换。 QState::propertiesAssigned
信号对于这个目的是最有用的。或者,您可以使用动画的 finished()
信号。
在下面的示例中,在窗口内单击以启动动画循环:
// https://github.com/KubaO/stackoverflown/tree/master/questions/statemachine-animation-42682462
#include <QtWidgets>
const char kColor[] = "color";
class Widget : public QWidget {
Q_OBJECT
Q_PROPERTY(QColor color MEMBER m_color NOTIFY colorChanged)
QColor m_color{Qt::blue};
QStateMachine m_machine{this};
QState s0{&m_machine}, s1{&m_machine}, s2{&m_machine};
QEventTransition t01{this, QEvent::MouseButtonPress};
QPropertyAnimation anim_s1{this, kColor}, anim_s2{this, kColor};
void paintEvent(QPaintEvent *) override {
QPainter{this}.fillRect(rect(), m_color);
}
Q_SIGNAL void colorChanged(const QColor &);
public:
Widget() {
connect(this, &Widget::colorChanged, [this]{ update(); });
s1.assignProperty(this, kColor, QColor{Qt::red});
s2.assignProperty(this, kColor, QColor{Qt::green});
t01.setTargetState(&s1);
s0.addTransition(&t01); t01.addAnimation(&anim_s1);
s1.addTransition(&s1, &QState::propertiesAssigned, &s2)->addAnimation(&anim_s2);
s2.addTransition(&s2, &QState::propertiesAssigned, &s1)->addAnimation(&anim_s1);
anim_s1.setDuration(1000);
anim_s2.setDuration(2000);
m_machine.setInitialState(&s0);
m_machine.start();
}
};
int main(int argc, char ** argv) {
QApplication app{argc, argv};
Widget w;
w.setFixedSize(300, 200);
w.show();
return app.exec();
}
#include "main.moc"
顺便说一句,这表明动画插入了 RGB 值,导致颜色在 red
和 blue
之间变暗,因为值从 (1 ,0,0)
到 (.5,.5,0)
到 (0,1,0)
。对于人类消费,插值 HSV 更有意义,这样值(亮度)保持不变,只有色调(我们人类真正称之为“颜色”)发生变化。
关于c++ - 带动画的 QStateMachine 事件循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42682462/
我对带有 ParallelState 的 QStateMachine 很着迷。 我有一个包含第一个状态的主状态(顺序), 包含一组并行状态的第二个状态,以及另一个连续的状态。 第一个状态代表创建 N
我对如何测试 QStateMachine 有点困惑。我有一个组织良好的项目,一侧是源代码,另一侧是测试代码。 标题 class Foo { signals: void sigG
我创建了 4 个不同的 QState,我想在每个状态转换中隐藏一个对象。 这是代码: QStateMachine partita; QState *inizio_mano = new QState()
我已经创建了一个 QStateMachine,我必须获取导致状态转换的事件。难道没有任何机会进入我的插槽 EnterStateInit() 导致此调用的信号。这是我的示例代码: CreateState
在另一个问题中,您告诉我使用 QStateMachine。 我是 Qt 的新手,这是我第一次使用对象,所以我犯了很多逻辑错误,所以使用 QStateMachine 是个大问题...... 这是唯一的方
我的程序中有一个 QStateMachine 实例。我在它的构造函数中配置它的状态、转换和初始状态。当然,我在构造函数中启动它。 this->stateA = new StateA(this); th
为了根据转换设置对象属性,我需要两个可以立即进一步转换的中间状态: A initial state Ta Tb different transitions Aa Ab
我想用 QStateMachine 创建一个无限循环,其中我还需要动画。 QColor leastTransparent, mostTransparent = color(); leastTransp
我正在考虑使用 QStateMachine 在游戏的菜单屏幕之间进行转换。但是,我不确定如何在发生状态转换时启动一些代码(例如 show() a QWidget)。我可以使用普通的旧信号很容易地做到这
以下代码由于内存损坏而导致崩溃。我假设这是因为 delete pTestStateMachine 试图删除未在堆中分配的内存。对吗? 如果是这样,是否意味着 QStateMachine::addSta
我正在尝试弄清楚如何将我自己的自定义信号与 QStateMachine 结合使用。我从 here 中的一个简单示例开始。现在我正在尝试创建一个新信号 mysignal 并触发它的转换。但我不知道如何构
我有这个 Projekt,它使用 QStatemachine 来管理 UI,我想在其中添加自定义列表。 UI 应该只由关键事件操作。据我了解,我需要在 qml 端有一个 ListView。 ListV
我的问题如下:我需要创建包含 QStateMachine 实例的类。此类应该有插槽,您可以通过这些插槽“请求”状态机转换到另一个状态。如果转换成功,我的类(class)应该发出有关它的信号。我将如何实
我目前正在移植 small application从 PyGTK 到位于系统托盘中的 PySide,它会定期检查服务器的更新。 当我最初写这篇文章时,我使用了一个自定义状态模型来表示它的行为: ini
我正在构建一个主要采用顺序流程但也有一些替代路径的程序。我认为状态机可能是最简单的实现方式,因为 Qt 提供了这样一个类:QStateMachine (另见 API)。 不过,我的状态好像挺多的(20
我正在尝试在 Qt (C++) 中实现状态机。如何检查 QStateMachine 的当前状态?我在文档中找不到方法。 谢谢 最佳答案 你试过QStateMachine::configuration(
有一个状态机(称为外部)。这台机器有两个状态——第一个和最后一个。第一个状态是自定义实现的。在第一个状态中创建了另一个状态机(称为内部状态机),在本例中它什么也不做。 具有两个状态的外部状态机:
我正在制作一个应用程序的原型(prototype),我想从应用程序的 C++ 端的 QStateMachine 控制 QML UI 转换。为了使事情更简单,我们可以说 QML UI 由多个页面组成,这
所以我试图了解 Qt 的 QStateMachine 的一个问题,我希望有人能帮助解释为什么会这样。我对 QStateMachine 的基本理解非常感兴趣,而不仅仅是修复。 首先考虑具有状态 A、B
我是一名优秀的程序员,十分优秀!