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. Change TableView Backgroun
Qt 6.11 is out! See what's new in the release blog

Change TableView Backgroun

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 2 Posters 936 Views
  • 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.
  • N Offline
    N Offline
    nero_ner
    wrote on last edited by nero_ner
    #1

    Hi I’m Working with TableView in 6.5.3 with quick controls 2.15 and am struggling to figure out how to change the background of a TableView. TableViewStyle doesn’t exist in 2.15 and I don’t see any properties on the docs that expose styling for the table view itself.

    this is some sample code for reference

    TableView {
                    id: topicsTableView
                    anchors.top: topicsHeader.bottom
                    anchors.bottom: parent.bottom
                    width: parent.width
                    columnSpacing: 1
                    rowSpacing: 1
                    clip: true
    
                    model: TableModel {
                        TableModelColumn {
                            display: "topic"
                        }
    
                        rows: debugTable.topics
                    }
    
                    delegate: Rectangle {
                        implicitWidth: topicsTableView.width
                        implicitHeight: debugTable.rowHeight
                        border.width: scrollingTopics
                                      && model.index === debugTable.selectedTopicIndex ? 5 : 1
    
                        Text {
                            text: display
                            anchors.centerIn: parent
                        }
                    }
                }
    
    JoeCFDJ 1 Reply Last reply
    0
    • N nero_ner

      @JoeCFD what do you mean "add a rectangle to tableview for background" when i say

      background: Rectangle {
         color: "black"
      }
      

      i get an error that background is not a known property of TableView

      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by
      #6

      @nero_ner
      something like:

      import QtQuick 2.15
      import QtQuick.Controls 2.15
      
      ApplicationWindow {
          visible: true
          width: 600
          height: 400
          title: "TableView with Background"
      
          // Define your custom background color or image here
          Rectangle {
              width: parent.width
              height: parent.height
              color: "lightblue" // Set your desired background color
      
              TableView {
                  id: tableView
                  anchors.fill: parent
      
                  model: ListModel {
                      ListElement { name: "John"; age: 25 }
                      ListElement { name: "Alice"; age: 30 }
                      ListElement { name: "Bob"; age: 22 }
                  }
                  
                  delegate: Rectangle {
                      implicitWidth: tableView.width / 2
                      implicitHeight: 40
                      border.width: 1
                      
                      Text {
                          text: model.name
                          color: "black"
                          anchors.centerIn: parent
                      }
                  }
              }
          }
      }
      
      N 1 Reply Last reply
      1
      • N nero_ner

        Hi I’m Working with TableView in 6.5.3 with quick controls 2.15 and am struggling to figure out how to change the background of a TableView. TableViewStyle doesn’t exist in 2.15 and I don’t see any properties on the docs that expose styling for the table view itself.

        this is some sample code for reference

        TableView {
                        id: topicsTableView
                        anchors.top: topicsHeader.bottom
                        anchors.bottom: parent.bottom
                        width: parent.width
                        columnSpacing: 1
                        rowSpacing: 1
                        clip: true
        
                        model: TableModel {
                            TableModelColumn {
                                display: "topic"
                            }
        
                            rows: debugTable.topics
                        }
        
                        delegate: Rectangle {
                            implicitWidth: topicsTableView.width
                            implicitHeight: debugTable.rowHeight
                            border.width: scrollingTopics
                                          && model.index === debugTable.selectedTopicIndex ? 5 : 1
        
                            Text {
                                text: display
                                anchors.centerIn: parent
                            }
                        }
                    }
        
        JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by JoeCFD
        #2

        @nero_ner if your tableview is fully filled with delegates, add color property to delegate as background color.

        take a look at the Qt6 example:
        quick/tableview/gameoflife

        N 1 Reply Last reply
        0
        • JoeCFDJ JoeCFD

          @nero_ner if your tableview is fully filled with delegates, add color property to delegate as background color.

          take a look at the Qt6 example:
          quick/tableview/gameoflife

          N Offline
          N Offline
          nero_ner
          wrote on last edited by
          #3

          @JoeCFD what if the table is not filled with delegates? Should I just make the tables height just wrap content?

          JoeCFDJ 1 Reply Last reply
          0
          • N nero_ner

            @JoeCFD what if the table is not filled with delegates? Should I just make the tables height just wrap content?

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by JoeCFD
            #4

            @nero_ner then add a rectangle to tableview for background. Usually, alternating colors are applied for delegates and you can fill tableview with delegates.

            N 1 Reply Last reply
            0
            • JoeCFDJ JoeCFD

              @nero_ner then add a rectangle to tableview for background. Usually, alternating colors are applied for delegates and you can fill tableview with delegates.

              N Offline
              N Offline
              nero_ner
              wrote on last edited by
              #5

              @JoeCFD what do you mean "add a rectangle to tableview for background" when i say

              background: Rectangle {
                 color: "black"
              }
              

              i get an error that background is not a known property of TableView

              JoeCFDJ 1 Reply Last reply
              0
              • N nero_ner

                @JoeCFD what do you mean "add a rectangle to tableview for background" when i say

                background: Rectangle {
                   color: "black"
                }
                

                i get an error that background is not a known property of TableView

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by
                #6

                @nero_ner
                something like:

                import QtQuick 2.15
                import QtQuick.Controls 2.15
                
                ApplicationWindow {
                    visible: true
                    width: 600
                    height: 400
                    title: "TableView with Background"
                
                    // Define your custom background color or image here
                    Rectangle {
                        width: parent.width
                        height: parent.height
                        color: "lightblue" // Set your desired background color
                
                        TableView {
                            id: tableView
                            anchors.fill: parent
                
                            model: ListModel {
                                ListElement { name: "John"; age: 25 }
                                ListElement { name: "Alice"; age: 30 }
                                ListElement { name: "Bob"; age: 22 }
                            }
                            
                            delegate: Rectangle {
                                implicitWidth: tableView.width / 2
                                implicitHeight: 40
                                border.width: 1
                                
                                Text {
                                    text: model.name
                                    color: "black"
                                    anchors.centerIn: parent
                                }
                            }
                        }
                    }
                }
                
                N 1 Reply Last reply
                1
                • JoeCFDJ JoeCFD

                  @nero_ner
                  something like:

                  import QtQuick 2.15
                  import QtQuick.Controls 2.15
                  
                  ApplicationWindow {
                      visible: true
                      width: 600
                      height: 400
                      title: "TableView with Background"
                  
                      // Define your custom background color or image here
                      Rectangle {
                          width: parent.width
                          height: parent.height
                          color: "lightblue" // Set your desired background color
                  
                          TableView {
                              id: tableView
                              anchors.fill: parent
                  
                              model: ListModel {
                                  ListElement { name: "John"; age: 25 }
                                  ListElement { name: "Alice"; age: 30 }
                                  ListElement { name: "Bob"; age: 22 }
                              }
                              
                              delegate: Rectangle {
                                  implicitWidth: tableView.width / 2
                                  implicitHeight: 40
                                  border.width: 1
                                  
                                  Text {
                                      text: model.name
                                      color: "black"
                                      anchors.centerIn: parent
                                  }
                              }
                          }
                      }
                  }
                  
                  N Offline
                  N Offline
                  nero_ner
                  wrote on last edited by
                  #7

                  @JoeCFD Wow, i cannot believe i didnt try that thank you so much!

                  1 Reply Last reply
                  0
                  • N nero_ner has marked this topic as solved on

                  • Login

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