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-TableView: Center CheckBox in Cell
Forum Update on Monday, May 27th 2025

QML-TableView: Center CheckBox in Cell

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

    Hy!

    I have some troubles by creating a table view. I want to create a table, wich contains a checkbox, but I have no idea how to center this checkbox in the cell.

     TableView {
            width: parent.width
            height: parent.height-50
    
            id: alarmTableInt
    
            style : TableViewStyle {
                textColor: pallete.textColor
                
            }
    
            model: prjCont.model
            //model: tmodel
    
            rowDelegate: Rectangle {
                id: row
                height: pallete.table_RowHeight
                width: parent.width
    
    
                color: styleData.alternate ? pallete.bgColor : pallete.altBgColor
                border.color: styleData.selected ? pallete.selectedTextColor : row.color
                border.width:  styleData.selected ? 1 : 0
    
            }
    
            headerDelegate: Rectangle {
                height: 25
                Text {
                    anchors.fill: parent
                    text: styleData.value
                    renderType: Text.NativeRendering
                    color: pallete.textColor
                    verticalAlignment:   Text.AlignVCenter
                    horizontalAlignment: Text.AlignHCenter
                }
                border.width: 1
                border.color:  pallete.textColor
                color:  pallete.altBgColor
            }
    .
    .
    .
          TableViewColumn {
                id: checkedCol
                role:  "checked"
                title: "checked"
                width: 50
    
                delegate: CheckBox {
                    anchors.fill: parent
                    checked: styleData.value
                    enabled: true
    
    
                    onClicked: {
                        var srcIndex = prjCont.model.mapToSource(prjCont.model.index(styleData.row,0));
                        prjCont.model.getEntry(srcIndex.row).checked = !prjCont.model.getEntry(styleData.row).checked;
    
                        console.debug("row in Table " + styleData.row + " mapToSource " + srcIndex.row );
                        console.debug("-got: " +  prjCont.model.getEntry(srcIndex.row).checked);
                    }
                }
            }
    
    

    maybe someone has an idea how to center "checked" in the cell.

    best regards
    Harry

    H 1 Reply Last reply
    0
    • H hpollak

      Hy!

      I have some troubles by creating a table view. I want to create a table, wich contains a checkbox, but I have no idea how to center this checkbox in the cell.

       TableView {
              width: parent.width
              height: parent.height-50
      
              id: alarmTableInt
      
              style : TableViewStyle {
                  textColor: pallete.textColor
                  
              }
      
              model: prjCont.model
              //model: tmodel
      
              rowDelegate: Rectangle {
                  id: row
                  height: pallete.table_RowHeight
                  width: parent.width
      
      
                  color: styleData.alternate ? pallete.bgColor : pallete.altBgColor
                  border.color: styleData.selected ? pallete.selectedTextColor : row.color
                  border.width:  styleData.selected ? 1 : 0
      
              }
      
              headerDelegate: Rectangle {
                  height: 25
                  Text {
                      anchors.fill: parent
                      text: styleData.value
                      renderType: Text.NativeRendering
                      color: pallete.textColor
                      verticalAlignment:   Text.AlignVCenter
                      horizontalAlignment: Text.AlignHCenter
                  }
                  border.width: 1
                  border.color:  pallete.textColor
                  color:  pallete.altBgColor
              }
      .
      .
      .
            TableViewColumn {
                  id: checkedCol
                  role:  "checked"
                  title: "checked"
                  width: 50
      
                  delegate: CheckBox {
                      anchors.fill: parent
                      checked: styleData.value
                      enabled: true
      
      
                      onClicked: {
                          var srcIndex = prjCont.model.mapToSource(prjCont.model.index(styleData.row,0));
                          prjCont.model.getEntry(srcIndex.row).checked = !prjCont.model.getEntry(styleData.row).checked;
      
                          console.debug("row in Table " + styleData.row + " mapToSource " + srcIndex.row );
                          console.debug("-got: " +  prjCont.model.getEntry(srcIndex.row).checked);
                      }
                  }
              }
      
      

      maybe someone has an idea how to center "checked" in the cell.

      best regards
      Harry

      H Offline
      H Offline
      hpollak
      wrote on last edited by
      #2

      I have allready tried:

       delegate: CheckBox {
                      //anchors.fill: parent
                      checked: styleData.value
                      enabled: true
      
                      width: parent.width
                      height: pallete.table_RowHeight
      
                      anchors.centerIn: parent
      

      Allso "x:" and "y:" doesn't work

      1 Reply Last reply
      0
      • H Offline
        H Offline
        hpollak
        wrote on last edited by
        #3

        with

         delegate: CheckBox {
                        checked: styleData.value
                        enabled: true
                        anchors.baseline: parent.verticalCenter 
        

        the checkbox is vertical centered in the cell, but i have no idea how to do this for vertical and horizontal center

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Marc37
          wrote on last edited by
          #4

          I "solved"this problem by putting the checkbox in a separate QML file, and set the left margin to almost half of the parent width.

          anchors.left: parent.left
          anchors.lefMargin: parent.width/2 - 6 // assuming the checkbox is about 12 pixels
          
          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