1. 程式人生 > >QML自定義控制元件Button

QML自定義控制元件Button

QT自帶的Button無法滿足我們的審美,和需求,為此專案開發中經常需要自定義控制元件,以Button控制元件為例

聯絡作者 qq 843230304

效果圖(裡面的按鈕都是Button):還有好多呢,你只需要自定義自己的圖片,很美觀哦
這裡寫圖片描述這裡寫圖片描述

寫控制元件的時候,注意控制元件的屬性,不要在內部賦值或者修改,要丟擲訊號供外部處理修改(否則解綁了屬性)

使用舉例

                Button{
                    id:myBtn
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.left: parent
.left anchors.leftMargin: 30 pressedSource: 圖片按下的效果圖資源路徑 normalSource: 圖片正常的效果圖資源路徑 text:qsTr("按鈕名稱") pixelSize:字型畫素 textRightImage:true //文字在按鈕右側(內部屬性) rightMargin:50
//內部屬性 spacing:40//內部屬性 horizontalAlignment: Text.AlignRight verticalAlignment: Text.AlignVCenter onClicked: { // } }

附上QML原始碼

import QtQuick 2.0

Item {
    id:root

    property string
normalSource: "" property string pressedSource: "" property string disableSource: "" property bool selectEnable: false property bool selectAuto: true property bool selected: false property 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.width property alias textHeight: buttonText.height property alias text: buttonText.text property alias elide: buttonText.elide property alias horizontalAlignment: buttonText.horizontalAlignment property alias verticalAlignment: buttonText.verticalAlignment property int textHorizontalOffset: 0 property int textVerticalOffset: 0 property alias pixelSize: buttonText.font.pixelSize property int topMargin: 0 property int bottomMargin: 0 property int leftMargin: 0 property int rightMargin: 0 property int spacing: 0 signal clicked signal pressed(real xPosition,real yPosition) signal pressAndHold signal 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: image visible: true x:textLeftImage ? (leftMargin + buttonText.width + spacing): leftMargin y:textOverImage ? (topMargin + buttonText.height + spacing) : topMargin } Text { id: buttonText x: 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:image source: normalSource } PropertyChanges { target:buttonText; color:textNormalColor } when:(enabled)&&(!selectEnable || (selectEnable && !selected)) && !imageMouseArea.pressed }, State{ name:"pressed" PropertyChanges { target:image source: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: imageMouseArea anchors.fill: parent onPressed: { 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

QT自帶的Button無法滿足我們的審美,和需求,為此專案開發中經常需要自定義控制元件,以Button控制元件為例 聯絡作者 qq 843230304 效果圖(裡面的按鈕都是Button):還有好多呢,你只需要自定義自己的圖片,很美觀哦 寫控制元件的時

定義控制元件讓TextView、Button的drawableLeft和drawableRight與文字一起居中顯示

TextView的drawableLeft、drawableRight和drawableTop是一個常用、好用的屬性,可以在文字的上下左右放置一個圖片,而不使用更加複雜佈局就能達到,我也常常喜歡用Ra

定義控制元件讓TextView(Radiobutton)、Button的drawableLeft和drawableRight與文字一起居中顯示

TextView的drawableLeft、drawableRight和drawableTop是一個常用、好用的屬性,可以在文字的上下左右放置一個圖片,而不使用更加複雜佈局就能達到,我也常常喜歡用RadioButton的這幾個屬性實現很多效果,但是苦於不支援讓drawbl

WPF/Blend4之定義控制元件——製作自己的Button

先展示一下效果圖 先用Ellipse控制元件畫一個30*30的橢圓,找到畫筆屬性Fill,選擇下面的漸變畫筆,左下角選擇徑向漸變,起始顏色設成#FFFF0000,結束顏色設成#FFFFC8C8 右鍵點Ellipse選擇構成控制元件 選擇Button,確定,進入Butt

[WPF定義控制元件庫]以Button為例談談如何模仿Aero2主題

1. 為什麼選擇Aero2 除了以外觀為賣點的控制元件庫,WPF的控制元件庫都預設使用“素顏”的外觀,然後再提供一些主題包。這樣做的最大好處是可以和原生控制元件或其它控制元件庫相容,而且對於大部分人來說模仿原生的主題也比自己設計一套好看的UI容易得多。 WPF有以下幾種原生主題: 主題檔案 桌面主題

iOS定義控制元件-UISearchBar

   在開發過程中,UISearchBar屬不多見的控制元件,在我們一般使用的是系統原生樣式:    但是UI設計師可能想要的是這種:    可能你覺得很簡單:覺得設定背景顏色,邊框圖示什麼的;

定義控制元件之側滑關閉 Activity 控制元件

隔壁 iOS 的小夥伴有一個功能就是左手向右手一個慢動作,輕輕一劃就可以關閉介面,這種操作感覺還是很絲滑的,而且這還是 iOS 系統自帶的功能,由於 Android 手機早期是有 back 鍵,home 鍵 和選單鍵(現在大部分手機都只保留一個鍵了),所以 Android 是沒有這個功能的。現在

定義控制元件之下載控制元件1(DownloadView1)

前段時間在乾貨集中營看到了兩個炫酷的下載按鈕:       可惜是隔壁 iOS 的孩子,怎麼辦,我也好喜歡,emmm,某該,只能自己模仿著實現一下了。先從第一個入手(第二個波浪效果暫時還不會)。 1 準備動作 寫過幾次自定義控制元件

定義控制元件之 PasswordEditText(密碼輸入框)

前兩天在掘金上看到了一個驗證碼輸入框,然後自己實現了一下,以前都是繼承的 View,這次繼承了 ViewGroup,也算是嘗試了一點不同的東西。先看看最終效果: 事實上就是用將輸入的密碼用幾個文字框來顯示而已,要打造這樣一個東西我剛開始也是一頭霧水,不急,直接寫不會,我們可以採取曲線救

定義控制元件之 Gamepad (遊戲手柄)

這段時間自己在復刻一個小時候玩過的小遊戲——魔塔,在人物操控的時候剛開始用的感覺 low low 的上下左右四個方向鍵,後來受王者農藥啟發,決定採用現在很多遊戲中的那種遊戲手柄,網上也有例子,不過最近自己對自定義控制元件很感興趣,決定自己擼一個,最後實現的效果是這樣的: 看到這樣

定義控制元件之 SubmitBotton (提交按鈕)

在 Android 中我覺得除了實現很多功能性很強的需求之外,最吸引我的就是各種炫酷的自定義控制元件,但是自定義控制元件這個東西沒有辦法用一種固定的模式來講解,因為自定義控制元件都是根據需求來定製的。同時這也說明只要程式猿牛逼,就沒有實現不了的功能。 之前有看到一個效果: Android

Android中引入佈局和和定義控制元件

首先是引入佈局: 1.我們自己新建一個layout,就是一個標題欄。 2.然後在我們的mainactivity_layout中使用一個語句就可以實現。 <?xml version="1.0" encoding="utf-8"?> <LinearLayout

android-定義控制元件

自定義控制元件兩種方式 1、繼承ViewGroup 例如:ViewGroup , LinearLayout, FrameLayout, RelativeLayout等。 2、繼承View 例如:View, TextView, ImageView, Button等。 View的測量

duilib建立定義控制元件

我之前也寫過一片封裝xml為一個容器的文章,只是寫的很隨意,僅僅貼出了一個demo的地址。 在群裡還有一些剛剛接觸duilib的朋友們問到duilib自定義控制元件的問題,這裡我轉載一篇redrain大佬的博文。主要是這篇文章寫的太好了,我們直接參考理解就好,我寫的肯定沒這個好。原文地址:htt

duilib中將xml封裝為控制元件簡單示例(簡單定義控制元件,封裝幾個基本控制元件合為1個定義控制元件)

使用duilib的時候,難免會有這樣的需求: 某一塊Container(Layout)以及裡面的佈局需要重複用,不想每次都複製貼上這麼多,要不然xml太大了; 通過繼承來自定義一個控制元件,比如CButtonUIEx之類的,想讓他像button一樣在xml中被識別; xml裡面的東西

定義控制元件學習之繪製刻度盤

以前面試的時候面試官問過我會不會寫標尺工具,我沒做過呀,然後胡亂的說什麼畫布,ondraw繪製。。然後就沒有然後了--!,現在想想真的有點囧。所以今天我試了下自己畫刻度盤,不是很難,只有方法對了,輕輕鬆鬆。。大神勿噴,這是菜鳥的日常(高手退散退散。。巴拉巴拉能量**>_<**)

定義控制元件能實時檢視

自定義控制元件實時檢視 建立一個xib檔案關聯某個view 在view定義哪裡新增IB_DESIGNABLE IB_DESIGNABLE @interface xxView () @end 圖片不顯示是由於bundel問題,寫個UIImage的分類

Android 定義控制元件-星級評分

在學習自定義控制元件時需要一些例子來練練手,本文這個控制元件就是在這種環境下產生的(可能有BUG); 這個控制元件設計的特點: 1,可以任意修改星星數量 2,可以星星大小會隨控制元件大小而縮小,在控制元件足夠大的情況可以任意設定星星大小 3,滑動監聽,根據滑動距離選擇星級 4,可

[Android定義控制元件] Android定義控制元件

轉載自: http://blog.163.com/[email protected]/blog/static/103242241201382210910473/ 開發自定義控制元件的步驟: 1、瞭解View的工作原理  2、

定義控制元件之固定Tab

在開發中我們通常用到固定的Tab,Tab的個數是可以動態配置的,但是不支援滑動,每個Tab均分佈局並且之間被一個豎線分割開,Tab底部是一條分割線。看到如下效果如下,Tab佈局、線條顏色都支援高度制定。這個Tab的難點在於首先Tab個數不固定,其次Tab豎線左右兩端沒有隻有相鄰的兩個才有,而且粗