QML自定义控件Button
QT自带的Button无法满足我们的审美,和需求,为此项目开发中经常需要自定义控件,以Button控件为例
联系作者 qq 843230304
效果图(里面的按钮都是Button):还有好多呢,你只需要自定义自己的图片,很美观哦
写控件的时候,注意控件的属性,不要在内部赋值或者修改,要抛出信号供外部处理修改(否则解绑了属性)
使用举例
Button{id:myBtnanchors.verticalCenter: parent.verticalCenteranchors.left: parent.leftanchors.leftMargin: 30pressedSource: 图片按下的效果图资源路径normalSource: 图片正常的效果图资源路径text:qsTr("按钮名称")pixelSize:字体像素textRightImage:true //文本在按钮右侧(内部属性)rightMargin:50//内部属性spacing:40//内部属性horizontalAlignment: Text.AlignRightverticalAlignment: Text.AlignVCenteronClicked: {//}}
附上QML源码
import QtQuick 2.0Item {id:rootproperty string normalSource: ""property string pressedSource: ""property string disableSource: ""property bool selectEnable: falseproperty bool selectAuto: trueproperty bool selected: falseproperty string normalSelectSource: ""property string pressedSelectSource: ""//文本在图片的方位property bool textUnderImage: false //文本在图片下方property bool textOverImage: false //文本在图片上方property bool textLeftImage: false //文本在图片左方property bool textRightImage: false //文本在图片右方property color textNormalColor: "white"property color textPressedColor: "white"property color textNormalSelectColor: "white"property color textPressedSelectColor: "white"property color textDisableColor: "gray"property alias textWidth: buttonText.widthproperty alias textHeight: buttonText.heightproperty alias text: buttonText.textproperty alias elide: buttonText.elideproperty alias horizontalAlignment: buttonText.horizontalAlignmentproperty alias verticalAlignment: buttonText.verticalAlignmentproperty int textHorizontalOffset: 0property int textVerticalOffset: 0property alias pixelSize: buttonText.font.pixelSizeproperty int topMargin: 0property int bottomMargin: 0property int leftMargin: 0property int rightMargin: 0property int spacing: 0signal clickedsignal pressed(real xPosition,real yPosition)signal pressAndHoldsignal released(real xPosition,real yPosition)signal positionChanged(real xPosition,real yPosition)implicitWidth: (textLeftImage || textRightImage) ? (leftMargin + image.width + spacing + buttonText.width + rightMargin) :(leftMargin + image.width + rightMargin)implicitHeight: (textUnderImage || textOverImage) ? (topMargin + image.height + spacing + buttonText.height + bottomMargin) :(topMargin + image.height + bottomMargin)Image{id: imagevisible: truex:textLeftImage ? (leftMargin + buttonText.width + spacing): leftMarginy:textOverImage ? (topMargin + buttonText.height + spacing) : topMargin}Text {id: buttonTextx: textLeftImage ? (leftMargin) :textRightImage ? (leftMargin + image.width + spacing) :(leftMargin + image.width/2-width/2 + textHorizontalOffset)y: textOverImage ? (topMargin) :textUnderImage? (topMargin + image.height + spacing) :(topMargin + image.height/2-height/2 + textVerticalOffset)font.pixelSize: 30}state:"normal"states:[State{name:"normal"PropertyChanges {target:imagesource: normalSource}PropertyChanges {target:buttonText;color:textNormalColor}when:(enabled)&&(!selectEnable || (selectEnable && !selected)) && !imageMouseArea.pressed},State{name:"pressed"PropertyChanges {target:imagesource:pressedSource}PropertyChanges {target:buttonText;color:textPressedColor}when:(enabled)&&(!selectEnable || (selectEnable && !selected)) && imageMouseArea.pressed},State{name:"normalSelect"PropertyChanges {target:image;source: normalSelectSource}PropertyChanges {target:buttonText;color:textNormalSelectColor}when:(enabled)&&(selectEnable && selected) && !imageMouseArea.pressed},State{name:"pressedSelect"PropertyChanges {target:image;source:pressedSelectSource}PropertyChanges {target:buttonText;color:textPressedSelectColor}when:(enabled)&&(selectEnable && selected) && imageMouseArea.pressed},State{name:"disable"PropertyChanges {target:buttonText;color:textDisableColor}PropertyChanges {target:image;source:disableSource}when:!enabled}]MouseArea {id: imageMouseAreaanchors.fill: parentonPressed: {root.pressed(mouse.x,mouse.y);}onReleased: {root.released(mouse.x,mouse.y);if(selectEnable && selectAuto){selected = !selected;}}onPositionChanged:{root.positionChanged(mouse.x,mouse.y);}onClicked: {root.clicked();}onPressAndHold: {root.pressAndHold();}}
}
QML自定义控件Button相关推荐
- QML 自定义控件Button,采用QtQuick.Controls 1.0和2.0两版本实现
在开发QtQuick项目时,对于界面上的控件Controls往往会重复使用,单个界面上会有多个同样的Controls.在调用系统Controls库时,往往一些属性,如字体.样式.大小等需要多次去编写, ...
- QML AbstractButton | Button | ButtonGroup | Action
AbstractButton | Button | ButtonGroup | Action 目录 AbstractButton QML Type Button QML Type Button ...
- QML 自定义控件 时间滚筒控件
QML 仿手机时间滚筒控件 效果:共3中模式 "Date" | "Time" | "DateTime" main.qml MouseAre ...
- qml学习---------------Button属性
本来学习了qml中的button,其实qml中的button跟qtQPushButton以及其他语言中的按钮是相同的. 首先通过一个简单的例子来学习button. import QtQuick 2.2 ...
- qml自定义控件----自定义菜单
效果 摘要 这段时间在写树的时候需要用到右键菜单,但是原生的不太好看,且用起来有点僵硬(不太熟悉),于是抱着手艺人的素养,就自己鼓捣了一个出来,虽然不一定非常通用,但是自己做出来的使用比较方便,后期的 ...
- 第48篇 QML自定义控件 — 完成实现登入界面显示
1.前期准备 Label 文本设置.字体设置 text: "登入密码" font.family: "微软雅黑" font.pixelSize: 20 TextF ...
- Qt自定义QML模块
自定义QML模块 含义为将常用风格的Button,Text,RadioButton,或者自定义的控件作为一个控件进行使用,节省代码. 优点: 代码简洁,减少重复代码 自定义的控件进行封装重复使用 可以 ...
- Qt 学习之路 2(79):QML 组件
前面我们简单介绍了几种 QML 的基本元素.QML 可以由这些基本元素组合成一个复杂的元素,方便以后我们的重用.这种组合元素就被称为组件.组件就是一种可重用的元素.QML 提供了很多方法来创建组件.不 ...
- qml demo分析(clocks-时钟)
一.效果展示 效果如图1所示,时钟列表支持鼠标左右拖动,带有黑色背景的是晚上时钟,无黑色背景的是白天时钟 二.源码分析 1.main.cpp文件中只包含了一个宏,该宏的具体解释请看qml 示例中的关键 ...
最新文章
- 基于Matlab和Wind SQL数据库的通用选股策略回测程序
- python(pil)图像处理(等比例压缩、裁剪压缩) 缩略(水印)图
- linux6用户t密码,linux系统 用户和组管理类命令的使用方法
- linux c icmp协议 判断主机存活
- jQuery EasyUI datagrid本地分页
- touchWX 自定义组件以及传值
- arcgis 获取json经纬度_干货|ArcGIS的矢量化操作——ArcGis中进行地形图的配准
- 用汇编的眼光看C++(之指针1)
- MySQL JOIN连接用法
- cad插件_CAD插件常青藤3.0
- 让子弹飞经典台词|让子弹飞经典语录
- 华为HCIP(HCNP)笔记,还不快快收藏!
- 用ES6语法存储美国邮政编码的IndexedDB数据库
- java jersey 搭建_Jersey搭建Rest web服务
- graphs菜单_spss菜单栏中英文对照.ppt
- 如何求子网掩码,默认网关地址,网络地址
- sklearn中shuffle的用法
- 洛谷P4218 [CTSC2010]珠宝商(后缀自动机+点分治)
- 实现浏览器多标签页通信
- 1月全球CTF比赛时间汇总来了!