如何使QML项目增长以适应内容?

lam*_*fun 27 qt qml

如何使ServerItem增长以适应内容?现在ServerItems只是相互重叠.

main.qml

import Qt 4.7
import "Teeworlds" as Teeworlds

Item {
    Column {
        Teeworlds.ServerItem {
            serverName: "InstaGib, lost [xyz]"
        }

        Teeworlds.ServerItem {
            serverName: "Arena.sbor (rus)"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

ServerItem.qml

import QtQuick 1.0

BorderImage {
    id: serverItem

    property string serverName: "unnamed server"
    property string gameType: "DM"
    property int numPlayers: 0
    property int maxPlayers: 8
    property int ping: 60

    Text {
        id: title
        text: parent.serverName
    }

    Grid {
        id: grid
        anchors.top: title.bottom
        columns: 2
        rows: 3
        Text { text: "Gametype: " }  Text { text: gameType }
        Text { text: "Players: " }   Text { text: numPlayers + "/" + maxPlayers }
        Text { text: "Ping: " }      Text { text: ping }
    }
}
Run Code Online (Sandbox Code Playgroud)

The*_*roo 12

您必须为容器指定大小,并在子级中添加锚点/绑定:

main.qml:

import QtQuick 1.1
import "Teeworlds" as Teeworlds

Item {
    width: 800; // root item so give a size
    height: 600;

    Flickable {
         clip: true;
         anchors.fill: parent; // use a flickable to allow scrolling
         contentWidth: width; // flickable content width is its own width, scroll only vertically
         contentHeight: layout.height; // content height is the height of the layout of items

         Column {
             id: layout;
             anchors { // the column should have a real size so make it fill the parent horizontally
                 left: parent.left;
                 right: parent.right;
             }

             Teeworlds.ServerItem {
                serverName: "InstaGib, lost [xyz]";
             }
             Teeworlds.ServerItem {
                serverName: "Arena.sbor (rus)";
             }
        }
    }
 }
Run Code Online (Sandbox Code Playgroud)

Teeworlds/ServerItem.qml:

import QtQuick 1.1

BorderImage {
    id: serverItem;
    height: grid.y + grid.height; // compute the item height using content position and size
    anchors { // to have a real size, items should grow horizontally in their parent
        left: parent.left;
        right: parent.right;
    }

    property string serverName  : "unnamed server";
    property string gameType    : "DM";
    property int      numPlayers  : 0;
    property int      maxPlayers   : 8;
    property int      ping               : 60;

    Text {
        id: title;
        text: parent.serverName;
    }
    Grid {
        id: grid;
        columns: 2;
        anchors { // the grid must anchor under the title but horizontally in the parent too
            top: title.bottom;
            left: parent.left;
            right: parent.right;
        }

        Text { text: "Gametype: " } 
        Text { text: gameType }
        Text { text: "Players: " }  
        Text { text: numPlayers + "/" + maxPlayers }
        Text { text: "Ping: " }    
        Text { text: ping }
    }
}
Run Code Online (Sandbox Code Playgroud)

请记住,默认情况下,所有Item,Rectangle,BorderImage都没有大小,没有位置,并且Column,Row,Flow,Grid,Text和Image将自己调整为其内容,因此如果要使用Column调整其子项的大小,则必须确保其子项之一不再自动定义列大小.因为在其他地方,孩子们将保持0x0,列也将是0x0.列应该锚定到其父级中以具有实际大小,并且其子级应该水平(左和右)锚定到列中以具有实际宽度,并且对于高度项目必须根据其内部布局来自我调整大小...


bla*_*raz 9

实际上这很容易.ServerItem对象没有大小,您只能看到内容,因为没有剪辑.解决方案是在ServerItem类(或main.qml中的实例)中设置高度和宽度,或者使用增长元素(例如Column)作为ServerItem根元素.

import QtQuick 1.0

Column {
    id: serverItem

    BorderImage {
        anchors.fill: parent
    }

    //... the rest
}
Run Code Online (Sandbox Code Playgroud)

  • 文档似乎说,锚被列禁止:`注意定位器假定其子项的x和y位置不会改变.如果在脚本中手动更改x或y属性,绑定x或y属性,在定位器的子项上使用锚点,或者让子项的高度取决于子项的位置,则定位器可能会出现奇怪的行为.如果您需要执行任何这些操作,请考虑在不使用列的情况下定位项目.或者我读错了.. (4认同)