簡體   English   中英

QML TableView自定義委托:如何對太長的文本使用省略號

[英]QML TableView Custom delegate: How to make ellipsis for too long text

我想在QML TreeView顯示QFileSystemModel ,並且需要一個自定義委托,因為我想在文件旁邊添加一個復選框。 這就是我所擁有的:

TreeView {
    id: view
    anchors.fill: parent
    sortIndicatorVisible: true
    model: fileSystemModel
    rootIndex: rootPathIndex
    selection: sel
    selectionMode: 2


    TableViewColumn {
        id: namecolumn
        title: "Name"
        role: "fileName"

        resizable: true
        width: parent.width-sizeWidth-dateWidth-scrollBarWidth
        delegate: fileCheckDelegate
    }


    Component {
        id: fileCheckDelegate
        Row{CheckBox{}
            Text{text: root.getText(model.fileName)}
         }
    }

但是,我在長文件名越過列邊界時遇到了問題。 默認委托將截斷文本,並在截斷的文本上添加省略號。 我想在我的自定義委托中做同樣的事情,但是不知道該怎么做。

如您所見,我嘗試使用自定義的getText函數,但是我不知道在其中使用哪個寬度和位置來確定是否應截斷文本

編輯:我發現在Text組件上設置Text.ElideRight可以省略,但是我需要設置一個顯式寬度。 那我該如何設置Text組件的寬度呢?

好的,這就是竅門:

    TreeView {
        id: view
        anchors.fill: parent
        sortIndicatorVisible: true
        model: fileSystemModel
        rootIndex: rootPathIndex
        selection: sel
        selectionMode: 2


        TableViewColumn {
            id: namecolumn
            title: "Name"
            role: "fileName"

            resizable: true
            width: parent.width-sizeWidth-dateWidth-scrollBarWidth
            delegate: fileCheckDelegate
        }


        Component {
            id: fileCheckDelegate
            Row{CheckBox{
                id: cbox
                }
                Text{text: model.fileName
                    width: namecolumn.width-x-cbox.width
                    elide: Text.ElideRight
                }
             }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM