c++qtqtableviewqheaderview

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:

TableView


Solution

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