QtQuick Column Layout

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts


ApplicationWindow {

    visible: true
    width: 200
    height:300
    title: "Template App"

    ColumnLayout {
        
        anchors.fill: parent
            
        Button {
        
            objectName: "button1"
        
            text: "Button 1"

            Layout.fillWidth: true
            Layout.fillHeight: true
        }

        Button {
           
            objectName: "button2"
        
            text: "Button 2"

            Layout.fillWidth: true
            Layout.fillHeight: true
        }

        Button {
        
            objectName: "button3"
        
            text: "Button 3"

            Layout.fillWidth: true
            Layout.fillHeight: true
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sys

from PySide6.QtCore import QObject, Slot
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine


class Logger(QObject):
    
    @Slot(str)
    def log(self, message):
        print(message)


if __name__ == '__main__':

    app = QGuiApplication(sys.argv)
    
    engine = QQmlApplicationEngine()
    engine.quit.connect(app.quit)
    engine.load('01_columnlayout.qml')
    
    root = engine.rootObjects()[0]
    
    button1 = root.findChild(QObject, 'button1') 
    button2 = root.findChild(QObject, 'button2')
    button3 = root.findChild(QObject, 'button3')
    
    logger = Logger()
    
    button1.clicked.connect(lambda: logger.log('Button 1 clicked'))
    button2.clicked.connect(lambda: logger.log('Button 2 clicked'))
    button3.clicked.connect(lambda: logger.log('Button 3 clicked'))

    result = app.exec()
    del engine
    
    sys.exit(result)