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. QML HorizontalHeaderView not working as expected
Forum Updated to NodeBB v4.3 + New Features

QML HorizontalHeaderView not working as expected

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmltableviewhorizontalheade
3 Posts 3 Posters 1.3k Views 2 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.
  • R Offline
    R Offline
    raphasauer
    wrote on last edited by
    #1

    I'm trying to add a header to a TableView in QML, but it is not working as expected. Here's a sample code:

    Main.cpp:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    
    int main(int argc, char *argv[])
    {
    #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    #endif
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        const QUrl url(QStringLiteral("qrc:/main.qml"));
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                         &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
        engine.load(url);
    
        return app.exec();
    }
    

    QML:

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import Qt.labs.qmlmodels 1.0
    import QtQuick.Controls 2.15
    
    Window {
        width: 600
        height: 600
        visible: true
    
        title: "Player"
    
    
        TableView {
            id: tableView
            anchors.fill: parent
            columnSpacing: 1
            rowSpacing: 1
            boundsBehavior: Flickable.StopAtBounds
    
    
            model: TableModel {
    
    
                id: tabela
    
                TableModelColumn { display: "type" }
                TableModelColumn { display: "speed" }
                TableModelColumn { display: "ammunition" }
                TableModelColumn { display: "active" }
                TableModelColumn { display: "coordinates" }
    
    
                rows: [
                    {
                        type: "1",
                        speed: "10",
                        ammunition: "30",
                        active: "True",
                        coordinates: "4x2"
    
                    },
                    {
                        type: "3",
                        speed: "0",
                        ammunition: "3",
                        active: "False",
                        coordinates: "10x2"
                    },
                ]
    
    
            }
            delegate:  Text {
                text: model.display
                padding: 20
    
    
    
                Rectangle {
                    anchors.fill: parent
                    color: "#efefef"
                    z: -1
                }
            }
        }
    
        HorizontalHeaderView {
            id: horizontalHeader
            syncView: tableView
            anchors.left: tableView.left
            model: TableModel {
    
                TableModelColumn { display: "type"}
                TableModelColumn { display: "speed" }
                TableModelColumn { display: "ammunition" }
                TableModelColumn { display: "active" }
                TableModelColumn { display: "coordinates" }
    
            }
        }
    }
    

    Here's the output I get from this code: Output

    d2b403f7-8b86-47a0-b647-eb3fb6818bdf-image.png

    Why is the header overlapped with the table? I've provided the header with a a sync view (tableView) in order to sync the header to table. According to the docummentation:

    "Once this property is bound to another TableView, both header and table will synchronize with regard to column widths, column spacing, and flicking horizontally."

    The header's label also seems wrong. I was expecting to get "Type", "Speed", etc. I haven't found much documentation about TableModelColumn, which is used to build the header.

    Thanks for your time.

    jeremy_kJ 1 Reply Last reply
    0
    • R raphasauer

      I'm trying to add a header to a TableView in QML, but it is not working as expected. Here's a sample code:

      Main.cpp:

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include <QQmlContext>
      
      int main(int argc, char *argv[])
      {
      #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      #endif
      
          QGuiApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
          const QUrl url(QStringLiteral("qrc:/main.qml"));
          QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                           &app, [url](QObject *obj, const QUrl &objUrl) {
              if (!obj && url == objUrl)
                  QCoreApplication::exit(-1);
          }, Qt::QueuedConnection);
          engine.load(url);
      
          return app.exec();
      }
      

      QML:

      import QtQuick 2.12
      import QtQuick.Window 2.12
      import Qt.labs.qmlmodels 1.0
      import QtQuick.Controls 2.15
      
      Window {
          width: 600
          height: 600
          visible: true
      
          title: "Player"
      
      
          TableView {
              id: tableView
              anchors.fill: parent
              columnSpacing: 1
              rowSpacing: 1
              boundsBehavior: Flickable.StopAtBounds
      
      
              model: TableModel {
      
      
                  id: tabela
      
                  TableModelColumn { display: "type" }
                  TableModelColumn { display: "speed" }
                  TableModelColumn { display: "ammunition" }
                  TableModelColumn { display: "active" }
                  TableModelColumn { display: "coordinates" }
      
      
                  rows: [
                      {
                          type: "1",
                          speed: "10",
                          ammunition: "30",
                          active: "True",
                          coordinates: "4x2"
      
                      },
                      {
                          type: "3",
                          speed: "0",
                          ammunition: "3",
                          active: "False",
                          coordinates: "10x2"
                      },
                  ]
      
      
              }
              delegate:  Text {
                  text: model.display
                  padding: 20
      
      
      
                  Rectangle {
                      anchors.fill: parent
                      color: "#efefef"
                      z: -1
                  }
              }
          }
      
          HorizontalHeaderView {
              id: horizontalHeader
              syncView: tableView
              anchors.left: tableView.left
              model: TableModel {
      
                  TableModelColumn { display: "type"}
                  TableModelColumn { display: "speed" }
                  TableModelColumn { display: "ammunition" }
                  TableModelColumn { display: "active" }
                  TableModelColumn { display: "coordinates" }
      
              }
          }
      }
      

      Here's the output I get from this code: Output

      d2b403f7-8b86-47a0-b647-eb3fb6818bdf-image.png

      Why is the header overlapped with the table? I've provided the header with a a sync view (tableView) in order to sync the header to table. According to the docummentation:

      "Once this property is bound to another TableView, both header and table will synchronize with regard to column widths, column spacing, and flicking horizontally."

      The header's label also seems wrong. I was expecting to get "Type", "Speed", etc. I haven't found much documentation about TableModelColumn, which is used to build the header.

      Thanks for your time.

      jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #2

      @raphasauer said in QML HorizontalHeaderView not working as expected:

      Why is the header overlapped with the table?

      Space for the header needs to be allocated. The QML code above uses the entire window (anchors.fill: parent) for the table.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      1
      • V Offline
        V Offline
        vinci
        wrote on last edited by vinci
        #3

        This question was asked on stackoverflow as well. Just posting this here because the accepted answer was useful.

        Another approach would be to anchor the table below the header.

        TableView {
            id: tableView
            anchors.top: horizontalHeader.bottom
            model: TableModel {
            }
        }
        
        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