qt - 使用 QML 在 tableview 中居中一个复选框

标签 qt pyqt qml qt-quick

我在 TableView 中使用复选框控件,我用 QML 定义如下:

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 }
    }
}

我可以在 TableView 中使用它,如下所示:
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"
            }
        }
    }
}

现在,尽管设置了 anchor (或任何可用的对齐属性),但复选框仍然与左侧对齐。我怎样才能将它水平居中到列?

最佳答案

包裹CheckBox在委托(delegate)中如下:

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

    }
}

顺便说一句,不要在同一个应用程序中混合 QtQuick 版本。

关于qt - 使用 QML 在 tableview 中居中一个复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40489984/

相关文章:

python - 如何将动画 GIF 集成到 QMessageBox 中?

python - PythonQt 被弃用了吗?

c++ - qt 应用程序崩溃但不容易重现,我已经捕获了转储堆栈以及如何通过堆栈查找源代码

python - 如何从 QVariant 获取原始 python 数据

python - 多处理问题 [pyqt, py2exe]

qml - 无法将 qml 属性初始化为 {}

c++ - 从 QML ListView 操作 QAbstractListModel 中的数据

c++ - Qt FrameBuffer 对象甚至不渲染顶点

c++ - Qt 创建者 : Widget is not added to layout

qt - CMake + Qt = 刻在石头上的 Qt 定义(又名。-DQT_...)?