当前位置:   article > 正文

qt之qml嵌入qwidget

qml嵌入qwidget

QT加载qml文件

qt调用qml文件的方式还是有多种,但是我习惯运用QQmlApplicationEngine来加载qml文件,因为这个类还可以吧qt类注册到qml中调用,这样交互非常方便。

比如:
  • 1
  	   QQmlApplicationEngine 	m_engin.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
  • 1

这样就可以把qml文件加载出来

		QQmlApplicationEngine  m_engin.rootContext()->setContextProperty("QMLInteractObj", this);
  • 1

这个是把qt QObject类注册到qml中提供调用,这样在qml文件可以任意调用qt函数,qt函数需要调用需要添加Q_INVOKABLE ,比如qt函数:

public slots:	
	 Q_INVOKABLE void showname(QString name);
  • 1
  • 2

此函数用Q_INVOKABLE 了,在qml文件就可以直接调用,qml调用函数方式:

   MouseArea{
            onClicked: {
			QMLInteractObj.showname(showname.text)
		}
	}
  • 1
  • 2
  • 3
  • 4
  • 5

main.qml


import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4

Window {
    id:mainwindow
    visible: true
    width:  600 //Screen.width
    height: 480  //Screen.height+1   //嵌入防止闪屏  高度必须加1
    //flags: Qt.Window | Qt.FramelessWindowHint
    title: qsTr("This is qml Window")
    color: "#ffffff"
    objectName: "mainwindowobj"
    Rectangle{
        z:111
        width: parent.width/2
        height: parent.height
        color: "#999999"
        focus: true
        Text {
            font.family: "微软雅黑"
            font.pointSize: 12
            x:10
            y:10
            color: "white"
            text: qsTr("这是qml窗口")
        }

        TextField{
            id:showname
            width: 200
            height: 50
            anchors.centerIn: parent
            placeholderText:"请输入名字"
            font.family: "宋体"
            font.pointSize: 14
            textColor:"black"
            text:""
            //  validator: IntValidator {bottom: 1000; top: 99999;}
            //focus: true
            style: TextFieldStyle {
                placeholderTextColor:"black"
                background: Rectangle {
                    implicitWidth: 300
                    implicitHeight: 50
                    border.width: 1
                    border.color: "#ffffff"
                }
            }
        }

        Button{
            text: "发送"
            width: 50
            height: 50
            anchors.bottom: parent.bottom
            anchors.horizontalCenter: parent.horizontalCenter
            onClicked: {
                if(showname.text != "")
                {
                    QMLInteractObj.showname(showname.text)
                }
            }
        }
    }

    Component.onCompleted: {
        //初始化设置qwiget窗口大小
        QMLInteractObj.setwindowRect(mainwindow.width/2,0,mainwindow.width/2,mainwindow.height)
    }

    Connections{
        //接收c++类发送的信号处理  可传参数
        target: QMLInteractObj
        onPutqmlinfo:{
            showname.text=qtname
        }
    }

    Connections{
        target: mainwindow
        onClosing:
        {
            //可处理程序退出前要做的事情  可添加提示
            console.log("我要退出了!!!!")
        }
    }

    function showname(name){
        showname.text=name
    }
}
  • 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
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94

初始化以及嵌入qml代码:

   m_engin.rootContext()->setContextProperty("QMLInteractObj", this);
    m_engin.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
    if (m_engin.rootObjects().isEmpty())
    {
        return false;
    }

    QObject* obj = m_engin.rootObjects().at(0);
    basewindowobj.setWndBaseInfo(obj);
    basewindowobj.setWndSplitType();

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

项目效果图:
在这里插入图片描述
在这里插入图片描述
此项目对初学者是个不错的选择,可加QQ交流1174158241

完整项目下载地址:
下载地址

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/831243
推荐阅读
相关标签
  

闽ICP备14008679号