In questa pagina

Termostato

L'esempio " Termostato " illustra come implementare diversi design a seconda delle dimensioni della finestra.

Un'interfaccia utente per un termostato domestico, che mostra come creare applicazioni responsive in Qt Quick in grado di adattarsi sia ai grandi schermi dei computer desktop che ai dispositivi mobili e ai piccoli schermi integrati.

Tema UI chiaro su schermo verticale di dispositivo mobile Tema UI chiaro su schermo orizzontale di dispositivi mobili

Tema UI chiaro sulla schermata del desktop

Tema UI scuro su schermo verticale di dispositivi mobili Tema scuro dell'interfaccia utente su schermo verticale di dispositivi mobili

Tema scuro dell'interfaccia utente sullo schermo del desktop

Esecuzione dell'esempio

È possibile eseguire l'esempio da:

Design reattivo

L'applicazione supporta una varietà di dimensioni di schermo. È in grado di ridimensionarsi dinamicamente quando l'utente modifica le dimensioni della finestra oppure, sui dispositivi mobili, l'applicazione seleziona le dimensioni corrette in base allo schermo disponibile. Constants.qml contiene proprietà che specificano le dimensioni dello schermo e controllano quale layout è attualmente in uso:

    property bool isBigDesktopLayout: layout === Constants.Layout.Desktop
    property bool isSmallDesktopLayout: layout === Constants.Layout.SmallDesktop
    property bool isMobileLayout: layout === Constants.Layout.Mobile
    property bool isSmallLayout: layout === Constants.Layout.Small

In ` App.qml`, le proprietà vengono associate all’altezza e alla larghezza della finestra all’avvio dell’applicazione:

    Component.onCompleted: {
        Constants.layout = Qt.binding(() => {
            let tall = window.height >= window.width
            if (window.width >= 1440 && window.height >= 520)
                return Constants.Layout.Desktop
            if (window.width >= 1024 && window.height >= 768)
                return Constants.Layout.SmallDesktop
            if (tall || (window.width >= 600 && window.height >= 380))
                return Constants.Layout.Mobile
            return Constants.Layout.Small
        })
    }

Gli stati vengono quindi utilizzati per controllare le proprietà dei componenti, quali larghezza, altezza, fontSize, posizione e layout (colonna o riga).

    states: [
        State {
            name: "bigDesktopLayout"
            when: Constants.isBigDesktopLayout
            PropertyChanges {
                target: statistics
                leftPadding: 53
                rightPadding: 53
                topPadding: 23
                bottomPadding: 43
            }
            PropertyChanges {
                target: scrollView
                isBackgroundVisible: false
                delegateWidth: 350
                delegateHeight: 182
                statisticsChartWidth: grid.width
                statisticsChartHeight: 647
            }
            PropertyChanges {
                target: grid
                width: 1100
            }
        },
        State {
            name: "smallDesktopLayout"
            when:  Constants.isSmallDesktopLayout
            PropertyChanges {
                target: statistics
                leftPadding: 53
                rightPadding: 53
                topPadding: 23
                bottomPadding: 43
            }
            PropertyChanges {
                target: scrollView
                isBackgroundVisible: false
                delegateWidth: 290
                delegateHeight: 182
                statisticsChartWidth: grid.width
                statisticsChartHeight: 541
            }
            PropertyChanges {
                target: grid
                width: 918
            }
        },
        State {
            name: "mobileLayout"
            when: Constants.isMobileLayout
            PropertyChanges {
                target: statistics
                leftPadding: 0
                rightPadding: 0
                topPadding: 0
                bottomPadding: 0
            }
            PropertyChanges {
                target: scrollView
                isBackgroundVisible: false
                delegateWidth: 327
                delegateHeight: 110
                statisticsChartWidth: 346
                statisticsChartHeight: 383
            }
        },
        State {
            name: "smallLayout"
            when: Constants.isSmallLayout
            PropertyChanges {
                target: statistics
                leftPadding: 0
                rightPadding: 0
                topPadding: 0
                bottomPadding: 0
            }
            PropertyChanges {
                target: scrollView
                isBackgroundVisible: true
                delegateWidth: 332
                delegateHeight: 90
                statisticsChartWidth: 420
                statisticsChartHeight: 240
            }
        }
    ]

File sorgente

Progetto di esempio su code.qt.io

Vedi anche Tutti gli esempi Qt e Esempi e tutorial diQt Quick .

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.