Qt弹窗实现退出渐变动画

37 篇文章 0 订阅
void
CommonDialog::closeEvent(QCloseEvent *event)
{
    m_impl->closeWithAnimation(this, event);
}
class CommonDialog::Impl {
public:

    ...
	void closeWithAnimation(CommonDialog *dlg, QCloseEvent *event)
	{
		if (!event)
			return dlg->closeEvent(event);

		event->ignore();
		auto graphicsOpacityEffect = new QGraphicsOpacityEffect(dlg);
		graphicsOpacityEffect->setOpacity(1.0);
		dlg->setGraphicsEffect(graphicsOpacityEffect);

		auto opacityAnimation = new QPropertyAnimation(graphicsOpacityEffect, "opacity");
		opacityAnimation->setDuration(m_exitAnimationTimeout);
		opacityAnimation->setStartValue(1.0);
		opacityAnimation->setEndValue(0.0);
		opacityAnimation->setEasingCurve(QEasingCurve::InBounce);
		opacityAnimation->start();
		connect(opacityAnimation, &QPropertyAnimation::finished, dlg, &QDialog::accept);
	}

	void setExitAnimationTimeout(int timeout)
	{
		m_exitAnimationTimeout = timeout;
	}
private:
	int m_exitAnimationTimeout = 300;
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Qt中常见的UI交互控件,可以用于提示信息、警告、错误等。Qt有多种实现方式,以下是其中两种常见的方法: 1. QMessageBox QMessageBox是Qt提供的控件,可以快速创建一个标准的,支持多种类型的,如提示、询问、警告、错误等。使用方式如下: ```cpp QMessageBox::information(this, "标题", "内容"); ``` 其中第一个参数是父口指针,第二个参数是标题,第三个参数是内容。除了information类型外,还有question、warning、critical等类型。 2. 自定义 如果需要更灵活的样式或交互方式,可以自定义。自定义实现方式有很多种,这里介绍一种常见的方法:通过QWidget实现一个模态对话框。代码示例: ```cpp // 自定义类 class MyDialog : public QWidget { Q_OBJECT public: MyDialog(QWidget *parent = nullptr) : QWidget(parent) { setFixedSize(300, 200); setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint); setAttribute(Qt::WA_TranslucentBackground); // 添加控件 QLabel *label = new QLabel("提示信息", this); label->setGeometry(10, 10, 280, 80); label->setAlignment(Qt::AlignCenter); QPushButton *okBtn = new QPushButton("确定", this); okBtn->setGeometry(100, 120, 100, 40); connect(okBtn, &QPushButton::clicked, this, &MyDialog::accept); } void paintEvent(QPaintEvent *) { QPainter painter(this); painter.setBrush(QColor(255, 255, 255)); painter.setPen(Qt::NoPen); painter.drawRoundedRect(rect(), 10, 10); } }; // 出自定义 void MainWindow::on_pushButton_clicked() { MyDialog dlg(this); dlg.exec(); } ``` 在自定义类中,我们继承了QWidget,并重载了paintEvent函数以实现圆角矩形口。在构造函数中添加了的控件,并且设置了口大小、样式等。在槽函数中创建自定义对象,并调用exec函数以模态方式口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

barbyQAQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值