You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

QML带标题栏异形窗口实现:中间区域透明的方案疑问

实现中间区域透明且保留原生标题栏的Qt窗口

针对你遇到的问题,这里提供两种场景下的可行方案,无需自定义标题栏:

Qt Widgets 方案

通过设置窗口透明属性,并重写paintEvent绘制上下不透明区域,中间区域自然透明,同时保留系统原生标题栏:

#include <QMainWindow>
#include <QPainter>

class TransparentMiddleWindow : public QMainWindow
{
    Q_OBJECT
public:
    TransparentMiddleWindow(QWidget *parent = nullptr) : QMainWindow(parent) {
        setFixedSize(640, 480);
        // 开启窗口透明背景属性
        setAttribute(Qt::WA_TranslucentBackground);
        // 不设置FramelessWindowHint,保留原生标题栏
    }

protected:
    void paintEvent(QPaintEvent *event) override {
        QMainWindow::paintEvent(event);
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);

        // 绘制顶部不透明区域(高度100)
        painter.fillRect(0, 0, width(), 100, Qt::white);
        // 绘制底部不透明区域(高度200)
        painter.fillRect(0, height() - 200, width(), 200, Qt::white);
    }
};

Qt Quick 方案

设置窗口背景为透明,保留默认窗口标志(含标题栏),通过内部放置矩形组件填充上下区域,中间区域自动透明:

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")
    // 设置窗口背景完全透明
    color: "transparent"
    // 开启透明属性,同时保留原生窗口标题栏标志
    flags: Qt.WA_TranslucentBackground | Qt.Window

    // 顶部不透明区域
    Rectangle {
        width: parent.width
        height: 100
        color: "#ffffff"
        anchors.top: parent.top
    }

    // 底部不透明区域
    Rectangle {
        width: parent.width
        height: 200
        color: "#ffffff"
        anchors.bottom: parent.bottom
    }
}

方案说明

  • 避免使用Qt.FramelessWindowHint,直接保留系统原生标题栏;
  • 通过WA_TranslucentBackground让窗口背景透明,再手动绘制/添加需要显示的不透明区域,中间区域自然呈现透明效果;
  • 无需双窗口绑定,避免了窗口移动时的延迟问题。

内容的提问来源于stack exchange,提问作者Murat Şeker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 10:05:25