C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – Qt中的动画绑定变化大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_801_2@
当绑定发生变化时,我正试图找到一种在QML元素上进行转换的方法.假设您有一个Text元素,text属性绑定到某个东西.我想要的是当绑定中的数据发生变化时,元素淡出(仍显示旧数据),切换并淡入新数据(当元素不可见时发生实际转换.)

我一直在寻找一种方法来做到这一点,但我可以搞清楚.我已尝试在QML中使用Qt Quick动画,但数据本身在动画运行之前发生了变化,不再需要动画.我已经尝试创建一个自定义的QDeclarativeItem对象,该对象在QDeclarativeItem :: paint()中调用动画,但我无法弄清楚如何让它实际运行.

我应该在这里注意,我知道我的绑定工作正常,因为显示的数据发生了变化,我无法让这些动画在适当的时间运行.

以下是我尝试使用QML的内容

Text {
    id: focusText
    text: somedata

    Behavior on text {
         SequentialAnimation {
             numberAnimation { target: focusText; property: "opacity"; to: 0; duration: 500 }
             numberAnimation { target: focusText; property: "opacity"; to: 1; duration: 500 }
         }
     }
}

这是我在实现自定义QDeclarativeItem时尝试的:

// PAINTER
void AnimatedBinding::paint(QPainter *painter,const QStyLeoptionGraphicsItem *option,QWidget *widget) {
    // Setup the pen
    QPen pen(m_color,2);
    painter->setPen(pen);
    painter->setOpacity(this->opacity());

    // Draw the item
    if (m_bindingType == QString("text")) {
        QPropertyAnimation animation(this,"opacity");
        animation.setDuration(1000);
        animation.setStartValue(1);

        if (drawn) {
            animation.setStartValue(1);
            animation.setEndValue(0);
            animation.start();
        } else drawn = true;

        painter->drawText(boundingRect(),m_data.toString());
        animation.setEndValue(0);
        animation.start();
    } else {
        qCritical() << "Error unkNown binding type!";
        return;
    }
}

但就像我说的那样,我在画家中开始的动画从未实际发射过.

有小费吗?有没有人曾经这样做过?我一直在敲打这个约一个星期.

@H_801_2@

解决方法

如何在qml中这样做:

>定义您自己类型的自定义元素,其行为方式与您希望的一样.@H_616_30@>使用此元素代替传统元素进行动画处理.

例如.我创建了一个自定义的’AnimatedText’类型,以便在文本元素相关的文本发生变化时,对文本元素进行淡入和淡出行为.

文件1:AnimatedText.qml

import QtQuick 1.0

Item
{
    id: topParent
    property String aText: ""
    property String aTextColor: "black"
    property int aTextFontSize: 10
    property int aTextAnimationTime : 1000

    Behavior on opacity { numberAnimation { duration: aTextAnimationTime } }

    onATextChanged:
    {
         topParent.opacity = 0
         junkTimer.running = true
    }

    Timer
    {
       id: junkTimer
       running: false
       repeat: false
       interval: aTextAnimationTime
       ontriggered:
       {
           junkText.text = aText
           topParent.opacity = 1
       }
    }

    Text
    {
        id: junkText
        anchors.centerIn: parent
        text: ""
        font.pixelSize: aTextFontSize
        color: aTextColor
    }
}

你的main.qml中

import QtQuick 1.0

Rectangle
{
    id: topParent
    width:  360
    height: 360

    AnimatedText
    {
      id: someText

      anchors.centerIn: parent
      aText: "Click Me to change!!!.."
      aTextFontSize: 25
      aTextColor: "green"
      aTextAnimationTime: 500
    }

    MouseArea
    {
        anchors.fill: parent
        onClicked:
        {
            someText.aText = "Some random junk"
        }
    }
}
@H_801_2@ @H_801_2@
@H_801_2@
@H_801_2@

大佬总结

以上是大佬教程为你收集整理的c – Qt中的动画绑定变化全部内容,希望文章能够帮你解决c – Qt中的动画绑定变化所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。