Luca
Luca

Reputation: 10996

center a checkbox in tableview with QML

I am using a checkbox control in a TableView, which I define with QML as follows:

import QtQuick 2.2
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4

TableViewColumn {
    title: ""
    role: "check"    
    delegate: CheckBox {
        anchors.fill: parent
        checked: false
        anchors { horizontalCenter: parent.horizontalCenter }
    }
}

I can use it in a TableView as follows:

import QtQuick 2.3
import QtQuick.Window 2.2
import QtQuick.Layouts 1.1

import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4

ControlView {
    visible: true
    width: parent.width; height: parent.height    
    anchors.centerIn: parent

    TableView {
        id: reviewTable
        width: parent.width; height: parent.height
        anchors.centerIn: parent

        TableViewCheckBoxColumn {
            title: "Upload"
            width: 100
            resizable: false
        }

        TableViewColumn {
            resizable: true
            role: "Dummy"
            title: "Dummy"
            width: 250
        }

        style: TableViewStyle {
            headerDelegate: Rectangle {
                height: textItem.implicitHeight
                width: textItem.implicitWidth

                Text {
                    id: textItem
                    anchors.fill: parent
                    verticalAlignment: Text.AlignVCenter
                    text: styleData.value
                    renderType: Text.NativeRendering
                    font.bold: true                    
                }
            }
        }        

        model: ListModel {
            ListElement {
                Dummy: "value 1"
            }
            ListElement {
                Dummy: "value 3"
            }
        }
    }
}

Now despite having set the anchors (or any alignment property that were available), the checkbox remains aligned to the left. How can I center it horizontally wrt to the column?

Upvotes: 5

Views: 2353

Answers (1)

folibis
folibis

Reputation: 12874

Wrap the CheckBox in the delegate as below:

Item {
    anchors.fill: parent
    CheckBox {
        anchors.centerIn: parent
        checked: false

    }
}

And btw, don't mix QtQuick versions in the same application.

Upvotes: 5

Related Questions