横向缓缓展开控件,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渐变消失和显示
发布评论