Qt 定义动画消失的位置(x,y)

参考至:
QT下的几种透明效果(QPalette背景白色,窗口设置setWindowOpacity,QPainter使用Clear模式绘图)
https://www.cnblogs.com/findumars/p/7487893.html

窗口整体不透明,局部透明:
在Paint事件中使用Clear模式绘图。

void TestWindow::paintEvent( QPaintEvent* ) 
{ 
	QPainter p(this); 
	p.setCompositionMode( QPainter::CompositionMode_Clear ); 
    p.fillRect( 10, 10, 300, 300, Qt::SolidPattern ); 
} 

试验效果:绘制区域全透明。如果绘制区域有控件不会影响控件。

效果图:
在这里插入图片描述
在这里插入图片描述
红线是自己painter画的临界点,到达这个(x,y)值的时候图标逐渐消失(实质上是红线左边画了透明区域,进入的painter也会随着透明)

代码:
widget.h

class Widget : public QWidget
{
    Q_PROPERTY(QPoint mPoint READ getmPoint WRITE setmPoint)
    Q_OBJECT
public:
    Widget(QWidget *parent = 0);
    ~Widget();

    QPoint getmPoint() {
        return mPoint;
    }
    void setmPoint( QPoint newPoint ) {
        mPoint = newPoint;
    }
    //void paintGL();
    void paintEvent(QPaintEvent *e);
private:
    QPoint mPoint;
};

widget1.h

class Widget1 : public QOpenGLWidget
{
    Q_OBJECT
public:
    explicit Widget1(QWidget *parent = 0);
    void paintEvent(QPaintEvent *e);
};

window.h

class Window : public QWidget
{
    Q_OBJECT
public:
    explicit Window(QWidget *parent = 0);
};

widget.cpp

void Widget::paintEvent(QPaintEvent *e)
{
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glDisable(GL_DEPTH_TEST);//关闭更新深度缓冲区,
QPainter painter;
painter.begin(this);
painter.setRenderHints(QPainter::Antialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);

painter.translate(getmPoint()); //****getmPoint()
painter.drawPixmap(0,60,128,128,QPixmap(":/images/image/armchair_128px.png"));
painter.drawPixmap(150,60,128,128,QPixmap(":/images/image/binoculars_128px.png"));
painter.drawPixmap(300,60,128,128,QPixmap(":/images/image/direction_sign_128px.png"));

painter.drawPixmap(0,200,128,128,QPixmap(":/images/image/gas-station_128px.png"));
painter.drawPixmap(150,200,128,128,QPixmap(":/images/image/giftbox_128px.png"));
painter.drawPixmap(300,200,128,128,QPixmap(":/images/image/lamp-2_128px.png"));
painter.translate(-getmPoint()); //****

painter.setCompositionMode( QPainter::CompositionMode_Clear );
painter.fillRect( 0,0,200,360, Qt::SolidPattern );

update();
painter.end();
}

widget1.cpp

void Widget1::paintEvent(QPaintEvent *e)
{
    QPainter painter;
    painter.begin(this);
	painter.drawPixmap(0,0,960,360,QPixmap(":/images/image/sishen.png"));
    QPen pen(Qt::red,3);
    painter.setPen(pen);
    painter.drawLine(200,0,200,360);
    painter.end();
}

window.cpp

Window::Window(QWidget *parent) : QWidget(parent)
{
    this->resize(960,360);
    Widget1 *m_widget1 = new Widget1(this);
    Widget *m_widget = new Widget(this);

    QGridLayout *layout = new QGridLayout(this);
    layout->setMargin(0);
    layout->addWidget(m_widget,0,0);
    layout->addWidget(m_widget1,0,0);
    setLayout(layout);

    QPropertyAnimation *Animation = new QPropertyAnimation(m_widget, "mPoint");
    Animation->setDuration(5000); //持续时间
    Animation->setStartValue(QPoint(340, 0));
    Animation->setEndValue(QPoint(0, 0));
    Animation->setEasingCurve(QEasingCurve::Linear);

    QParallelAnimationGroup *m_group = new QParallelAnimationGroup(this);
    m_group->addAnimation( Animation );
    m_group->setDirection(QAbstractAnimation::Forward); //Backward显示  Forward消失
    m_group->setLoopCount(1);
    m_group->start();
}

main.cpp

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Window win;
    win.show();
    return a.exec();
}
发布了12 篇原创文章 · 获赞 3 · 访问量 1407
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览