qml C++插件plugin.qmltypes

本文分享了一款在Qt上使用的开源插件QtKnobs的使用经验,详细介绍了从下载到编译的过程,并通过实例展示了如何在QML中调用C++编写的插件,实现各种样式和功能的旋钮组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先上图:

在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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Liu-Eleven

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值