Header to a TableView

▼魔方 西西 提交于 2019-12-13 03:30:33

问题


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:


回答1:


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);


来源:https://stackoverflow.com/questions/57038810/header-to-a-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!