Unsolved 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
Thank you, in advance.
-
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 frommodel()->headerData()
. I understand there are various external models beyond your control. Instead of callingQAbstractItemModel::setHeaderData()
, are you prepared to interpose aQIdentityProxyModel
on your view's connection to its model? Then you can override the proxy'sheaderData()
, 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 fromQIdentityProxyModel
, 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 fromQIdentityProxyModel
, 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.