1. 程式人生 > >QML,QtQuick2.0以上常用控制元件

QML,QtQuick2.0以上常用控制元件

QML Type

本篇主要介紹QtQuick Controls 2,Qt Creator 5.10

1.Container

import QtQuick 2.10
import QtQuick.Window 2.10
import QtQuick.Controls 2.3

ApplicationWindow{
    visible: true;
    width: 1280;
    height: 720;
    Row {
        anchors.fill: parent;
        TabBar {
            id: tabBar

            currentIndex: 0
width: parent.width - addButton.width - btnDelete.width TabButton { text: "TabButton" } } Component { id: tabButton TabButton { text: "TabButton" } } Button { id: addButton text: "+" flat: true onClicked: { tabBar.addItem
(tabButton.createObject(tabBar)) console.log("added:", tabBar.itemAt(tabBar.count - 1)) } } Button { id: btnDelete text: "-" flat: true onClicked: { tabBar.removeItem(tabBar.itemAt(tabBar.count
-1)); } } } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

2.DelayButton

import QtQuick 2.10
import QtQuick.Window 2.10
import QtQuick.Controls 2.3

ApplicationWindow{
    visible: true;
    width: 1280;
    height: 720;

    Label{
        id: lbl;
        text: "未點選";
        font.bold: true;
        font.pixelSize: 28;
        anchors.centerIn: parent;
    }

    DelayButton{
        id: delayBtn;
        text: "PressAndHold";

        onActivated: {
            lbl.text = "已點選";
        }
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

3.Dial

import QtQuick 2.10
import QtQuick.Window 2.10
import QtQuick.Controls 2.3

ApplicationWindow{
    visible: true;
    width: 1280;
    height: 720;

    Label{
        id: lbl;
        text: "0";
        font.bold: true;
        font.pixelSize: 28;
        anchors.centerIn: parent;
    }

    Dial {
        id: dial
        //Keys.onLeftPressed: {}
        snapMode: Dial.SnapAlways;
        stepSize: 0.1;
        onMoved: {
            lbl.text = value;
        }
    }

    Button{
        id: btnIncrease
        text: "增加"
        anchors.left: dial.right;
        anchors.leftMargin: 40;
        anchors.bottom: dial.bottom;

        onClicked: {
            dial.increase();
        }
    }

    Button{
        id: btnDecrease
        text: "減少"
        anchors.left: btnIncrease.right;
        anchors.leftMargin: 40;
        anchors.bottom: btnIncrease.bottom;

        onClicked: {
            dial.decrease();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

4.DialogButtonBox

import QtQuick 2.10
import QtQuick.Window 2.10
import QtQuick.Controls 2.3

ApplicationWindow{
    visible: true;
    width: 1280;
    height: 720;

    Row{
        DialogButtonBox {
            standardButtons: DialogButtonBox.Ok | DialogButtonBox.Cancel

            onAccepted: console.log("Ok clicked")
            onRejected: console.log("Cancel clicked")
        }

        DialogButtonBox {
            Button {
                text: qsTr("Save")
                DialogButtonBox.buttonRole: DialogButtonBox.AcceptRole
            }
            Button {
                text: qsTr("Close")
                DialogButtonBox.buttonRole: DialogButtonBox.DestructiveRole
            }
        }
    }




}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

5.Dialog

import QtQuick 2.10
import QtQuick.Window 2.10
import QtQuick.Controls 2.3

ApplicationWindow{
    visible: true;
    width: 1280;
    height: 720;

    Dialog {
        id: dialog
        x:(parent.width-width)/2
        y:(parent.height-height)/2
        implicitWidth: 500;
        implicitHeight: 300;
        title: "Title"
        modal: true;
        standardButtons: Dialog.Ok | Dialog.Cancel

        onAccepted: console.log("Ok clicked")
        onRejected: console.log("Cancel clicked")
    }

    Button{
        text: "open";
        onClicked: {
            dialog.open();
        }
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

6.Drawer

import QtQuick 2.10
import QtQuick.Window 2.10
import QtQuick.Controls 2.3

ApplicationWindow{
    visible: true;
    width: 1280;
    height: 720;

    Drawer {
        id: drawer
        width: 0.4 * parent.width
        height: parent.height
        dragMargin: parent.width * 0.1; //拉動開始生效的區域,最低為0,也就是0的位置拖動才有效
    }

    Label {
        id: content

        text: "Aa"
        font.pixelSize: 96
        anchors.fill: parent
        verticalAlignment: Label.AlignVCenter
        horizontalAlignment: Label.AlignHCenter

        transform: Translate {
            x: drawer.position * content.width * 0.33
        }
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

7.Menu

import QtQuick 2.10
import QtQuick.Window 2.10
import QtQuick.Controls 2.3

ApplicationWindow{
    visible: true;
    width: 1280;
    height: 720;

    Button {
        id: fileButton
        text: "File"
        onClicked: menu.open()

        Menu {
            id: menu
            y: fileButton.height
            Action { text: "Cut" }
            Action { text: "Copy" }
            Action { text: "Paste" }

            MenuSeparator { }

            Menu {
                title: "Find/Replace"
                Action { text: "Find Next" }
                Action { text: "Find Previous" }
                Action { text: "Replace" }
            }
        }
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

8.MenuBar

import QtQuick 2.10
import QtQuick.Controls.Material 2.3
import QtQuick.Window 2.10
import QtQuick.Controls 2.3


ApplicationWindow{
    visible: true;
    width:1280;
    height:720;

//    Material.theme: Material.Light
//    Material.accent: Material.Purple

    menuBar: MenuBar {
        Menu {
            title: qsTr("&File")
            Action { text: qsTr("&New...")}
            Action { text:qsTr("&Open...")}
            Action { text:qsTr("&Save")}
            Action { text:qsTr("Save &As...")}
            MenuSeparator { }
            Action { text:qsTr("&Quit")}
        }
        Menu {
            title:qsTr("&Edit")
            Action { text: qsTr("Cu&t")}
            Action { text:qsTr("&Copy")}
            Action { text:qsTr("&Paste")}
        }
        Menu {
            title:qsTr("&Help")
            Action { text: qsTr("&About")}
        }
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

9.Overlay

import QtQuick 2.10
import QtQuick.Controls.Material 2.3
import QtQuick.Window 2.10
import QtQuick.Controls 2.3


ApplicationWindow{
    visible: true;
    width: 1280;
    height: 720;

    Button {
        anchors.left: parent.left;
        anchors.leftMargin: 20;
        text: "Popup"
        onClicked: popup.open()

        Popup {
            id: popup

            parent: Overlay.overlay

            x: (parent.width - width) / 2
            y: (parent.height - height) / 2
            width: 500
            height: 300
        }
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

10.PageIndicator

import QtQuick 2.10
import QtQuick.Controls.Material 2.3
import QtQuick.Window 2.10
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3


ApplicationWindow{
    visible: true;
    width: 1280;
    height: 720;

    SwipeView {
        id: view
        currentIndex: pageIndicator.currentIndex
        anchors.fill: parent

        Page {
            title: qsTr("Home")
            Label{
               anchors.centerIn: parent
               text: "Home";
               font.bold: true;
               font.pixelSize: 28;
            }
        }
        Page {
            title: qsTr("Discover")
            Label{
               anchors.centerIn: parent
               text: "Discover";
               font.bold: true;
               font.pixelSize: 28;
            }
        }
        Page {
            title: qsTr("Activity")
            Label{
               anchors.centerIn: parent
               text: "Activity";
               font.bold: true;
               font.pixelSize: 28;
            }
        }
    }

    PageIndicator {
        id: pageIndicator
        interactive: true
        count: view.count
        currentIndex: view.currentIndex

        anchors.bottom: parent.bottom
        anchors.horizontalCenter: parent.horizontalCenter
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

11.RangeSlider

import QtQuick 2.10
import QtQuick.Controls.Material 2.3
import QtQuick.Window 2.10
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3


ApplicationWindow{
    visible: true;
    width: 1280;
    height: 720;

    RangeSlider {
        id: rangeSlider
        from: 1
        to: 100
        first.value: 25
        second.value: 75
        first.onValueChanged:{

        }
    }


    Label{
        id: lbl;
        text: rangeSlider.first.value;
        anchors.centerIn: parent;
    }

    Label{
        text: rangeSlider.second.value;
        anchors.centerIn: parent;
        anchors.verticalCenterOffset: 30;
    }
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

12.ScrollView

import QtQuick 2.10
import QtQuick.Controls.Material 2.3
import QtQuick.Window 2.10
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3


ApplicationWindow{
    visible: true;
    width: 1280;
    height: 720;

    ScrollView {
        width: 200
        height: 200
        clip: true

        Label {
            text: "ABC"
            font.pixelSize: 224
        }
    }

    ScrollView {
        width: 200
        height: 200

        anchors.centerIn: parent;
        ListView {
            model: 20
            delegate: ItemDelegate {
                text: "Item " + index
            }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

13.SpinBox

import QtQuick 2.10
import QtQuick.Controls.Material 2.3
import QtQuick.Window 2.10
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3


ApplicationWindow{
    visible: true;
    width: 1280;
    height: 720;

    SpinBox {
        id: spinbox
        from: 0
        value: 110
        to: 100 * 100
        stepSize: 100
        anchors.centerIn: parent

        property int decimals: 2
        property real realValue: value / 100

        //DoubleValidator 為隨機數生成的,QIntValidator
        validator: DoubleValidator {
            bottom: Math.min(spinbox.from, spinbox.to)
            top:  Math.max(spinbox.from, spinbox.to)
        }

        textFromValue: function(value, locale) {
            return Number(value / 100).toLocaleString(locale, 'f', spinbox.decimals)
        }

        valueFromText: function(text, locale) {
            return Number.fromLocaleString(locale, text) * 100
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

14.StackView

import QtQuick 2.10
import QtQuick.Controls.Material 2.3
import QtQuick.Window 2.10
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3


ApplicationWindow{
    visible: true;
    width: 1280;
    height: 720;

    StackView {
        id: stack
        initialItem: mainView
        anchors.fill: parent
    }

    Component {
        id: mainView

        Row {
            spacing: 10

            Button {
                text: "Push"
                onClicked: stack.push(mainView)
            }
            Button {
                text: "Pop"
                enabled: stack.depth > 1
                onClicked: stack.pop()

            }
            Text {
                text: stack.depth
            }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

15.SwipeView

import QtQuick 
            
           

相關推薦

QMLQtQuick2.0以上常用控制元件

QML Type 本篇主要介紹QtQuick Controls 2,Qt Creator 5.10 1.Container import QtQuick 2.10 import QtQuick.Window 2.10 import QtQuick.Contr

android 5.0以上版本控制元件點選 水波紋效果

android api21及以上引入了控制元件點選的水波紋效果 1.可以直接給控制元件設定 android:background="?android:attr/selectableItemBackground"(有邊界效果) 或者  android:backgro

Android常用控制元件用執行緒寫一個進度條。

一、事件監聽(三種寫法) 1、標籤上直接繫結監聽方法       public void xxx(View view) 2、 建立監聽器物件,元件再繫結監聽器物件 2.1、匿名內部類 2.2、使用匿名內部類並定義成全域性的屬性 二、文字框(TextView) 1、T

Telerik 常用控制元件屬性記錄一下

1.radTreeView 文字顯示多顏色 RadTreeNode childNode1 = new RadTreeNode(); childNode1.Text = @"<html>" + string.Format(@"<span style=""fo

CefSharp.v49.0.1瀏覽器控制元件完全WPF版實現禁止彈出新視窗在同一視窗開啟連結並且支援帶type="POST" target="_blank"的連結

    需求場景:在查詢頁面,填寫查詢條件,查詢條件包括上傳的圖片,根據圖片的特徵查詢,這就需要在提交的時候,使用POST提交,因為GET提交無法提交圖片資料,提交查詢條件之後,在新的視窗展示查詢結果。(當然查詢結果頁面可能不支援F5重新整理頁面) 表單HTML程式碼示意(注意metho

常用控制元件01 TextView與EditText Android簡單計算器

事件監聽(三種寫法) 1.1 標籤上直接繫結監聽方法 public void xxx(View view) 1.2 建立監聽器物件,元件再繫結監聽器物件 1.2.1 匿名內部類 1.2.2 使用匿名內部類並定義成全域性的屬性 1 2. 文字框(TextVie

Asp.net 2.0自定義控制元件(點選HyperLink後執行事件)[網友問題: DataList裡HyperLink控制元件激發事件在哪定義?]

 (一). 概述         HyperLink預設沒有Click事件,  重寫了一個HyperLink自定義控制元件.         實現原理:          預設Hyperlink是跳到點選請求的頁面, 本HyperLink自定義控制元件最終也是跳轉到請求的頁面, 但期間        執行

常用控制元件含義屬性繼承關係

由於繼承關係,所以子類傳承父類非私有的屬性,並有特有屬性。二級父類屬性簡介: TextView作用: 在介面上顯示文字,沒有編輯功能,屬性:         text 控制元件中要顯示內容         textColor  內容文字顏色         textSiz

安卓常用控制元件RecyclerView+HorizontalScrollView實現item側滑效果 安卓常用控制元件RecyclerView+HorizontalScrollView實現item側滑效果

原 安卓常用控制元件RecyclerView+HorizontalScrollView實現item側滑效果 2017年10月28日 12:23:14 低-調

常用控制元件——圖片框與進度條

訊息提示框 1. Toast(吐絲框) 1.1 Toast是Android中的一種簡易的訊息提示框 1.2 使用這個類的最簡單的方法是呼叫靜態方法構造您所需要的一切,並返回一個新的Toast物件。 Toast toast=Toast.makeText(getApplicationCo

Android 開發:(三)安卓常用控制元件以及仿《微門戶》登入介面實現

一、常用控制元件: 1、文字類控制元件 TextView 負責展示文字,非編輯 EditText 可編輯文字控制元件 2、按鈕類控制元件 Button 按鈕 ImageButton 圖片按鈕 RadioButton與RadioGroup 單

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

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

C語言Windows程式開發—Windows視窗樣式與常用控制元件樣式【第04天】

(一)Windows視窗(MDICLIENT)樣式介紹 1 /* Windows視窗樣式 */ 2 WS_BORDER //帶有邊框的視窗 3 WS_CAPTION //帶有標題欄的視窗 4 WS_CHILD

《第一行程式碼Android》學習總結第三章 常用控制元件使用方法

1、TextView match_parent:表示讓父佈局決定當前控制元件大小,當前控制元件大小與父佈局大小一樣。 wrap_content:表示讓空間內容決定當前控制元件大小讓當前控制元件大小能夠剛好包含住控制元件內的內容。         對控

NX二次開發-Block UI各種常用控制元件的獲取(持續補充)

在Block UI中UF_initialize();和UF_terminate();的使用 用Block UI作NX二次開發的時候,不需要在使用UFUN函式的時候加UF_initialize();和UF_terminate();。 可以直接加在CPP裡這個位置: extern "C"

VC++6.0 MFC列表控制元件的基本使用方法

1、新增ListCtrl控制元件  2、屬性中View(樣式)設定為Report(報告) 3、類嚮導為列表控制元件關聯變數 4、初始化列表: 在列表控制元件所在的對話方塊類裡的OnInitDialog()函式中初始化表頭: //列表初始化 m_L

c#窗體學習——常用控制元件介紹(一)

偷懶,轉自若雲流風,原文:https://blog.csdn.net/ruoyunliufeng/article/details/72874691  一.常用控制元件 Lable標籤→僅顯示文字; TextBox文字控制元件→文字框; Button按鈕控制元件

[Xcode10 實際操作]四、常用控制元件-(1)UIButton控制元件的使用

本文將演示按鈕控制元件的使用,按鈕是使用者介面中最常見的互動控制元件 在專案導航區,開啟檢視控制器的程式碼檔案【ViewController.swift】 1 import UIKit 2 3 class ViewController: UIViewController { 4

[Xcode10 實際操作]四、常用控制元件-(4)UILabel文字標籤的自動換行

本文將演示標籤控制元件的換行功能, 在專案導航區,開啟檢視控制器的程式碼檔案【ViewController.swift】 1 import UIKit 2 3 class ViewController: UIViewController { 4 5 override f

[Xcode10 實際操作]四、常用控制元件-(3)UILabel文字標籤的使用

本文將演示標籤控制元件的基礎用法, 在專案導航區,開啟檢視控制器的程式碼檔案【ViewController.swift】 1 import UIKit 2 3 class ViewController: UIViewController { 4 5 override f