all articles in this series can be viewed here http://blog.csdn.net/cloud_castle/article/category/2123873
Answer the QT5 official demo set 32--QT Quick examples-threading
To our second example of QT Quick examples, the main reason for choosing this demo is that when we use the QT development interface (especially the cross-platform interface), Local screen information and window calls are unavoidable issues.
This example shows us how to get local screen information in QML.
The project tree, where SHARED.QRC is some of the resources that many QML examples share, including buttons, sliders, and so on.
In addition, for a good cross-platform, the demo adds two formats of icon files (Mac-supported Icns, and PNG for other platforms).
Program runs, we can call a subwindow to display in windowed or maximized form. Below are some of the information on my laptop screen, including resolution, pixel density, direction, and so on.
Due to the use of icons, it is necessary to take the pro file out to see:
Window.pro:
TEMPLATE = appqt + = Quick Qmlsources + = main.cppresources + = window.qrc . /shared/shared.qrcexample_files = Window.qmltarget.path = $$[qt_install_examples]/quick/windowinstalls + = Targeticon = resources/icon64.png //Set icon icon Macx:icon = Resources/icon.icns //Mac platform additional definition icon Win32:rc_file = Res Ources/window.rc
Main.cpp:
#include <QtGui/QGuiApplication> //Personal comparison recommended header file included in # include <QtQml/QQmlEngine> #include <qtqml/ qqmlcomponent> #include <QtQuick/QQuickWindow> #include <QtCore/QUrl> #include <qdebug>int main ( int argc, char* argv[]) { qguiapplication app (argc, argv); Qqmlengine engine; Notice that after 5.3 we mostly use Qqmlapplicationengine to load the qml file //It actually encapsulates the Qqmlengine + qqmlcomponent here, and provides some other handy functions Qqmlcomponent component (&engine); Qquickwindow::setdefaultalphabuffer (true); If we need to apply a transparent form, we need to set the function to True Component.loadurl (Qurl ("QRC:///WINDOW/WINDOW.QML") before the first qquickwindow appears; if (Component.isready ()) component.create (); else qwarning () << component.errorstring (); return app.exec ();}
We look at the first QML file in the order of loading
WINDOW.QML:
Import QtQuick 2.0import Qtquick.window 2.1import ". /shared "as Sharedqtobject {///non-visual lightweight top-level object property real Defaultspacing:10 The object Interval property Systempalette Palette:systempalette {}//Systempalette makes it easy to provide localized control styles Prope Rty var Controlwindow:window {//main window Width:visibilityLabel.implicitWidth * 1.2 height:col. Implicitheight + defaultspacing * 2 Color:palette.window title: "Control window" Column { Id:col anchors.fill:parent anchors.margins:defaultSpacing spacing:defaultspacing Property real CELLWIDTH:COL.WIDTH/3-spacing//Grid cell width text {text: "Control the second win Dow: "} Grid {Id:grid columns:3 spacing:defaultspacing Width:parent.width Shared.button {//b from the Shared control packageUtton Id:showbutton width:col.cellWidth Text:testWindow.visibl E? "Hide": "Show" onClicked:testWindow.visible =!testwindow.visible}//! [Windowedcheckbox] Shared.checkbox {//CheckBox text: "Windowed" height:showbutton.h Eight width:col.cellWidth Binding on checked {value:testWindow.visibility = = = Win Dow. Windowed}//Note that the binding is a QML type, not a keyword This statement equals binding{target:checked; value:testWindow.visibility = = = Window.windowed} This method is more suitable for property bindings of VAR types than for Checked:xxx Therefore, for the bool type of checked, checked:testWindow.visibility = = = Window.windowed Also possible onClicked:testWindow.visibility = window.windowed}//! [Windowedcheckbox] Shared.checkbox {height:showButton.height Width:col.cellWidth Text: ' Full Screen ' Binding on checked {value:testWindow.visibility = = = Window.fullscreen} onClicked:testWindow.visibility = Window.fullscreen} shared.button { Window.automaticvisibility adjusts the display according to the platform, for example, Windows, Android is full screen Id:autobutton Width:col.cellWidth text: "Automatic" onClicked:testWindow.visibility = Win Dow. Automaticvisibility} shared.checkbox {Height:autoButton.height Text: "Minimized" Binding on checked {value:testWindow.visibility = = = Window.minimized } onClicked:testWindow.visibility = window.minimized} shared.checkbox {height:autoButton.height text: "Maximized" Binding on checked {value:testWindow.visibility = = = window.maximized} onClicked:testWindow.visibility = Window.maximiz Ed}} function Visibilitytostring (v) {//State conversion string function Swit CH (v) {case Window.Windowed:return "windowed"; Case Window.Minimized:return "minimized"; Case Window.Maximized:return "maximized"; Case Window.FullScreen:return "fullscreen"; Case Window.AutomaticVisibility:return "Automatic"; Case Window.Hidden:return "Hidden"; } return "Unknown"; } text {Id:visibilitylabel text: "Second window is" + (testwindow.visible? ") Visible ":" invisible ") +" and has visibility "+ parent.visibilitytostring (testwindow.visibility) } Rectangle {Id:horizontalrule color: "Black" width:p Arent.width height:1} screeninfo {}//screen information obtained, implementation code below}} !controlwindow Property var Testwindow:window {//Sub-window width:320 height:24 0 Color: "#215400"//window background color, but we see the color only around a circle, because the middle is rectangle, that lap for the interval defaultspacing title: "Test Wi Ndow with Color "+ Color Flags:Qt.Window | Qt.windowfullscreenbuttonhint Rectangle {anchors.fill:parent anchors.margins:defaultSpacin G//Rectangle with window interval Text {anchors.centerin: Parent text: "Second Window"} mousearea {//Click to toggle Color Anchors.fill:parent OnClicked:testWindow.color = "#e0c31e"} Shared . Button {anchors.right:parent.right anchors.top:parent.top anchors.margins: defaultspacing text:testWindow.visibility = = = Window.fullscreen? "Exit fullscreen": "Go Fullscreen" width:150 onclicked: {if (Testwindo w.visibility = = = Window.fullscreen) testwindow.visibility = window.automaticvisibility else testwindow.visibility = Window.fullscreen}} Shar Ed. Button {anchors.left:parent.left Anchors.top:parent.top anchors.margins:d Efaultspacing text: "X" width: OnClicked:testWindow.visible = false//window-Hidden}}}//!te Stwindow Property var Splashwindow:splash {//startup Window OnTimeout:controlWindow.visible = True Custom signal processing function, display main window}}
In order to get the screen information in QML, QT provides us with the type of screens, which can be an additional object for other visual qml types, pointing to the device that the object displays:
SCREENINFO.QML:
Import QtQuick 2.1import qtquick.window 2.1Item {id:root width:400 height:propertyGrid.implicitHeight + 16 function Orientationtostring (o) {//state to String function switch (o) {case qt.primaryorientation: return "PRIMARY"; Case Qt.PortraitOrientation:return "Portrait"; Case Qt.LandscapeOrientation:return "Landscape"; Case Qt.InvertedPortraitOrientation:return "Inverted portrait"; Case Qt.InvertedLandscapeOrientation:return "inverted landscape"; } return "Unknown"; } Grid {Id:propertygrid columns:2 spacing:8 x:spacing y:spacing//! [Screen] Text {text: "screen \" "+ Screen.name +" \ ":"//Display device name Font.bold:true} Item { Width:1; HEIGHT:1}//device name to the right of the placeholder text {text: "Dimensions"}//Resolution TEXT { Text:Screen.width + "X" + screen.height} text {text: "Pixel density"}//Pixel density T ext {text:Screen.pixelDensity.toFixed (2) + "dots/mm (" + (screen.pixeldensity * 25.4). toFixed (2) + "Dots/inch"} Text {text: "Logical pixel Density"}//logical pixel density text {text:Screen.logicalPixelDensity.toFixed (2) + "dots/mm (" + (screen.logicalpixeldensity * 25.4). toFixed (2) + "Dots/inch"} text {text: "Available virtual D Esktop "}//Available virtual desktops, 30 less than the resolution height is because the status bar under Windows Text {text:Screen.desktopAvailableWidth +" X "+ SCREEN.D Esktopavailableheight} text {text: "orientation"}//Screen orientation Text {Text:orientationt Ostring (screen.orientation) + "(" + screen.orientation + ")"} text {text: "Primary Orientation"} Priority direction Text {text:orientationtostring (screen.primaryorientation) + "(" + screen.primaryorientation + ")"} //! [Screen]}}
Finally, the implementation of the Start screen
SPLASH.QML:
Import QtQuick 2.0import Qtquick.window 2.1//! [Splash-properties] window {Id:splash color: "Transparent" title: "Splash Window" modality:Qt.ApplicationModal//application window Port mode Flags:Qt.SplashScreen//Start Screen property int timeoutinterval:2000 signal timeout Custom Overflow Signal//! [splash-properties]//! [Screen-properties] x: (screen.width-splashimage.width)/2//Center Y: (screen.height-splashimage.height)/ 2//! [Screen-properties] width:splashImage.width//window size consistent with picture size height:splashImage.height image { Id:splashimage Source: ". /shared/images/qt-logo.png "Mousearea {anchors.fill:parent onClicked:Qt.quit ()/ /Click to exit}}//! [Timer] Timer {interval:timeoutinterval; running:true; Repeat:false ontriggered: {visible = False Hidden after 2 seconds, and a timeout signal is emitted to show the main window Splash.timeout () } } //! [Timer] Component.onCompleted:visible = true}
QT5 Official Demo parsing set 33--QT Quick Examples-window and screen