QT 点击按钮横向缓缓展开控件,label渐变消失和显示

编程入门 行业动态 更新时间:2024-10-25 16:17:33

QT 点击按钮<a href=https://www.elefans.com/category/jswz/34/1768009.html style=横向缓缓展开控件,label渐变消失和显示"/>

QT 点击按钮横向缓缓展开控件,label渐变消失和显示

点击按钮横向缓缓展开控件

效果图

关键代码

form.cpp

#include "form.h"
#include "ui_form.h"
const int Scaling_factor = 5;Form::Form(QWidget *parent) :QWidget(parent),ui(new Ui::Form)
{ui->setupUi(this);setAttribute(Qt::WA_TranslucentBackground);setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);timer = new QTimer;connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout()));mLength = width();minimumLength = 0;maximumLength = mLength;mIsBtn = false;
}Form::~Form()
{delete ui;
}void Form::showBtn()
{QRect rect;rect.setX(955);  //rect.setX(100);  弹出位置rect.setY(600);   //rect.setY(200);rect.setHeight(height());rect.setWidth(minimumLength);setGeometry(rect);show();minimumLength += Scaling_factor;if (minimumLength >= mLength)timer->stop();update();
}void Form::closeBtn()
{QRect rect;rect.setX(x());rect.setY(y());rect.setHeight(height());rect.setWidth(maximumLength);setGeometry(rect);maximumLength -= Scaling_factor;if (maximumLength <= 0){timer->stop();close();}update();
}void Form::onTimeout()
{if (mIsBtn == true){showBtn();}else if(mIsBtn == false){closeBtn();}
}void Form::onShowBtnClicked()
{if(timer->isActive())return;timer->start(5);if (mIsBtn == false){mIsBtn = true;minimumLength = 0;}else if (mIsBtn == true){mIsBtn = false;maximumLength = mLength;}
}void Form::on_pushButton_clicked()
{if(timer->isActive())return;timer->start(5);if (mIsBtn == false){mIsBtn = true;minimumLength = 0;}else if (mIsBtn == true){mIsBtn = false;maximumLength = mLength;}
}

mainwindow.cpp

w = new Form;connect(this, SIGNAL(showBtnClicked()), w, SLOT(onShowBtnClicked())); //构造void MainWindow::on_pushButton_clicked()
{emit showBtnClicked();
}

label渐变消失和显示

效果图

关键代码

构造

    m_pGraphicsOpacityEffect = new QGraphicsOpacityEffect(ui->label);m_pGraphicsOpacityEffect->setOpacity(1);ui->label->setGraphicsEffect(m_pGraphicsOpacityEffect);m_pNameAnimation = new QPropertyAnimation(m_pGraphicsOpacityEffect,"opacity",this);m_pNameAnimation->setEasingCurve(QEasingCurve::InOutExpo);  //淡出效果m_pNameAnimation->setDuration(1000);                        //动画时间

调用

void MainWindow::on_pushButton_2_clicked()
{m_pNameAnimation->setStartValue(1); //关闭labelm_pNameAnimation->setEndValue(0);m_pNameAnimation->start(QAbstractAnimation::KeepWhenStopped);
}void MainWindow::on_pushButton_3_clicked()
{m_pNameAnimation->setStartValue(0);//开启labelm_pNameAnimation->setEndValue(1);m_pNameAnimation->start(QAbstractAnimation::KeepWhenStopped);
}

参考了网上的例子进行了部分修改,怕忘掉所以记录下来。另制作gif的软件为GifCam,免费好用,推荐。

另附链接:工程连接 代码命名比较随意。。

更多推荐

QT 点击按钮横向缓缓展开控件,label渐变消失和显示

本文发布于:2024-02-25 21:24:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1700339.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:横向   控件   按钮   QT   label

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!