c++ - 字体更改时 QML Listview 显示统一宽度

标签 c++ qml qt5

我有一个简单的 ListView 。我需要用户能够操纵字体大小(视觉障碍问题)。 QML 非常高兴地为 listView 项目计算新的宽度和高度,但是由于字符串的长度不同导致 listView 看起来像堆叠不当的盒子。我需要的是让它看起来像最长字符串宽度的矩形,如果它到达窗口边缘则换行。我想我使 listView 项目背景透明并计算 listView 所在的矩形的宽度以适合更新的字体大小。我已经尝试了几种方法来做到这一点,但都没有成功。

有什么线索吗?下面的代码(数据来自 C++)

import QtQuick 2.0

Rectangle
{
id: theMenu
property double fontSize: menuManager.menuFontPointSize
property double menuWidth: menuManager.menuItemHeight
Component
{
    id: menuEntryDelegate

    Rectangle
    {
        id: menuItemContainer
        width: menuEntry.width
        height: menuEntry.height * 1.25
        anchors.top: prompts.bottom
        property double fontSize: theMenu.fontSize

        state: ListView.isCurrentItem ? "selected" : "notselected"

        Text
        {
            id: menuEntry
            font.pointSize: fontSize
            //width: parent.width
            wrapMode: Text.WordWrap
            text: displayText
            clip: true
        }

        MouseArea
        {
            hoverEnabled: false
            anchors.fill: parent
            onClicked: menuHolder.currentIndex = index
            onDoubleClicked: menuManager.displayMenu(menuHolder.currentIndex)
        }

        states:
        [
            State
            {
                name: "selected"
                PropertyChanges
                {
                    target: menuItemContainer
                    color: "#FAFCD9"
                }
                PropertyChanges
                {
                    target: icon
                    source: iconUrl
                }
                PropertyChanges
                {
                    target: prompts
                    text: getPrompt()
                }
                PropertyChanges
                {
                    target: menuEntry
                    color: "black"
                }
            },

            State
            {
                name: "notselected"
                PropertyChanges
                {
                    target: menuItemContainer
                    color: "black"
                }
                PropertyChanges
                {
                    target: menuEntry
                    color: "white"
                }
            },
            State
            {
                name: "hidden"
                PropertyChanges
                {
                    target: menuItemContainer
                    color: "green"
                }
            }

        ]

    }
}

Rectangle
{
    id: menuContainer
    width: menuManager.menuWidth
    height: (50 * 9) //TBD
    anchors.horizontalCenter: parent.horizontalCenter
    anchors.top: prompts.bottom
    color: "purple"
    ListView
    {
        id: menuHolder
        model: menuModel
        anchors.fill: parent
        opacity: 1

       /* header: Rectangle
        {
            TextBox {}
    }*/
        header: Rectangle
        {
            width: menuHolder.width
            height: 50
            color: "#2A51A3"


           Text
            {
               id: header
               anchors.centerIn: parent

               text: "FIX" + currentMenu.displayText
               font.pointSize: currentMenu.fontPointSize
               color: "green"
               width: parent.width
               wrapMode: Text.WordWrap
            }
        }

        delegate: menuEntryDelegate
        focus: true

        add: Transition
        {
            NumberAnimation { properties: "x,y" }
        }

        Keys.onPressed:
        {
            if(event.key === Qt.Key_F1)
            {
                theMenu.z = -1
            }
            else if(event.key === Qt.Key_F3)
            {
                theMenu.z = 1
            }
            else if(event.key === Qt.Key_F2)
            {
                menuManager.menuFontPointSize *= menuManager.scale
                theMenu.fontSize = menuManager.menuFontPointSize

            }
            else if(event.key === Qt.Key_F10)
            {
                scaleFactor -= 0.1
                menuContainer.scale = scaleFactor
                promptsContainer.scale = scaleFactor
                //promptsContainer.z = 1
            }
            else if(event.key === Qt.Key_Right)//zoom in
            {
                menuContainer.x +=10
            }
            else if(event.key === Qt.Key_Left)//zoom out
            {
                menuContainer.x -=10
            }
            else if(event.key === Qt.Key_Home)//go back to Main menu
            {
                menuManager.displayMainMenu();
                theMenu.fontSize = menuManager.menuFontPointSize
            }
            //Ways to select a menu item
            else if((event.key >= Qt.Key_1 && event.key <= Qt.Key_9)
                    || event.key === Qt.Key_Return || event.key === Qt.Key_Enter)
            {
                if(event.key >= Qt.Key_1 && event.key <= Qt.Key_9)
                {
                    menuHolder.currentIndex = event.key  - Qt.Key_1;
                }
                menuManager.displayMenu(menuHolder.currentIndex);
                theMenu.fontSize = menuManager.menuFontPointSize
            }
            menuEntryDelegate.updateIcon()
        }
    }
}

}@

最佳答案

只需使用:

width: parent.width;

在委托(delegate)组件的 menuItemContainer 矩形元素中,填充 ListView 宽度。

然后添加:

anchors { left: parent.left; right: parent.right }

menuEntry 文本元素一个最大尺寸,这样它就知道什么时候必须换行(否则它会无限向右延伸)。

关于c++ - 字体更改时 QML Listview 显示统一宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15050917/

相关文章:

java - 在 'for' 循环中递增 1 时格式化背后的技术原因?

c++ - 获取通用/前向引用地址有什么意义?

c++ - 取消对结构的 int 指针地址的引用

javascript - Qt QML 如何格式化(突出显示)文本

qt - 在 QML 中将 TabButton 动态添加到 TabBar

c++ - "Attempting to reference a deleted function"添加 QMutex 到类后

c++ - 如何同时在 Visual Studio 2015 和 Visual Studio 2002 中构建 C++ 项目

c++ - 如何从 QML 更改 C++ 模型?

c++ - 将滚动条与 QGridLayout 一起使用

c++ - 如何将我的 Qt 应用程序主窗口始终放在 Windows 任务栏上方?