简体   繁体   中英

Header to a TableView

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:

表格检视

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM