33

如何使 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)"
        }
    }
}

服务器项.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 }
    }
}
4

3 回答 3

17

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

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)";
             }
        }
    }
 }

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 }
    }
}

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

于 2013-06-26T07:44:30.210 回答
11

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

import QtQuick 1.0

Column {
    id: serverItem

    BorderImage {
        anchors.fill: parent
    }

    //... the rest
}
于 2011-04-27T19:04:08.410 回答
2

您必须明确设置大小,以便行/列中的两个 QML 元素不会重叠。

于 2011-11-24T17:44:53.733 回答