Specifying font for many Text-elements in Qt QML

后端 未结 3 1113
遥遥无期
遥遥无期 2020-12-18 23:16

I have a widget specified through a QML file. This widget contains a top levelRectangle which contains two Columns. Each of these Columns

相关标签:
3条回答
  • 2020-12-18 23:38

    One possible solution is to write a function, that iterates over the children of a passed element (for example a Column). In this function all the properties can be set:

    import QtQuick 1.0
    
        Rectangle {
        Row {
            spacing: 10
    
            Column {
                id: col1
    
                Text {
                    property bool useStyle: true
                    text: "Foo1"
                }
                Text {
                    property bool useStyle: true
                    text: "Bar1"
                }
                Text {
                    property bool useStyle: true
                    text: "Baz1"
                }
            }
    
            Column {
                id: col2
    
                Text {
                    property bool useStyle: true
                    text: "Foo2"
                }
                Text {
                    text: "not styled"
                }
                Text {
                    property bool useStyle: true
                    text: "Baz2"
                }
            }
        }
    
        function setTextStyle(parentElement) {
            for (var i = 0; i < parentElement.children.length; ++i) {
                console.log("t", typeof parentElement.children[i]);
                if (parentElement.children[i].useStyle) {  // apply style?
                    parentElement.children[i].color = "blue";
                    parentElement.children[i].font.family = "Arial"
                    parentElement.children[i].font.bold = true;
                    parentElement.children[i].font.italic = true;
                    parentElement.children[i].font.pixelSize = 12;
                }
            }
        }
    
        // set style
        Component.onCompleted: {
            setTextStyle(col1);
            setTextStyle(col2);
        }
    }
    

    Each element, that contains the property useStyle that is set to true, gets styled. This is shorter, than assigning the style manually, but you can still define which elements should get styled or not.

    0 讨论(0)
  • 2020-12-18 23:53

    Another possibility is to write a new QML component, that inherits from Text an sets some properties by default:

    StyledText.qml

    import QtQuick 1.0
    
    Text {
        // set default values
        color: "blue"
        font.family: "Arial"
        font.bold: true
        font.italic: true
        font.pixelSize: 12
    }
    

    main.qml

    import QtQuick 1.0
    
    Rectangle {
        Row {
            spacing: 10
    
            Column {
                StyledText {
                    text: "Foo1"
                }
                StyledText {
                    text: "Bar1"
                }
                StyledText {
                    text: "Baz1"
                }
            }
    
            Column {
                StyledText {
                    text: "Foo2"
                }
                StyledText {
                    text: "Bar2"
                }
                StyledText {
                    text: "Baz2"
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-18 23:53

    In Qt 5.6 (at least, probably earlier too), you can use Qt.font() to dynamically allocate a font object and refer to it elsewhere. So, this works:

    property font myFont: Qt.font({
        family: fontfamily,
        bold: fontbold,
        italic: fontitalic,
        pixelSize: fontpixelsize
    });
    
    Text
    {   
        color: fontcolor
        font: parent.myFont
    }
    

    More info on Qt.font() here: https://doc-snapshots.qt.io/qt5-5.6/qml-qtqml-qt.html#font-method

    0 讨论(0)
提交回复
热议问题