Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Show column titles in HorizontalHeaderView
Forum Updated to NodeBB v4.3 + New Features

Show column titles in HorizontalHeaderView

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 1.4k 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.
  • F Offline
    F Offline
    Futster
    wrote on last edited by
    #1

    I am trying to create a simple QML Table view, based on a static model. While I have that working, I can't get my column titles to display. What displays now is "1", "2", "3" etc. But I want to show the column titles as defined in my model.

    I would like to use HorizontalHeaderView (since that is the NEW way to use titles). Can someone point out how to fix my code below?

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Controls 2.15
    import Qt.labs.qmlmodels 1.0
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
        
        TableModel {
            id: myModel
            TableModelColumn { display: "companyName" }
            TableModelColumn { display: "info1" }
            TableModelColumn { display: "info2" }
            rows: [
                {
                    companyName: "company 1",
                    info1: "company 1 info 1",
                    info2: "company 1 info 2"
                },
                {
                    companyName: "company 2",
                    info1: "company 2 info 1",
                    info2: "company 2 info 2"
                },
                {
                    companyName: "company 3",
                    info1: "company 3 info 1",
                    info2: "company 3 info 2"
                }
            ]
        }
        
        TableView {
            id: myTableView
            anchors.fill: parent
            clip: true
            
            model: myModel
            
            delegate: Rectangle {
                implicitWidth: 100
                implicitHeight: 50
                border.width: 1
                Text {
                    text: display
                    anchors.centerIn: parent
                }
            }
        }
        
        HorizontalHeaderView {
            id: horizontalHeader
            syncView: myTableView
            model: myModel
            //        model: [ "A","B","C"]
            delegate: Rectangle {
                implicitWidth: 100
                implicitHeight: 50
                Text {
                    text: display
                    anchors.centerIn: parent
                }
            }
        }
    }
    
    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by fcarney
      #2

      I don't see a way to add this to TableModel. I think the best you can do is just create a model that mirrors your columns, or create a QAbtractTableModel.

      HorizontalHeaderView {
              id: horizontalHeader
              syncView: myTableView
              model: ["Company Name", "Info 1", "Info 2"]
              delegate: Rectangle {
                  implicitWidth: 100
                  implicitHeight: 50
                  Text {
                      text: modelData  // notice non abstract models assume data appears in modelData
                      anchors.centerIn: parent
                  }
              }
          }
      

      Edit:
      If you go the QAbstractTableModel route you have to define the headerData() function.

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Futster
        wrote on last edited by
        #3

        I have implemented a QAbstractTableModel with a headerData function, but the header row is still messed up. Could you elaborate on your answer, or provide a complete example? I assume that my horizontalHeaderView model points to the same model as the view.

        1 Reply Last reply
        0
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by fcarney
          #4

          The docs don't show it specifying a model. Not sure what is supposed to happen with headerData. I don't know how to build the delegate for a qabstracttablemodel. I just read the docs and figure things out.

          Edit: This might help. https://stackoverflow.com/questions/63719365/horizontalheaderview-not-calling-headerdata-of-qabstracttablemodels-child

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          0

          • Login

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