Header to a TableView

2019-08-19 22:07发布

I've been browsing everywhere and I just cannot find any information on how to create a certain type of header to a TableView in Qt Creator.

I want it to look similar to this:

TableView

1条回答
Explosion°爆炸
2楼-- · 2019-08-19 22:49

short answer: there is no settings in the QTCreator that you can set to define the Header of a table view...

long answer: That is a TableView with custom model. you need then to define a new Model that inherits the QAbstractTableModel

and then in tne FooModel header override the headerData method

class FooModel : public QAbstractTableModel
{
    Q_OBJECT

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

then in the in 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();
}

and finally in controller:

    myFooModel  = new FooModel(this);
    ui->myTableView->setModel(myFooModel);
查看更多
登录 后发表回答