程序运行截图如下:
程序结构如下:
代码如下:
main.cpp
#include <QApplication> #include <QDir> #include <QQuickView> #include <QUrl> #include <QQmlEngine> int main(int argc, char *argv[]) { QApplication a(argc, argv); QQuickView viewer; viewer.setSource(QUrl("qrc:/main.qml")); viewer.setResizeMode(QQuickView::SizeRootObjectToView); //默认的,不写也阔以 viewer.show(); return a.exec(); }
main.qml
import QtQuick 2.0 Item { width:800; //这个;可以有,也可以没有,不敲;我心理面不舒服 height:600; QtObject{ id:myObject; property int counter; Component.onCompleted: { myObject.counter=100; } } Rectangle{ id:topInformation; anchors.top: parent; width:parent.width; height:80; opacity:0.6 color:"black"; Text{ id:myTopText; color:"white"; anchors.centerIn: parent; text:"这是一个显示消息长方体,透明度为0.6!"; font.pixelSize: 22; } MouseArea{ anchors.fill: parent; onPressed:{ myTopText.text="现在:鼠标左键按下了!点击的是矩形框!"; } onReleased: { myTopText.text="现在:鼠标左键释放了!点击的是矩形框!"; } } } Rectangle{ id:centerInformation; anchors.centerIn: parent; width:parent.width; height:80; opacity:0.7 color:"gray"; Text{ id:myCenterText; color:"white"; anchors.centerIn: parent; font.pixelSize: 22; text:myObject.counter; } MouseArea{ anchors.fill: parent; onClicked: { countDown.start(); } } } Timer{ id:countDown; interval:20; repeat:true; triggeredOnStart: true; onTriggered: { myCenterText.text=myObject.counter; myObject.counter-=1; if(myObject.counter<0){ countDown.stop(); myCenterText.text="已经为0了!"; } } } }