前言
`在实际的开发中,QML自带的组件基本上不会直接用,因为需要花里胡哨的样式,所以需要我们。`
运行效果
知识点
- ApplicationWindow
- Item
- MouseArea
- Button
实现思想
// <1> 隐藏原生窗口的系统标题栏
// Qt.Window >> 指定该部件为窗口属性。请注意,如果小部件没有父级,则无法取消设置此标志。
// Qt.FramelessWindowHint >> 设置窗口属性为无边框样式
flags: Qt.Window | Qt.FramelessWindowHint
<2> 自定义窗口标题栏样式
// 直接定义一个 Item 或者其他组件作为窗口的标题栏即可
// 窗口的id为 rootWindow
// 处理窗口 onTitleChanged 信号,同步到自定义标题栏即可
Rectangle {
id : rootWindowMenuBar
width: rootWindow.width
height: 40
Text {
id: rootWindowTitleBarTitleText
text: rootWindow.title
}
}
<3> 自定义窗口标题栏事件
// MouseArea >> 不可见的组件,其功能提供鼠标处理信号
MouseArea {
MouseArea {
anchors.fill: rootWindowTitleBar
// 只处理鼠标左键
acceptedButtons: Qt.LeftButton
// 接收鼠标按下事件
onPressed: {
rootWindow.rootWindowTitleMousePos = Qt.point(mouseX,mouseY)
rootWindow.isMoveWindow = true
}
// 鼠标释放,关闭窗口移动flag
onReleased: {
if(mouse.button === Qt.LeftButton){
rootWindow.isMoveWindow = false
}
}
//
onMouseXChanged: {
if(rootWindow.isMoveWindow){
rootWindow.x += mouseX - rootWindow.rootWindowTitleMousePos.x;
}
}
onMouseYChanged: {
rootWindow.y += mouseY - rootWindow.rootWindowTitleMousePos.y;
}
}
<4> 自定义窗口标题栏样式
Button {
id : pushbtnWindowsClose
width: 30
height: 30
anchors.margins: 5
anchors.right: rootWindowTitleBar.right
anchors.topMargin: 5
anchors.verticalCenter: parent.verticalCenter
text: "X"
}
Button {
id : pushbtnWindowsMaximize
width: 30
height: 30
anchors.margins: 5
anchors.verticalCenter: parent.verticalCenter
anchors.right: pushbtnWindowsClose.left
text: "口"
}
Button {
id : pushbtnWindowsMinimize
width: 30
height: 30
anchors.margins: 5
anchors.verticalCenter: parent.verticalCenter
anchors.right: pushbtnWindowsMaximize.left
text: "一"
}
完整QML代码
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5
ApplicationWindow {
property bool isMoveWindow: false
property point rootWindowTitleMousePos: Qt.point(x,y)
id : rootWindow
visible: true
width: 640
height: 480
title: qsTr("Hello World")
flags: Qt.Window | Qt.FramelessWindowHint
onTitleChanged: rootWindowTitleBarTitleText.text = title
Rectangle {
id : rootWindowTitleBar
width: rootWindow.width
height: 40
border.color: "red"
color: "black"
Text {
id: rootWindowTitleBarTitleText
text: rootWindow.title
color: "white"
anchors.verticalCenter: parent.verticalCenter
}
// 为窗口标题栏添加鼠标事件
MouseArea {
anchors.fill: rootWindowTitleBar
// 只处理鼠标左键
acceptedButtons: Qt.LeftButton
// 接收鼠标按下事件
onPressed: {
rootWindow.rootWindowTitleMousePos = Qt.point(mouseX,mouseY)
rootWindow.isMoveWindow = true
}
// 鼠标释放,关闭窗口移动flag
onReleased: {
if(mouse.button === Qt.LeftButton){
rootWindow.isMoveWindow = false
}
}
//
onMouseXChanged: {
if(rootWindow.isMoveWindow){
rootWindow.x += mouseX - rootWindow.rootWindowTitleMousePos.x;
}
}
onMouseYChanged: {
rootWindow.y += mouseY - rootWindow.rootWindowTitleMousePos.y;
}
}
Button {
id : pushbtnWindowsClose
width: 30
height: 30
anchors.margins: 5
anchors.right: rootWindowTitleBar.right
anchors.topMargin: 5
anchors.verticalCenter: parent.verticalCenter
text: "X"
}
Button {
id : pushbtnWindowsMaximize
width: 30
height: 30
anchors.margins: 5
anchors.verticalCenter: parent.verticalCenter
anchors.right: pushbtnWindowsClose.left
text: "口"
}
Button {
id : pushbtnWindowsMinimize
width: 30
height: 30
anchors.margins: 5
anchors.verticalCenter: parent.verticalCenter
anchors.right: pushbtnWindowsMaximize.left
text: "一"
}
}
}
作者:
怪小子
Github:
https://github.com/MrSunHua
邮箱:sh4a01@163.com
本文版权归作者和博客园共有,欢迎转载,如文章有版权冲突或者有更好的见解及建议,还望联系博主讨论并修改。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具