QT之QML从入门到精通(第四章)

news/2024/9/24 4:26:48/

Text使用

许许多多的控件都继承于此控件,比较重要。

import QtQuick 2.12 //2.15
import QtQuick.Window 2.12 //2.15
import QtQuick.Controls 2.12 //可以引入别的控件
import Qt.labs.folderlistmodel 2.12
import Qt.labs.platform 1.0 as Platform
import QtQuick.Layouts 1.15Window{width:500height:500title: "Mouse Area"visible :trueRectangle{width:50;height: 50;border.color: "black"Text {id: textfont.bold: true //设置粗体font.family: "Courier New" //设置字体text: qsTr("text 99999\n999999\n999999\n99999999123 ") //设置文本color:"red" //设置字体颜色font.italic: true;//设置斜体font.letterSpacing: 10 //设置字体之间的距离// font.pointSize: 36 //以榜为单位// font.pixelSize: 30 // 字体的大小像素为单位font.underline: true //设置下划线lineHeight: 2elide: Text.ElideRight //当文字过长是省略右边文件,需要和下方属性配合使用:有左边 中间 右边三个选项anchors.fill: parent//设置控件填充为父部件Component.onCompleted: { //组件加载完成时调用console.log(contentWidth) //打印内容的宽度console.log(contentHeight) //打印内容的高度console.log(lineCount) //返回字体的行数console.log(lineHeight)//每行文本的间隔高度}}}
}

Text控件格式化

import QtQuick 2.12 //2.15
import QtQuick.Window 2.12 //2.15
import QtQuick.Controls 2.12 //可以引入别的控件
import Qt.labs.folderlistmodel 2.12
import Qt.labs.platform 1.0 as Platform
import QtQuick.Layouts 1.15Window{width:500height:500title: "Mouse Area"visible :trueColumn {Text {font.pointSize: 24//默认支持富文本text: "<b>Hello</b> <i>World!</i>"}Text {font.pointSize: 24textFormat: Text.RichText //设置富文本text: "<b>Hello</b> <i>World!</i>"}Text {font.pointSize: 24textFormat: Text.PlainText //设置普通字符串文本不进行转义text: "<b>Hello</b> <i>World!</i>"}Text {font.pointSize: 24textFormat: Text.MarkdownText //设置markdown语法文本text: "**Hello** *World!*"}}}
import QtQuick 2.12 //2.15
import QtQuick.Window 2.12 //2.15
import QtQuick.Controls 2.12 //可以引入别的控件
import Qt.labs.folderlistmodel 2.12
import Qt.labs.platform 1.0 as Platform
import QtQuick.Layouts 1.15Window{width:500height:500title: "Mouse Area"visible :trueRectangle{id:rectwidth:100;height:200border.color: "black"Text{id:txttext:"<a href=\"https://www.baidu.com\">test test test test</a>"//使用html设置,注意href前不能有空格wrapMode: Text.WordWrap //设置换行方式,根据单词来换行anchors.fill: parentelide: Text.ElideRightMouseArea{anchors.fill: parent // 控件填充,不过会填充整个区域,点击文字外也会响应hoverEnabled: true //开启悬停cursorShape: Qt.PointingHandCursor //改变光标的形状,但是此时点击时不会触发onLinkActivated事件因为被拦截了onClicked: {console.log("onclicked:") //当链接被点击时,输出链接}}onLinkHovered: {console.log("hover",link) //链接悬停时触发信号}onHoveredLinkChanged: {console.log("onHoveredLinkChanged",link) //悬停离开时触发}onLinkActivated: {console.log(link) //当链接被点击时,输出链接Qt.openUrlExternally(link) //打开超链接}}}
}

 text打开超链接

import QtQuick 2.12 //2.15
import QtQuick.Window 2.12 //2.15
import QtQuick.Controls 2.12 //可以引入别的控件
import Qt.labs.folderlistmodel 2.12
import Qt.labs.platform 1.0 as Platform
import QtQuick.Layouts 1.15Window {visible: trueMouseArea {anchors.fill: parentonClicked: {// Qt.quit();}}MouseArea{anchors.centerIn: parentwidth: hp.widthheight: hp.heighthoverEnabled: truecursorShape: containsMouse ? (pressed ? Qt.ClosedHandCursor : Qt.OpenHandCursor) : Qt.ArrowCursorText {id: hptext: "<a href='http://www.baidu.com'><h1>点击进入首页</h1></a>"anchors.centerIn: parentonLinkActivated: Qt.openUrlExternally(link)}}}

Popup的绘制与Overlay的使用

import QtQuick 2.12 //2.15
import QtQuick.Window 2.12 //2.15
import QtQuick.Controls 2.12 //可以引入别的控件
import Qt.labs.folderlistmodel 2.12
import Qt.labs.platform 1.0 as Platform
import QtQuick.Layouts 1.15Window{width:500height:500title: "Mouse Area"visible :true// // 坑一:// Rectangle{//     width:200;height:100//     visible:false //这里qml有一个逻辑,就是如果父控件设置为false,则他的子控件都不显示,但是Popup是一个例外,父控件为false是,子控件的Popup显示,但是父控件是Popup会不显示//     // color:"black"//     Popup{//         width:50;height:50//         visible:true//         // color:"red"//     }// }// 坑二:// Popup的z顺序是一个例外,z影响与其他Popup之间的顺序,不影响与其他之间的顺序Rectangle{id:rectwidth:200height:100visible:true// color:"black"// z:1}Popup{id:rect1width:200height:100visible:true// color:"blue"x:100}// Button{//     width:50;height:50//     onClicked: {//         popup.open() //open打卡,close关闭,相当于修改visible属性//     }// }//存在两个坑Popup{  //类似于Rectangleid:popupx:100y:100width:200;height:300visible: trueComponent.onCompleted: { //一般的信号前面都要加onconsole.log(visible) //默认的时候是false不显示,可以手动让他显示。}}
}

使用多文件加载的Popup例子

MyPopup.qml

import QtQuick 2.12
import QtQuick.Controls 2.15Popup {id:popupx:100;y:100width:400;height:300visible:true// closePolicy:Popup.CloseOnEscape|Popup.CloseOnPressOutsideParentclosePolicy: Popup.NoAutoClose //可以设置不自动关闭,设置后怎么点击都关闭不了modal:true //模态对话框,必须先处理对话框内部的逻辑// dim:true //默认为true,背景是深灰色,false是白色 ,控制非模态对话框的背景颜色// The default value is Popup.CloseOnEscape | Popup.CloseOnPressOutside.默认值点击外部或者按下esc退出enter: Transition { //打卡的时候NumberAnimation{property: "opacity"from:0.0to:1.0duration: 1000}}//不能和下方一起使用// background:Rectangle{//     color:"gray"// }contentItem: Rectangle{ //控件的绘制属性anchors.fill: parentcolor:"gray"Text{id:txtanchors.fill: parenttext:"Message Box Area!"font.pixelSize: 30wrapMode: Text.WordWrap}Button{anchors.bottom: parent.bottomanchors.bottomMargin: 30anchors.right: parent.rightanchors.rightMargin: 30text:"cancel"}Button{anchors.bottom: parent.bottomanchors.bottomMargin: 30anchors.right: parent.rightanchors.rightMargin: 230text:"ok"}}Overlay.modal:Rectangle{ //对话框是模态时,背景色为浅蓝色,需要设置dim:false,modal:trueanchors.fill: parent// color:"lightsteelblue" //英文设置颜色// color:"#FF0000" //RGB的16进制color:"#33000000" // RGBA 前2位是透明度Popup{ //overlay类中存在的Popup则按钮可以点击width:parent.width;height:80visible:truebackground:Rectangle{// color:"transparent" //设置为透明色,color:"gray"}Button{width:50;height:50anchors.right: parent.rightonClicked: {console.log("click")}}}}Overlay.modeless:Rectangle{ //对话框是模态时,背景色为颜色,需要设置dim:true,modal:falseanchors.fill: parentcolor:"blue"}}

main.qml

import QtQuick 2.12 //2.15
import QtQuick.Window 2.12 //2.15
import QtQuick.Controls 2.12 //可以引入别的控件
import Qt.labs.folderlistmodel 2.12
import Qt.labs.platform 1.0 as Platform
import QtQuick.Layouts 1.15Window{width:500height:500title: "Mouse Area"visible :trueButton{width:50;height:50onClicked: {console.log("btn clicked")popup.close()}}MyPopup{}
}

repeater

用于设置控件重复几次

import QtQuick 2.12 //2.15
import QtQuick.Window 2.12 //2.15
import QtQuick.Controls 2.12 //可以引入别的控件
import Qt.labs.folderlistmodel 2.12
import Qt.labs.platform 1.0 as Platform
import QtQuick.Layouts 1.15Window{width:800height:600title: "Mouse Area"visible :trueRepeater{ //设置重复控件 转轮手枪;连发枪//循环几次// model:3 //模型 可以是任何类型,数字代表是几个控件/模型model:["Button","Rectangle","MouseArea"] //数量还是3,读取list,可以获取model中的数据Button{y:index*50 //描述一个控件,通过index拿到每个控件width:100;height:40// border.width: 1// color:"yellow"text:modelData //modelData拿到model中的值}Component.onCompleted: {console.log(count)}}}


http://www.ppmy.cn/news/1529645.html

相关文章

MatrixOne助力一道创新打造高性能智能制造AIOT系统

客户简介 深圳一道创新&#xff08;ETAO Innovation&#xff09;成立于2012年&#xff0c;是一家创新型软件及信息技术服务商&#xff0c;致力于制造戏份行业—电子制造业的数字转型服务&#xff0c;构建万物互联的智能工程。一道创新致力于把先进的软件系统、数字平台、人工智…

高维数据和超高维数据

在统计学中&#xff0c;高维数据和超高维数据都是指具有大量特征&#xff08;变量&#xff09;的数据集&#xff0c;但它们之间存在一些重要的联系与区别。 联系 维度概念&#xff1a;两者都涉及到数据维度的增高&#xff0c;意味着每个观测值包含许多特征。挑战&#xff1a;…

成都睿明智科技有限公司怎么样?

在这个日新月异的数字时代&#xff0c;抖音电商正以破竹之势重塑着消费市场的格局&#xff0c;成为无数商家和品牌竞相追逐的新蓝海。在这片充满机遇与挑战的浪潮中&#xff0c;成都睿明智科技有限公司犹如一颗璀璨的明星&#xff0c;凭借其专业的服务、创新的策略和敏锐的市场…

Flask 设置session 自定义登录验证

"""1. 设置session# 设置session成功 重定向到首页session.permanent True # 设置会话过期时间session[info] usernamereturn redirect(url_for(index))2. 获取sessioninfo session.get(info, default0)return render_template(index.html, infoinfo)3. 设置…

React + Vite 多环境配置

1.根目录创建文件&#xff1a; .env.dev //测试环境 .env.development //本地环境 .env.production //正式环境 .env.uat //预发布环境 注&#xff1a;变量名必须使用 VITE_API 开头 2.package.json 配置&#xff1a; --mode 设置读取制定 .env文件 &#xff0c;默认读取.en…

TLC/TK Adv学习笔记1 - Py版本+美化

Python下重点 tkinter.ttk 模块自 Tk 8.5 开始引入&#xff0c;它提供了对 Tk 风格的部件集的访问。 它还带来了一些额外好处包括在 X11 下的反锯齿字体渲染和透明化窗口&#xff08;需要有 X11 上的混合窗口管理器&#xff09;。 tkinter.ttk 的基本设计思路&#xff0c;就是…

html+css+js网页设计 旅游 穷游10个页面

htmlcssjs网页设计 旅游 穷游10个页面 网页作品代码简单&#xff0c;可使用任意HTML辑软件&#xff08;如&#xff1a;Dreamweaver、HBuilder、Vscode 、Sublime 、Webstorm、Text 、Notepad 等任意html编辑软件进行运行及修改编辑等操作&#xff09;。 获取源码 1&#xff…

基于PHP的电脑线上销售系统

作者&#xff1a;计算机学姐 开发技术&#xff1a;SpringBoot、SSM、Vue、MySQL、JSP、ElementUI、Python、小程序等&#xff0c;“文末源码”。 专栏推荐&#xff1a;前后端分离项目源码、SpringBoot项目源码、SSM项目源码 系统展示 【2025最新】基于phpMySQL的电脑线上销售系…