QML —— ComboBox、RadioButton的组合示例(附完整源码)

示例 - 效果

在这里插入图片描述

实例 - 源码
import QtQuick 2.12
import QtQuick.Window 2.12

import QtQuick.Layouts 1.12
import QtQuick.Controls 2.5

Window
{
    id: rootId
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Text
    {
        id: className
        text: qsTr("--")
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
        anchors.verticalCenterOffset: parent.height/4
        font.pixelSize: 25
        font.family: "微软雅黑"
    }

    Column
    {
        anchors.top: parent.top
        anchors.topMargin: 10
        anchors.left: parent.left
        anchors.leftMargin: 10
        spacing: 20

        Row
        {
            spacing: 10
            Text
            {
                text: qsTr("通过下拉列表控件选择你喜欢的一门课程:")
                font.pixelSize: 20
                font.family: "微软雅黑"
            }

            ComboBox
            {
                id: comboboxID
                font.pixelSize: 18
                font.family: "微软雅黑"
                currentIndex: 0
                height: 30

                /* 内容设置方式一 */
                model:["语文","数学","英语","物理","化学","政治","历史","美术","音乐"]
                /****************/

                /* 内容设置方式二 */
//                model: ListModel
//                {
//                    id: comboBoxListModel
//                    ListElement{text:"语文";color:"yellow"}
//                    ListElement{text:"数学";color:"red"}
//                    ListElement{text:"英语";color:"pink"}
//                    ListElement{text:"物理";color:"black"}
//                    ListElement{text:"化学";color:"green"}
//                    ListElement{text:"政治";color:"blue"}
//                    ListElement{text:"历史";color:"purple"}
//                    ListElement{text:"美术";color:"orange"}
//                    ListElement{text:"音乐";color:"gray"}
//                }
//                textRole: "text"
                /****************/

                onActivated:{rootId.sltComboboxChanged(index)}

                // RaduiButton选中则对下拉列表进行修改相同的名称
                property string oldCheckedTxt : ""
                function sltCheckedChanged(txt)
                {
                    if(oldCheckedTxt.length > 0 &&  oldCheckedTxt === txt){return}
                    oldCheckedTxt=txt

                    let idIndex = comboboxID.find(txt)
                    if(idIndex !== -1)
                    {
                        comboboxID.currentIndex = idIndex
                        className.text = txt
                    }
                }
            }
        }


        Grid
        {
            anchors.left: parent.left
            anchors.leftMargin: 30
            rowSpacing: 10
            columnSpacing: 40
            columns: 3

            Repeater
            {
                id: radioButtonList
                model:["语文","数学","英语","物理","化学","政治","历史","美术","音乐"]
                RadioButton
                {
                    text: modelData
                    autoExclusive:true
                    onClicked:
                    {
                        if(checked){comboboxID.sltCheckedChanged(text)}
                    }
                }
            }
        }
    }

    // 根据下拉列表控件索引去设置Repeater索引对应的RaduiButton为选中状态
    function sltComboboxChanged(index)
    {
        radioButtonList.itemAt(index).checked = true
        className.text = radioButtonList.itemAt(index).text

        // 更新comboboxID.oldCheckedTxt,为了同步下拉列表的内容更新
        comboboxID.oldCheckedTxt = className.text
    }
}

关注

笔者 - jxd

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
QML中自制ComboBox可以通过自定义控件来实现,可以使用基础的控件来模拟ComboBox的功能。根据您的需求,您可以创建一个下拉菜单控件,通过点击按钮展开或收起菜单选项。在QML中,您可以使用ListView或者Repeater来显示菜单选项,然后根据用户的选择更新显示文本。您可以使用Item来创建自定义样式,设置按钮样式、菜单选项样式等。您还可以使用属性来填充数据模型,可以使用JavaScript数组、ListModel或者整数,也支持其他类型的数据模型提供的属性。通过使用这些控件和属性,您可以根据自己的需求来自制ComboBox。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [QML 自定义ComboBox控件](https://blog.csdn.net/weixin_43892328/article/details/122615847)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [QML 自制二级开合下拉菜单](https://blog.csdn.net/weixin_51019869/article/details/118146812)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

信必诺

嗨,支持下哥们呗。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值