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)}"
                        );
相关推荐
234710212711 小时前
4.16 学习笔记
开发语言·软件测试·python
014-code11 小时前
日志规范:怎么写才不算写废话
java·开发语言·设计模式·日志
云中飞鸿11 小时前
如何编译编译 Qwt-5.2.0?
qt
Binarydog_Lee11 小时前
Rust 核心机制:所有权、借用与生命周期
开发语言·rust
XMYX-011 小时前
17 - Go 通道 Channel 底层原理 + 实战详解
开发语言·golang
Hello--_--World12 小时前
ES13:类私有属性和方法、顶层 await、at() 方法、Object.hasOwnProperty()、类静态块 相关知识点
开发语言·javascript·es13
Hugh-Yu-13012312 小时前
二元一次方程组求解器c++代码
开发语言·c++·算法
weixin_5206498712 小时前
C#进阶-特性全知识点总结
开发语言·c#
文祐12 小时前
C++类之虚函数表及其内存布局
开发语言·c++
编程大师哥12 小时前
C++类和对象
开发语言·c++·算法