Qt实现类似ToDesk顶层窗口 & 不规则按钮

先看效果:

在进行多进程开发时,可能会遇到需要进行全局弹窗的需求。

因为平时会使用ToDesk进行远程桌面控制,在电脑被控时,ToDesk会在右下角进行一个顶层窗口的提示,效果如下:

其实要实现顶层窗口,最关键的是设置窗口属性:

cpp 复制代码
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);

同时一般也不太需要Windows状态栏的图标,我们可以这样,加一个Qt::Tool上去:

cpp 复制代码
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool);

动画效果的使用,会使程序的呈现更加奈斯:

cpp 复制代码
// 初始化
{
    moveAnimation_ = new QPropertyAnimation(this,"pos",this);
    QScreen *primaryScreen = QGuiApplication::primaryScreen();
    if(primaryScreen==nullptr) { return; }
    geometry=primaryScreen->availableGeometry();
    this->move(geometry.width()-300-pix_right_.width(),geometry.height()-200);
    setFixedSize(300+pix_right_.width(), 200);
}

// 调用
{
    QString imageName = is_expanded_ ? "right" : "left";
    QPoint showPoint = QPoint(geometry.width()-300-pix_right_.width(),geometry.height()-200);
    QPoint hidePoint = QPoint(geometry.width()-pix_right_.width(),geometry.height()-200);
    if(is_expanded_) {
        startAnimation(showPoint, hidePoint);
        btn_->setMask(pix_right_.mask());
    } else {
        startAnimation(hidePoint, showPoint);
        btn_->setMask(pix_left_.mask());
    }
}

// target目标点,oldpos起始点
void AssistDlg::startAnimation(QPoint target, QPoint oldpos)
{
    moveAnimation_->setDuration(300);
    moveAnimation_->setStartValue(oldpos);
    moveAnimation_->setEndValue(target);
    moveAnimation_->setEasingCurve(QEasingCurve::OutCubic);
    moveAnimation_->start();
}

不规则按钮的主要代码如下:

cpp 复制代码
    btn_ = new QPushButton(this);
    btn_->setFixedSize(pix_right_.size());
    btn_->setMask(pix_right_.mask());
    btn_->setStyleSheet("background-image: url(:/images/right.png)");
    btn_->setStyleSheet("QPushButton{"
                        "border-image:url(:/images/right.png)}"
                        );
相关推荐
道不尽世间的沧桑33 分钟前
第17篇:网络请求与Axios集成
开发语言·前端·javascript
久绊A41 分钟前
Python 基本语法的详细解释
开发语言·windows·python
软件黑马王子4 小时前
C#初级教程(4)——流程控制:从基础到实践
开发语言·c#
cpp_learners4 小时前
QT 引入Quazip和Zlib源码工程到项目中,无需编译成库,跨平台,压缩进度
qt·zlib·加密压缩·quazip
闲猫4 小时前
go orm GORM
开发语言·后端·golang
李白同学6 小时前
【C语言】结构体内存对齐问题
c语言·开发语言
黑子哥呢?7 小时前
安装Bash completion解决tab不能补全问题
开发语言·bash
青龙小码农7 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿7 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
彳卸风8 小时前
Unable to parse timestamp value: “20250220135445“, expected format is
开发语言