先上图:
在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