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)}"
                        );
相关推荐
lemon_sjdk1 小时前
ObjectProperty
java·开发语言·算法
大模型码小白1 小时前
Spring AI Tool 实现自然语言操作 MySQL 数据库详解
服务器·开发语言·数据库·人工智能·python·mysql·spring
hh9501 小时前
Agent Plan x DeepSeek Harness:Agent 供应链安全与第三方插件审计
java·开发语言·安全·agent plan·adg成都社区·adg社区
「QT(C++)开发工程师」1 小时前
C++ std::move 详解
开发语言·c++
matlab代码1 小时前
基于matlab的玉米种子计数设计(GUI界面)【源码58期】
开发语言·matlab
c#上位机2 小时前
C#上位机项目实战——C#的dll项目编译时拷贝到指定目录下
开发语言·c#
Littlehero_1212 小时前
QT自定义控件之内嵌报表(4)(源码开源)
开发语言·qt
晓晓_za8986682 小时前
开源 GEO 优化源码二次开发:贴牌改造与业务模块扩展实践
java·运维·服务器·开发语言·性能优化·开源
曹牧2 小时前
C#:数组与列表的差异
开发语言·c#
LINgZone22 小时前
后端Java编码规范
java·开发语言