当前位置: 首页 > news >正文

QML弹窗


目标:QML实现弹窗实现点击按钮弹窗,点击不同的按钮 弹窗的内容会改变,但窗口不消失。点击窗口外部则窗口消失。

效果:

代码:

import QtQuick 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
    width: 400
    height: 300
    visible: true

    // 弹窗控制器
    property string currentContent: ""

    // 主界面按钮
    Row {
        spacing: 10
        Button {
            text: "显示内容A"
            onClicked: {
                currentContent = "A"
                popup.open()
            }
        }
        Button {
            text: "显示内容B"
            onClicked: {
                currentContent = "B"
                popup.open()
            }
        }
    }

    // 弹窗定义
    Popup {
        id: popup
        anchors.centerIn: parent
        modal: true
        dim: true // 点击外部关闭
        closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside

        // 核心:动态内容加载器
        Loader {
            id: contentLoader
            sourceComponent: {
                if (currentContent === "A") return contentA
                else if (currentContent === "B") return contentB
                else return null
            }
        }
    }

    // 定义不同内容组件
    Component {
        id: contentA
        Rectangle {
            width: 200
            height: 100
            color: "lightblue"
            Text { text: "这是内容A"; anchors.centerIn: parent }
        }
    }

    Component {
        id: contentB
        Rectangle {
            width: 200
            height: 150
            color: "lightgreen"
            Column {
                anchors.centerIn: parent
                Text { text: "这是内容B" }
                Button { text: "子按钮"; onClicked: console.log("点击B的子按钮") }
            }
        }
    }
}

http://www.dtcms.com/a/107190.html

相关文章:

  • H(e^j\omega) H(\omega)
  • 计算机网络复习 吉林大学
  • PH热榜 | 2025-04-02
  • OSI每一层的SAP和CEP分别是什么
  • 宠物店小程序怎么做?助力实体店实现营销突破
  • vue3+vite,引入Tailwind问题汇总
  • 模型开源|支持东方40语种+中国22方言的新SOTA语音大模型Dolphin开源啦!
  • 制造业数字化转型:流程改造先行还是系统固化数据?基于以MTO和MTS的投资回报分析
  • 防爆风扇选型指南:根据风量风压匹配应用场景​
  • C语言函数实战指南:从零到一掌握函数设计与10+案例解析(附源码)
  • PPTAgent:一款开源免费生成和评估幻灯片的项目
  • QILSTE/旗光
  • RabbitMQ基础
  • 【5090d】配置运行和微调大模型所需基础环境【一】
  • 简述竞赛经历在考研复试中的作用
  • rom定制系列------红米note8pro原生安卓12批量线刷 安卓14批量线刷定制功能项 解锁bl后fast刷写
  • Bash 花括号扩展 {start..end} 进阶使用指南——字典生成
  • Linux进程间通信(1)
  • 天梯赛 L2-025 分而治之
  • GoldenEye: 1靶场渗透
  • 第四章,动态路由介绍//////RIP
  • 【Kubernetes】如何使用 kubeadm 搭建 Kubernetes 集群?还有哪些部署工具?
  • 基于昇腾NPU的YOLOv8部署
  • redis一些常用的命令(1)
  • 【零基础入门unity游戏开发——2D篇】SortingGroup(排序分组)组件
  • acwing 每日一题4889. 空调II
  • WinForm真入门(4)——窗体和控件、属性和事件 的基本概念
  • NFC碰一碰到底是什么?具体有什么功能
  • Transformer
  • Vue.js状态管理利器:Vuex核心原理与实战指南