找回密码
 立即注册
收起左侧

QML TableView 中添加RadioButton

1
回复
5003
查看
[复制链接]
累计签到:22 天
连续签到:1 天
来源: 2019-10-14 15:19:27 显示全部楼层 |阅读模式
1Qter豆
简单描述一下,我新建一个QML项目,放置了一个TableView,目的是显示数据库的数据,每一行的最后一个字段是个RadioButton,现在实现了给每一条数据添加一个RadioButton,但是不知道怎么获取这个RadioButton是否是选中的状态。
贴一下代码:
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4

Window {
    visible: true
    width: 640
    height: 900
    title: qsTr("Hello World")

    ExclusiveGroup{
        id: mos
    }

    Component{
        id: rastyle
        RadioButtonStyle{
            indicator: Rectangle{
                implicitWidth: 16
                implicitHeight: 12
                radius: 16
                border.color: control.hovered ? "darkblue" : "green"
                border.width: 1
                Rectangle {
                    anchors.fill: parent
                    visible: control.checked
                    //color: "#0000A0"
                    color: "red"
                    radius: 5
                    anchors.margins: 3
                }
            }
        }
    }

    TableView{
        id: phoneTable
        anchors.fill: parent

        TableViewColumn {role: "name"; title: "Name"; width: 100; elideMode: Text.ElideRight;}
        TableViewColumn {role: "cost"; title: "Cost"; width: 100;}
        TableViewColumn {role: "manufacturer"; title: "Manufacturer"; width: 140;}
        TableViewColumn {role: "submit"; title: "Submit"; width: 100
            delegate: RadioButton{
                style: rastyle
                text: "提交"
            }
        }
        model: ListModel {
            id: phoneModel
            ListElement {name: "iPhone 5"; cost: "4900"; manufacturer: "Apple"; submit: ""}
            ListElement {name: "Galaxy S5"; cost: "4699"; manufacturer: "Samsung"; submit: ""}
        }

    }
    Rectangle{
        width: parent.width / 4
        height: width / 2
        anchors.bottom: parent.bottom
        anchors.right: parent.right
        color: "red"
        MouseArea{
            anchors.fill: parent
            onClicked: {
                console.log(phoneTable.currentRow)
                var m = phoneTable.model.get(phoneTable.currentIndex)
                console.log(m.submit.checked)//这里不行
                console.log(rat.checked)//不在TableView中,在其他地方建一个RadioButton,可以获取状态
            }
        }
    }
    RadioButton{
        id: rat
        width: 100
        height: 100
        text: "测试"
        anchors.centerIn: parent
        style: rastyle
    }
}


附件: 您需要 登录 才可以下载或查看,没有帐号?立即注册
回复

使用道具 举报

累计签到:1 天
连续签到:1 天
2019-10-15 17:15:05 显示全部楼层
本帖最后由 feeeee 于 2019-10-15 17:18 编辑

TableView{
        id: phoneTable
        anchors.fill: parent

        TableViewColumn {role: "name"; title: "Name"; width: 100; elideMode: Text.ElideRight;}
        TableViewColumn {role: "cost"; title: "Cost"; width: 100;}
        TableViewColumn {role: "manufacturer"; title: "Manufacturer"; width: 140;}
        TableViewColumn {role: "submit"; title: "Submit"; width: 100
            delegate: RadioButton{
                exclusiveGroup: mos
                style: rastyle
                text: "提交"
                checked: model.submit
                onClicked: {
                    phoneModel.get(model.index).submit = checked
                }
            }
        }
        model: ListModel {
            id: phoneModel
            ListElement {name: "iPhone 5"; cost: "4900"; manufacturer: "Apple"; submit: false}
            ListElement {name: "Galaxy S5"; cost: "4699"; manufacturer: "Samsung"; submit: true}
        }

    }
访问photoModel就知道了,想不出tableView访问radioButton的方法
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了