Qml文件的两种加载方式|启动Qt quick app的两种方法

来源:互联网 发布:insert mysql自增 编辑:程序博客网 时间:2024/06/07 03:29

一种是QQmlApplicationEngine搭配Window,例如:

[cpp] view plain copy
  1. #include <QGuiApplication>  
  2. #include <QQmlApplicationEngine>  
  3.   
  4. int main(int argc, char *argv[])  
  5. {  
  6.     QGuiApplication app(argc, argv);  
  7.   
  8.     QQmlApplicationEngine engine;  
  9.     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));  
  10.     return app.exec();  
  11. }  

相应的qml文件是这样的:

[cpp] view plain copy
  1. import QtQuick 2.3  
  2. import QtQuick.Window 2.2  
  3. import QtQuick.Controls 1.4  
  4. //实现对鼠标事件的处理和对文本的移动处理(键盘事件)  
  5. Window {  
  6.     id:main;  
  7.     visible: true;  
  8.     MouseArea {  
  9.         acceptedButtons: Qt.LeftButton|Qt.RightButton;//确定接收哪些事件  
  10.         anchors.fill: parent;  
  11.         onClicked: {  
  12.             if(mouse.button==Qt.LeftButton)//鼠标事件  
  13.             {  
  14.                 text.text="Leftbutton clicked";  
  15.             }  
  16.             else if(mouse.button==Qt.RightButton)  
  17.             {  
  18.                 text.text="Rightbutton clicked";  
  19.             }  
  20.         }  
  21.     }  
  22.     Text {          
  23.         id:text;  
  24.         focus: true;  
  25.         x:50;  
  26.         y:50;  
  27.         anchors.bottom: t.bottom;  
  28.         Keys.enabled: true;//设置键盘可用  
  29.         Keys.onPressed:  
  30.         {  
  31.             switch(event.key){//对键盘事件进行处理  
  32.             case Qt.Key_Left:  
  33.                 x-=10;  
  34.                 event.accepted=true;//对接受到事件处理,避免再次向上传递  
  35.                 break;  
  36.             case Qt.Key_Right:  
  37.                 x+=10;  
  38.                 event.accepted=true;  
  39.                 break;  
  40.             case Qt.Key_Up:  
  41.                 y-=10;//因为原点在窗口的左上角  
  42.                 event.accepted=true;  
  43.                 break;  
  44.             case Qt.Key_Down:  
  45.                 y+=10;  
  46.                 event.accepted=true;  
  47.                 break;  
  48.             default:return  
  49.             }  
  50.         }  
  51.         color: "blue";  
  52.         text: "hello world";  
  53.         font.bold: true;  
  54.         font.pointSize: 16;  
  55.         styleColor: "#f51515";  
  56.         verticalAlignment: Text.AlignVCenter;  
  57.         horizontalAlignment: Text.AlignHCenter;  
  58.         ColorAnimation on color {  
  59.             to: "black";  
  60.             duration: 2000;  
  61.         }  
  62.     }  

还有一种是QQuickViuew搭配Item。

当然这里所说的item就无需多说了,item是其他盒子模型的老祖……

因此在qml文件中,有window这个item的地方,你都要替换成Rectangle

例如:
[cpp] view plain copy
  1. #include <QGuiApplication>  
  2. #include <QQmlApplicationEngine>  
  3. #include <QQuickView>  
  4.   
  5. int main(int argc, char *argv[])  
  6. {  
  7.     QGuiApplication app(argc, argv);     
  8.     QQuickView view;  
  9.     view.setResizeMode(QQuickView::SizeRootObjectToView);  
  10.     view.setSource(QUrl("qrc:/main.qml"));  
  11.     view.show();  
  12.     return app.exec();  
  13. }  

相应的qml文件是这个样子的:

[cpp] view plain copy
  1. import QtQuick 2.3  
  2. import QtQuick.Window 2.2  
  3. import QtQuick.Controls 1.4  
  4. //实现对鼠标事件的处理和对文本的移动处理(键盘事件)  
  5. Rectangle {  
  6.     id:main;  
  7.     visible: true;  
  8.     MouseArea {  
  9.         acceptedButtons: Qt.LeftButton|Qt.RightButton;//确定接收哪些事件  
  10.         anchors.fill: parent;  
  11.         onClicked: {  
  12.             if(mouse.button==Qt.LeftButton)//鼠标事件  
  13.             {  
  14.                 text.text="Leftbutton clicked";  
  15.             }  
  16.             else if(mouse.button==Qt.RightButton)  
  17.             {  
  18.                 text.text="Rightbutton clicked";  
  19.             }  
  20.         }  
  21.     }  
  22.     Text {  
  23.         id:text;  
  24.         focus: true;  
  25.         x:50;  
  26.         y:50;  
  27.         anchors.bottom: t.bottom;  
  28.         Keys.enabled: true;//设置键盘可用  
  29.         Keys.onPressed:  
  30.         {  
  31.             switch(event.key){//对键盘事件进行处理  
  32.             case Qt.Key_Left:  
  33.                 x-=10;  
  34.                 event.accepted=true;//对接受到事件处理,避免再次向上传递  
  35.                 break;  
  36.             case Qt.Key_Right:  
  37.                 x+=10;  
  38.                 event.accepted=true;  
  39.                 break;  
  40.             case Qt.Key_Up:  
  41.                 y-=10;//因为原点在窗口的左上角  
  42.                 event.accepted=true;  
  43.                 break;  
  44.             case Qt.Key_Down:  
  45.                 y+=10;  
  46.                 event.accepted=true;  
  47.                 break;  
  48.             default:return  
  49.             }  
  50.         }  
  51.         color: "blue";  
  52.         text: "hello world";  
  53.         font.bold: true;  
  54.         font.pointSize: 16;  
  55.         styleColor: "#f51515";  
  56.         verticalAlignment: Text.AlignVCenter;  
  57.         horizontalAlignment: Text.AlignHCenter;  
  58.         ColorAnimation on color {  
  59.             to: "black";  
  60.             duration: 2000;  
  61.         }  
  62.     }  
  63. }  

对比后发现,就是Window换成了Rectangle,如果你不更换,就会变成空白。


转载于点击打开链接 

原创粉丝点击