qml C++插件plugin.qmltypes

先上圖:

在git上看到一個開源的工程,(git地址:https://github.com/machinekoder/QtKnobs) 下載下來後簡單的研究了下。

這個工程主要是用C++寫插件然後給qml調用。正好自己現在在學習這方面的知識,於是就記錄一下。

1.下載並編譯

下載後編譯會生成三個文件:(這裏生成的文件名被我改了)

2.生成 plugin.qmltypes文件

這一步,由於不太熟悉qmlplugindump,踩了不少坑。qmlplugindump 格式如下:

格式:qmlplugindump -nonrelocat Arg1 Arg2 Arg3 Arg4
Arg1 :module[插件]名稱
Arg2 :module[插件]版本 
Arg3 :插件所在的父路徑
Arg4 :xxx.qmltypes的生成路徑,一般是和qmldir一樣的路徑

我這裏生成的信息如下:

E:\QtPro\QML\QtKnobs\examples\testQtKnobs\QtKnobs>qmlplugindump -nonrelocatable  QtKnobs 1.0 E:\QtPro\QML\QtKnobs\examples\testQtKnobs > E:\QtPro\QML\QtKnobs\examples\testQtKnobs\QtKnobs\plugin.qmltypes
QCoreApplication::postEvent: Unexpected null receiver
QCoreApplication::postEvent: Unexpected null receiver
QCoreApplication::postEvent: Unexpected null receiver

生成文件內容如下:(截取了部分內容)

3.新建測試工程,測試源碼如下:

import QtQuick 2.4
import QtQuick.Window 2.2
import QtKnobs 1.0


Window {
    visible: true
    width: 800;
    height: 600;

    property int newVal : 0

    Timer {
        interval: 1000; running: true; repeat: true
        onTriggered: newVal = Math.floor((Math.random() * 100) + 0)
    }


    Rectangle {
        anchors.fill: parent;


        Column {
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.top: parent.top
            anchors.topMargin: 10
            spacing: 30
            Row {
                spacing: 5

                Knob {
                    width:  100
                    height: 100;
                    style: Knob.Pie
                    pieType: Knob.Flat
                    value: 75
                }

                Knob {
                    width:  100
                    height: 100;
                    style: Knob.Arc
                    value: 25
                    color: "#EF6555"
                }

                Knob {
                    width:  100
                    height: 100;
                    readOnly: true
                    style: Knob.Needle
                    needleType: Knob.Point
                    value: newVal
                    color: "#FEC56B"
                }

                Knob {
                    width:  100
                    height: 100;
                    readOnly: true
                    style: Knob.Pie
                    pieType: Knob.Curve
                    value: newVal
                    color: "#C55186"
                }

                Knob {
                    width:  100
                    height: 100;
                    style: Knob.Needle
                    needleType: Knob.Groove
                    value: 35
                }

                Knob {
                    width:  100
                    height: 100;
                    style: Knob.Needle
                    needleType: Knob.Round
                    value: 88
                    color: "#96040F"
                }
            }

            Row {
                spacing: 5

                Knob {
                    width:  100
                    height: 100;
                    style: Knob.Arc
                    //percent: 50
                    //mode: Knob.Percent
                    color: "#EF6555"
                }

                Knob {
                    width:  100
                    height: 100;
                    style: Knob.Pie
                    pieType: Knob.Curve
                    //percent: 90
                    //mode: Knob.Percent
                    color: "#C55186"
                }

                Knob {
                    width:  100
                    height: 100;
                    style: Knob.Arc
                    value: 2500
                    maximumValue: 5000
                }

                Knob {
                    width:  100
                    height: 100;
                    style: Knob.Pie
                    value: 8563
                    maximumValue: 10000
                    color: "#EF6555"
                }

                Item {
                    width: 200
                    height: 200
                    Knob {
                        width:  100
                        height: 100;
                        style: Knob.Pie
                        pieType: Knob.Curve
                        value: 135
                        maximumValue: 200
                        color: "#D7AAFB"
                    }
                }

                Knob {
                    width:  100
                    height: 100;
                    style: Knob.Pie
                    pieType: Knob.Curve
                    value: 25
                    maximumValue: 100
                    color: "#FF3300"
                    backgroundColor: "black"
                    foregroundColor: "#00FF99"
                    borderColor: "#003399"
                    textColor: "#8D18D7"
                }
            }
        }
    }



}

具體的坑我就懶得寫了,可以看我的筆記:

文檔:QML插件擴展2(基於C++的插件擴展).no...
鏈接:http://note.youdao.com/noteshare?id=294f86c78fb006f1b1b78cc430a20d74&sub=6027D5FEADC5411D834955387912CAFC

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章