当前位置:网站首页>QML(二):设置自定义窗体
QML(二):设置自定义窗体
2022-07-29 11:05:00 【God港】
1.思路
自定义窗体主要有标题栏和内容框组成,于是我们设计两个组件分别为QCustomTitleBar和QCustomWindow
2.QCustomTitleBar.qml
用Row定位器放置常用的三个按钮(其实是四个,最大化和正常模式都是一个按钮)。
借用上文中的自定义button我们直接上代码
import QtQuick 2.0
Rectangle {
property bool isMaxed: false//用于判断当前是最大化还是正常模式
color: "green"
Row{
id: controlButtons
height: 20
anchors.verticalCenter: parent.verticalCenter
anchors.right: parent.right
anchors.rightMargin: 12
spacing: 10
//其中view是主界面的id
TImgTextBtn{
id:min_button
width: 20
height: 20
currentStyle:TImgTextBtn.Style.CircleButton
text:""
onClicked: {
view.showMinimized()
}
}
TImgTextBtn{
id:max_button
width: 20
height: 20
currentStyle:TImgTextBtn.Style.CircleButton
text:""
onClicked: {
view.showMaximized()
isMaxed = true
}
visible: !isMaxed
}
TImgTextBtn{
id:normal_button
width: 20
height: 20
visible: isMaxed
currentStyle:TImgTextBtn.Style.CircleButton
text:""
onClicked: {
view.showNormal()
isMaxed = false
}
}
TImgTextBtn{
id:closing_button
width: 20
height: 20
currentStyle:TImgTextBtn.Style.CircleButton
text:""
onClicked: {
view.close()
}
}
}
}
3.QCustomWindow.qml
这个较为简单主要写了个鼠标操作类,测试效果
import QtQuick 2.0
import QtQuick.Controls 2.0
Rectangle {
Rectangle {
x: 200
y: 200
anchors.fill: parent
color: "#5088E0"
//实例化一个MoveArea
QMouseArea {
//指定control为parent。其实默认就是parent,写出来示意一下
operateControl: parent
anchors.fill: parent
enableStatus: true
}
}
}
4.QMouseArea.qml
鼠标测试可用于所有组件,我们可以在这个qml中设置各种事件,本例子主要是改变鼠标样式和拖动功能,具体效果自行测试
import QtQuick 2.0
MouseArea {
id:mouse_area
//定义按下鼠标终点位置
property real endX:0
property real endY:0
property bool enableStatus:false //是否启用拖动
property var operateControl:parent //操作控件对象
hoverEnabled: true
//按下事件记录鼠标位置
onPressed: {
endX=mouseX
endY=mouseY
}
//鼠标进入当前区域事件
onContainsMouseChanged: {
if(containsMouse)
{
cursorShape=Qt.SizeAllCursor
}
else
{
cursorShape=Qt.ArrowCursor
}
}
//鼠标移动事件
onPositionChanged: {
if(enableStatus&&operateControl&&pressed)
{
//作用的控件平移鼠标拖动的距离
operateControl.x+=mouseX-endX
operateControl.y+=mouseY-endY
}
}
}
5.main.aml
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
id:view
visible: true
width: 640
height: 480
title: qsTr("Hello World")
flags:Qt.FramelessWindowHint
Column{
anchors.fill: parent
spacing:0//设置组件之间的距离
QCustomTitleBar{
width: parent.width
height: 20
}
QCustomWindow{
id:main_window
width: parent.width
height: parent.height-20
}
}
}
6.效果
边栏推荐
- Leetcode bit operation
- Why should kubernetes be used in development environments
- 深入理解C# 进入快速通道的委托
- Xiaoxiao authorization system V5.0 happy version
- Starrocks technology insider: how to have both real-time update and fast query
- leetcode-位运算
- 专访 | 阿里巴巴首席技术官程立:云 + 开源共同形成数字世界的可信基础
- 就这?TypeScript其实并不难!(建议收藏)
- WPF 截图控件之绘制方框与椭圆(四) 「仿微信」
- 【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
猜你喜欢

How to realize the function of adding watermark

暑假集训week1

VMware: use commands to update or upgrade VMware esxi hosts

幸运抽奖系统带后台源码

ECCV 2022 | SSP: 自支持匹配的小样本任务新思想

PyQt5快速开发与实战 6.6 QFormLayout(表单布局) && 6.7 嵌套布局 && 6.8 QSplitter

Pyqt5 rapid development and practice 6.6 qformlayout & 6.7 nested layout & 6.8 qsplitter
![[image processing] image skeleton extraction based on central axis transformation with matlab code](/img/34/80e777c5c0a2a791acd0892e3e0b04.png)
[image processing] image skeleton extraction based on central axis transformation with matlab code

WPF 截图控件之绘制方框与椭圆(四) 「仿微信」

专访 | 阿里巴巴首席技术官程立:云 + 开源共同形成数字世界的可信基础
随机推荐
Zhou Hongyi: 360 is the largest secure big data company in the world
PHP basics uses arrays to save data
Gbase8s core data backup
判断两个对象的值是否都相等
2022 latest WiFi master applet independent version 3.0.8
IPV6基础
Hugo NexT V4 介绍
Package Delivery(贪心)
"Knowledge Collection" article to understand mysql index!!(recommended collection)
使用anyio替代asyncio
Leetcode bit operation
golang 实现文件上传下载
[image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code
如何开始为您的 Kubernetes 应用程序编写 Helm 图表
How to use grep to display file names and line numbers before matching lines
INVALID_ARGUMENT : Invalid rank for input: modelInput Got: 3 Expected: 4 Please fix either the input
TCP和UDP
正则表达式匹配网址
How to realize the function of adding watermark
Design and implementation of gbase8s Informix dodker high availability cluster self recovery cluster startup command oninitdb