網(wǎng)站首頁 編程語言 正文
按鈕
添加ZButton.qml
import QtQuick 2.14
import QtQuick.Window 2.14
/*
文件名即自定義控件名
使用別名導(dǎo)出屬性:相當(dāng)于函數(shù)的變量形參
不同的是導(dǎo)出的屬性,調(diào)用控件是可以不使用(賦值)
*/
Rectangle {
id: root
//導(dǎo)出自定義屬性
property alias text: label.text
property alias fontSize: label.font.pointSize
property alias textColor: label.color
property alias bgColor: root.color
property alias borderRadius: root.radius
property alias borderColor: root.border.color
property alias borderWidth: root.border.width
signal clicked
width: 116; height: 36
color: "blue"
border.color: "#f3f3f3"
border.width: 1
radius: 4
Text {
id: label
anchors.centerIn: parent
text: "按鈕"
font.family: "微軟雅黑"
font.pointSize: parent.height/3
color: "white"
}
MouseArea {
anchors.fill: parent
hoverEnabled: true
cursorShape: "PointingHandCursor"
onEntered: {
root.opacity = 0.8
}
onExited: {
root.opacity = 1
}
onClicked: {
root.clicked()
}
}
}
注意
在自定義導(dǎo)出屬性的時候不要和最外層的組件的屬性名重復(fù),會覆蓋掉默認的屬性。
使用
import QtQuick 2.14
import QtQuick.Window 2.14
Window {
width: 640
height: 240
visible: true
title: qsTr("Hello World")
ZButton{
text: "我是按鈕"
height: 36
width: 100
fontSize: 12
borderRadius: 18
anchors.horizontalCenter: parent.horizontalCenter
anchors.topMargin: 10
}
}
顯示效果
下拉菜單
添加ZComboBox.qml
import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Templates 2.14 as T
import QtQuick.Controls 2.14
import QtQuick.Controls.impl 2.14
T.ComboBox {
id:control
//checked選中狀態(tài),down按下狀態(tài),hovered懸停狀態(tài)
property color backgroundTheme: "#fefefe"
//下拉框背景色
property color backgroundColor: control.down ? "#eeeeee": control.hovered ? Qt.lighter(backgroundTheme) : backgroundTheme
//邊框顏色
property color borderColor: Qt.darker(backgroundTheme)
//item高亮顏色
property color itemHighlightColor: "#eeeeee"
//item普通顏色
property color itemNormalColor: backgroundTheme
//每個item的高度
property int itemHeight: height
//每個item文本的左右padding
property int itemPadding: 10
//下拉按鈕左右距離
property int indicatorPadding: 3
//下拉按鈕圖標(biāo)
property url indicatorSource: "qrc:/qt-project.org/imports/QtQuick/Controls.2/images/double-arrow.png"
//圓角
property int radius: 4
//最多顯示的item個數(shù)
property int showCount: 5
//文字顏色
property color textColor: "#333333"
property color textSelectedColor: "#222222"
//model數(shù)據(jù)左側(cè)附加的文字
property string textLeft: ""
//model數(shù)據(jù)右側(cè)附加的文字
property string textRight: ""
implicitWidth: 120
implicitHeight: 30
spacing: 0
leftPadding: padding
rightPadding: padding + indicator.width + spacing
font{
family: "微軟雅黑"
pixelSize: 16
}
//各item
delegate: ItemDelegate {
id: box_item
height: control.itemHeight
//Popup如果有padding,這里要減掉2*pop.padding
width: control.width
padding: 0
contentItem: Text {
text: control.textLeft+
(control.textRole
? (Array.isArray(control.model)
? modelData[control.textRole]
: model[control.textRole])
: modelData)+
control.textRight
color: (control.highlightedIndex == index)
? control.textSelectedColor
: control.textColor
leftPadding: control.itemPadding
rightPadding: control.itemPadding
font: control.font
elide: Text.ElideRight
renderType: Text.NativeRendering
verticalAlignment: Text.AlignVCenter
}
hoverEnabled: control.hoverEnabled
background: Rectangle{
radius: control.radius
color: (control.highlightedIndex == index)
? control.itemHighlightColor
: control.itemNormalColor
//item底部的線
Rectangle{
height: 1
width: parent.width-2*control.radius
anchors.bottom: parent.bottom
anchors.horizontalCenter: parent.horizontalCenter
color: Qt.lighter(control.itemNormalColor)
}
}
}
indicator: Item{
id: box_indicator
x: control.width - width
y: control.topPadding + (control.availableHeight - height) / 2
width: box_indicator_img.width+control.indicatorPadding*2
height: control.height
//按鈕圖標(biāo)
Image {
id: box_indicator_img
anchors.centerIn: parent
source: control.indicatorSource
}
}
//box顯示item
contentItem: T.TextField{
//control的leftPadding會擠過來,不要設(shè)置control的padding
leftPadding: control.itemPadding
rightPadding: control.itemPadding
text: control.editable
? control.editText
: (control.textLeft+control.displayText+control.textRight)
font: control.font
color: control.textColor
verticalAlignment: Text.AlignVCenter
//默認鼠標(biāo)選取文本設(shè)置為false
selectByMouse: true
//選中文本的顏色
selectedTextColor: "green"
//選中文本背景色
selectionColor: "white"
clip: true
//renderType: Text.NativeRendering
enabled: control.editable
autoScroll: control.editable
readOnly: control.down
inputMethodHints: control.inputMethodHints
validator: control.validator
renderType: Text.NativeRendering
background: Rectangle {
visible: control.enabled && control.editable
border.width: parent && parent.activeFocus ? 1 : 0
border.color: control.itemHighlightColor
color: "transparent"
}
}
//box框背景
background: Rectangle {
implicitWidth: control.implicitWidth
implicitHeight: control.implicitHeight
radius: control.radius
color: control.backgroundColor
border.color: control.borderColor
}
//彈出框
popup: T.Popup {
//默認向下彈出,如果距離不夠,y會自動調(diào)整()
y: control.height
width: control.width
//根據(jù)showCount來設(shè)置最多顯示item個數(shù)
implicitHeight: control.delegateModel
?((control.delegateModel.count<showCount)
?contentItem.implicitHeight
:control.showCount*control.itemHeight)+2
:0
//用于邊框留的padding
padding: 1
contentItem: ListView {
clip: true
implicitHeight: contentHeight
model: control.popup.visible ? control.delegateModel : null
currentIndex: control.highlightedIndex
//按行滾動SnapToItem ;像素移動SnapPosition
snapMode: ListView.SnapToItem
//ScrollBar.horizontal: ScrollBar { visible: false }
ScrollBar.vertical: ScrollBar { //定制滾動條
id: box_bar
implicitWidth: 10
visible: control.delegateModel&&(control.delegateModel.count>showCount)
//background: Rectangle{} //這是整體的背景
contentItem: Rectangle{
implicitWidth:10
radius: width/2
color: box_bar.pressed
? Qt.rgba(0.6,0.6,0.6)
: Qt.rgba(0.6,0.6,0.6,0.5)
}
}
}
//彈出框背景(只有border顯示出來了,其余部分被delegate背景遮擋)
background: Rectangle{
border.width: 1
border.color: control.borderColor
radius: control.radius
}
}
}
使用方式
import QtQuick 2.14
import QtQuick.Window 2.14
Window {
width: 640
height: 280
visible: true
title: qsTr("Hello World")
ZComboBox{
width: 160
height: 36
model: ["小明","小紅","小剛"]
editable: false
anchors.horizontalCenter: parent.horizontalCenter
anchors.topMargin: 10
}
}
效果
原文鏈接:https://blog.csdn.net/m0_60259116/article/details/127990361
相關(guān)推薦
- 2022-02-23 fatal error: mpi.h: No such file or directory // f
- 2022-12-29 Python?PyQt5實現(xiàn)拖拽與剪貼板功能詳解_python
- 2023-02-27 Python?input()函數(shù)案例教程_python
- 2023-01-18 Python函數(shù)的參數(shù)列表解析_python
- 2022-05-13 C++ 使用Poco庫實現(xiàn)代碼運行時間統(tǒng)計
- 2022-08-12 Python3.8安裝tensorflow的簡單方法步驟_python
- 2022-06-08 golang操作rocketmq的示例代碼_Golang
- 2022-03-22 .NET?6開發(fā)TodoList應(yīng)用之實現(xiàn)查詢排序_實用技巧
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支