当前位置:网站首页>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.效果
边栏推荐
- WPF 截图控件之绘制方框与椭圆(四) 「仿微信」
- ECCV 2022 | SSP: 自支持匹配的小样本任务新思想
- What is kubernetes custom resource definition (CRD)?
- Exclusive interview | Cheng Li, chief technology officer of Alibaba: cloud + open source together form a credible foundation for the digital world
- 小笑授权系统V5.0开心版
- sql join中on条件后接and和where
- 从零开始Blazor Server(3)--添加cookie授权
- 基于flask写的一个小商城mall项目
- Analysis of QT basic engineering
- Watch the open source summit first | quick view of the sub Forum & Activity agenda on July 29
猜你喜欢

Qt 之自定义界面(实现无边框、可移动)

Basic construction of QT project
![[image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code](/img/c1/f962f1c1d9f75732157d49a5d1d0d6.png)
[image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code

重磅 | 2022 开放原子全球开源峰会在北京开幕

std::vector 拷贝、追加、嵌套访问

【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码

IPv6 Foundation

Function comparison between report control FastReport and stimulus soft

Why should kubernetes be used in development environments

Peking University open classes are coming! Welcome to the "AI for science" class
随机推荐
Hugo NexT V4 介绍
Gbase8s core data backup
牛客网刷题
How to use grep to find pattern matching across multiple lines
2022最新 wifi大师小程序独立版3.0.8
2022 latest WiFi master applet independent version 3.0.8
Watch the open source summit first | quick view of the sub Forum & Activity agenda on July 29
Great golang Road
How to realize the function of adding watermark
The heavy | open atomic school source activity was officially launched
DoD 和 DoR,消减「认知偏差」的两大神器
DNS协议、ICMP协议、NAT技术
INVALID_ ARGUMENT : Invalid rank for input: modelInput Got: 3 Expected: 4 Please fix either the input
Hutool日期时间
基本.分块
matplotlib中文问题
Watch the open source summit first | quick view of the sub Forum & Activity agenda on July 29
SkiaSharp 之 WPF 自绘 弹动小球(案例版)
小笑授权系统V5.0开心版
Site data collection -scrapy usage notes