Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTableView SecondLine In Header



  • Hi all,

    We have a QTableView which can use different kinds of models. So that we cannot change the model headers. For example we cannot do something like bleow since it modifies the data inside the QAbstractItemModel

    p_model->setHeaderData(col, Qt::Horizontal, header_name2);

    For example there are some headers in the model like "Duration [s]", "Length [mm]" or "Length (mm)"

    If the header includes a measurement between (), [], or "" we want to show it in next line like below. It is easy to do it with setHeaderData, but we cannot use it. Can you help us in this issue? Is it possible to do it with QStyledItemDelegate or something else?

    We want a solution which gives the result below in the header of the QTableView
    images.png

    Thank you, in advance.


  • Qt Champions 2019

    Simply add a linebreak - \n



  • @DzCode said in QTableView SecondLine In Header:

    So that we cannot change the model headers. For example we cannot do something like bleow since it modifies the data inside the QAbstractItemModel
    p_model->setHeaderData(col, Qt::Horizontal, header_name2);

    I don't think QStyledItemDelegate is involved in displaying header text.

    QTableView get its header text from model()->headerData(). I understand there are various external models beyond your control. Instead of calling QAbstractItemModel::setHeaderData(), are you prepared to interpose a QIdentityProxyModel on your view's connection to its model? Then you can override the proxy's headerData(), to call the base model and deal with text alteration?



  • @JonB said in QTableView SecondLine In Header:

    QIdentityProxyModel

    do you have any example about this issue. Can I cast QAbstractItemModel to QIdentityModel and then should I use stHeaderData() of QIdentityModel?



  • @DzCode
    No you cannot cast. You have to create a proxy model and interpose it between view and actual model. So instead of having view -> actual model you have to have view -> proxy model -> actual model.

    Where you currently have something like:

    view->setModel(model);
    

    you will need something like:

    proxyModel = new MyIdentityProxyModel;
    proxyModel->setSourceModel(model);
    view->setModel(proxyModel);
    

    First verify that works precisely as now, so the view still shows the original model data.

    Then in your MyIdentityProxyModel sublass derived from QIdentityProxyModel, you can start overriding methods. For your case, MyIdentityProxyModel::headerData() (https://doc.qt.io/qt-5/qidentityproxymodel.html#headerData) can retrieve the header data from the original model and insert \n to break the text.



  • @JonB said in QTableView SecondLine In Header:

    @DzCode
    No you cannot cast. You have to create a proxy model and interpose it between view and actual model. So instead of having view -> actual model you have to have view -> proxy model -> actual model.

    Where you currently have something like:

    view->setModel(model);
    

    you will need something like:

    proxyModel = new MyIdentityProxyModel;
    proxyModel->setSourceModel(model);
    view->setModel(proxyModel);
    

    First verify that works precisely as now, so the view still shows the original model data.

    Then in your MyIdentityProxyModel sublass derived from QIdentityProxyModel, you can start overriding methods. For your case, MyIdentityProxyModel::headerData() (https://doc.qt.io/qt-5/qidentityproxymodel.html#headerData) can retrieve the header data from the original model and insert \n to break the text.

    I did it witthout subclassing, but I used QIdentityProxyModel inside a method of my custom tableview. It works. Thank you.


Log in to reply