如何在处理过程中确定表中的活动列以便对其进行修改?

问题描述 投票:0回答:1

我不太明白如何命名我的列,以及如何更改它们的类型。例如,第一个是输出,第二个是浮点数,第三个是 QDataType。

但是,在第三个中,我想应用格式。我如何告诉代理它位于第三列,并且需要应用日期格式?

示例使用

TableModelColumn
,但不使用 QAbstractListModel 中的模型类:

TableView {
    id: statisticsTable
    model: WD_RX
    interactive: false

    delegate: Rectangle {
        id: delegate
        implicitWidth: 70
        implicitHeight: 20
        Text {
            text: display
            anchors.fill: parent
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
        }
    }
}
QVariant WeatherDataReceiver::data(const QModelIndex &index, int role) const
{
    if (role == Qt::DisplayRole) {
        if (index.column() == 0)
            return index.row();
        if (index.column() == 1)
            return QString::number(/*some float*/, 'f', 1);
        if (index.column() == 2)
            return QVariant(/*some QDataTime*/);
        
    }
    return QVariant(0);
}

我使用

Context
来注册类型。

main.cpp:

WeatherDataReceiver wdr;
engine.rootContext()->setContextProperty("WD_RX", &wdr);

我想在各列上签名并将

Qt.formatTime(/*some date*/, "hh:mm")
应用于第三列。

我唯一的解决方案是返回值类型的条件,但我认为这不是一个很好的解决方案,而且列的命名也存在问题:

TableView {
    id: statisticsTable
    model: WD_RX
    interactive: false

    delegate: Rectangle {
        id: delegate
        implicitWidth: 70
        implicitHeight: 20
        Text {
            text: {
                if (getType(display) !== 'object')
                    return display
                else
                    return Qt.formatTime(display, "hh:mm")
            }
            anchors.fill: parent
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
        }
    }
}
c++ qt model qt6
1个回答
0
投票

嗨,要将不同的前端应用于每个列,您必须使用DelegateChooser。然后您将能够指定您正在处理的列的索引。

为了给它们起一个标题,我将为您提供您需要实现的 headerData 函数的实现:

在你的模型中。h

    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;

在你的 model.cpp 中

QVariant ModelTransactions::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal)
{
    switch (section)
    {
    case 0:
        return "Client";

    case 1:
        return "Document";

    case 2:
        return "Référence client";

    case 3:
        return "Type transaction";

    case 4:
        return "Opérateur";

    case 5:
        return "Montant";

    case 6:
        return "Référence bénéficiaire";

    case 7:
        return "Date";

    case 8:
        return "Token";

    case 9:
        return "Caissier";

    case 10:
        return "idTransaction";

    default:
        return QVariant ();

    }
}
else if (orientation == Qt::Vertical)
    return section + 1;

return QVariant ();

}

在 if 部分,您可以为每一列命名;在 else 中,您可以为每一行指定一个数字,或者如果您觉得有用的话,可以指定一个名称

在 yout qml 文件中,您应该添加一个 HorizontalHederView (也称为 VerticialHeaderView)项目来自定义您的标题

HorizontalHeaderView
                    {
                        id: hHeaderView
                        syncView: tableView // give a id to your tableview and then paste it here
                        width: parent.width
                        height: parent.height / 10


                        delegate: Button
                        { // replace cellWidth and cellHeight by  value of your choice
                            implicitWidth: cellWidth
                            implicitHeight: cellHeight
                            width: cellWidth
                            height: cellHeight



                            background: Rectangle
                            {
                                anchors.fill: parent
                                color: Qt.rgba (0.0, 0.0, 1.0, 0.5)


                            }

                            contentItem: RowLayout
                            {
                                anchors.fill: parent
                                Text
                                {
                                    text: model.display
                                    color: "white"
                                    Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
                                 }
                            }

                           
                        }

                    }

希望我的回答有用。

© www.soinside.com 2019 - 2024. All rights reserved.