且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在TableView的一列中显示多个角色

更新时间:2022-12-12 11:25:44

我认为您可以使用 delegate 组件.

I think you could join both values using the delegate component.

就你而言:

TableView {
    id: table
    ...

    TableViewColumn {           
        width: 575
        delegate: Text { text: model.name + " "  + model.surname }
    }       
    TableViewColumn {
        role: "phone"
        width: 575
    }
    TableViewColumn {
        role: "ip_address"
        width: 525
    }
    model: abonents
}

这里有另一个示例,仅用于测试您是否想使用它.该示例基于此 Qt 示例.

Here you have another example, just for testing if you want to work with it. The example is based on this Qt example.

ma​​in.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

ma​​in.qml

import QtQuick 2.2
import QtQuick.Window 2.2
import QtQuick.Controls 1.4

ApplicationWindow {
    id: window
    visible: true
    title: "Table View Example"

    TableView {
        TableViewColumn {
            role: "title"
            title: "Title"
            width: 100
        }
        TableViewColumn {
            role: "author"
            title: "Author"
            width: 100
        }

        TableViewColumn{
            width: 300
            delegate: Text { text: model.title + " "  + model.author }
        }

        TableViewColumn{
            width: 300
            delegate: Text {
                text: model.title + " "  + model.author
                font.family: "Courier New"
                font.pixelSize: 18
                color: "red"
            }
        }

        model: libraryModel

        ListModel {
            id: libraryModel
            ListElement {
                title: "A Masterpiece"
                author: "Gabriel"
            }
            ListElement {
                title: "Brilliance"
                author: "Jens"
            }
            ListElement {
                title: "Outstanding"
                author: "Frederik"
            }
        }
    }

}