0

我一直在到处浏览,只是找不到任何关于如何在 Qt Creator中为TableView创建某种类型的标题的信息。

我希望它看起来像这样:

表视图

4

1 回答 1

2

简短的回答:QTCreator 中没有可以设置来定义表格视图的标题的设置...

长答案:那是一个带有自定义模型的 TableView。然后您需要定义一个继承 QAbstractTableModel 的新模型

然后在 FooModel 标头中覆盖 headerData 方法

class FooModel : public QAbstractTableModel
{
    Q_OBJECT

    //...
    QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
    //... more methods may be here

然后在cpp中:

QVariant FooModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::DisplayRole)
    {
        switch (section)
        {
        case 0:
            return QString("Name");
        case 1:
            return QString("ID");
        case 2:
            return QString("HexID");
        // etc etc    
        }

    }
    return QVariant();
}

最后在控制器中:

    myFooModel  = new FooModel(this);
    ui->myTableView->setModel(myFooModel);
于 2019-07-15T11:58:25.787 回答