QTcreator中 qml使用总结

最近在做人脸识别界面,用qml同时打开四个摄像头,纠结了半天,最后终于打开成功了。USB摄像头在外接线上,功率不够,要直接接到电脑上。

通过隐藏qml中,gridlayout的item来实现传几个摄像头,显示几个摄像头。

1,将区域变成可以拖动的

 //变成可拖动

        Drag.active: dragArea.drag.active

        MouseArea{

            id:dragArea

            anchors.fill: parent

            z:2

            drag.target:parent//限制拖动区域

        }

2,添加时间label并动态更新时间

 Label{

        id:timelabel

        text: qsTr("2018-00-00 00:00:00 星期一")

        z: 1

        color: "#d7e71a"

        font.pointSize: 20

        fontSizeMode: Text.HorizontalFit

 

        elide: Text.ElideMiddle

        font.bold: true

    }

    Timer{

        interval: 1000; running: true;repeat: true

        onTriggered: timelabel.text = Qt.formatDateTime(new Date(),

                                                     "yyyy-MM-dd  hh:mm:ss dddd")

    }

3,QML全屏,

mainWindow.visible= Window.FullScreen

qml隐藏系统最大化最小化按钮

 mainWindow.flags=Qt.FramelessWindowHint

4,自定义menuBar样式,menuBar无法隐藏,通过自定义menuBar隐藏

Import QtQuick.Controls.Styles 1.2

MenuItem中间加 MenuSeparator{}分割条

 menuBar:MenuBar{

        style: MenuBarStyle{

            font: Qt.font({

                          family:"微软雅黑",poinsSize:34,weight:Font.Normal

                          })

            background: Rectangle{

                    color:"blue"

                    width: 200

                    height: 100

            }

            menuStyle: MenuStyle{

                font:Qt.font({

                             family:"微软雅黑",poinsSize:34,weight:Font.Normal

                             })

            }

        }

5, c++调用qml方法,给qml传递数据

  C++文件方法


#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQuickWindow>
#include<QVariant>
#include<QDebug>
#include<QRect>
#include<QLabel>
#include<QQmlContext>
#include<QGuiApplication>


//定义全局变量

QVariant msgLabel1 = 0;
QVariant msgLabel2 = 0;
QObject *topLevel;
//声明方法

void updateNum(QVariant msgLabel1,QVariant msgLabel2);

int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine W;
W.load(QUrl(QStringLiteral("qrc:/test.qml")));
topLevel = W.rootObjects().value(0);
}

void updateNum(QVariant msgLabel1,QVariant msgLabel2)
{
    QMetaObject::invokeMethod(topLevel,"updateNumberFunction",Q_RETURN_ARG(QVariant, rValue),
                              Q_ARG(QVariant,msgLabel1),Q_ARG(QVariant,msgLabel2));

}


qml文件方法

 

function updateNumberFunction(msgLabel1,msgLabel2){

     console.log(msgLabel1);
       console.log(msgLabel2);


       

   }


有啥问题加我微信:

QTcreator中 qml使用总结