Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableView SecondLine In Header
Forum Updated to NodeBB v4.3 + New Features

QTableView SecondLine In Header

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 549 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DzCode
    wrote on 18 Jun 2020, 11:03 last edited by DzCode
    #1

    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.

    J 1 Reply Last reply 18 Jun 2020, 18:35
    3
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 18 Jun 2020, 17:33 last edited by
      #2

      Simply add a linebreak - \n

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • D DzCode
        18 Jun 2020, 11:03

        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.

        J Offline
        J Offline
        JonB
        wrote on 18 Jun 2020, 18:35 last edited by JonB
        #3

        @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?

        D 1 Reply Last reply 19 Jun 2020, 11:55
        3
        • J JonB
          18 Jun 2020, 18:35

          @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?

          D Offline
          D Offline
          DzCode
          wrote on 19 Jun 2020, 11:55 last edited by
          #4

          @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?

          J 1 Reply Last reply 19 Jun 2020, 12:04
          0
          • D DzCode
            19 Jun 2020, 11:55

            @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?

            J Offline
            J Offline
            JonB
            wrote on 19 Jun 2020, 12:04 last edited by JonB
            #5

            @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.

            D 1 Reply Last reply 19 Jun 2020, 15:23
            2
            • J JonB
              19 Jun 2020, 12:04

              @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.

              D Offline
              D Offline
              DzCode
              wrote on 19 Jun 2020, 15:23 last edited by
              #6

              @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.

              1 Reply Last reply
              0

              1/6

              18 Jun 2020, 11:03

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved