巢湖城市建设投资有限公司网站,wpf可以做网站吗,太仓网站建设太仓,全网营销型的网站qml相关知识1 QtQuick.Controls 哪个版本支持TreeModel 和 TreeItemqt5.12开始#xff0c;TreeItem 类被删除#xff0c;无法使用delegate 什么时候可以用Qt5.15中没有 import QtQuick.Controls 1吗#xff0c;哪个版本有control1qml如何两种版本的controls混用#xff08;… qml相关知识1 QtQuick.Controls 哪个版本支持TreeModel 和 TreeItemqt5.12开始TreeItem 类被删除无法使用delegate 什么时候可以用Qt5.15中没有 import QtQuick.Controls 1吗哪个版本有control1qml如何两种版本的controls混用QML之在QtQuick.Controls 2项目中使用QtQuick.Controls模块中的控件QML Row: Cannot specify left, right, horizontalCenter, fill or centerIn anchors for items inside Rowqml 虚线边框绘制关闭文件系统视图消息提示框PopupDialog相关如何给下边框设置阴影效果Qt Design Studio 4.2.0怎么引入Calendar组件并使用Calendar引入QtQuick.Controls.Styles 2.15显示module not foundachors.rightMargin失效label下拉框.ui.qml不支持function鼠标响应事件Menu介绍自定义菜单组件官方文档菜单组件Menu应用点击事件出现Dialog弹框弹框中modal属性pointSize和pixelSize区别表格自定义内容中delegate类型Rectangle隐藏下边框表格只显示下边框QML 布局详解RectanglebuttonButton自定义树形结构边的阴影怎么实现TextInput的使用方法TextInput禁止输入模式TextInput文字居中方式Text文字右对齐Text文字居中方式anchors.fill和anchors.centerIn区别自定义组件自定义属性Itemanchors详解什么时候用Item什么时候用Rectangle什么时候用Row什么时候用RowLayoutlable用法ColumnQt中font.pointSize和font.pixelSize的区别Qt中fileMode隐藏虚拟键盘qt design studioA开发工具中 新建的.ui.qml中引入ColumnLayout后无法启动【Run Project】用Column布局可以正常运行但是加上以下四句运行失效报错module QtQuick.Layouts version 5.15 is not installedqt design studio中用ColumnLayout失效用Column正常显示【没法用ColumnLayoutRowLayout】qt design studio的2D预览和运行不一致的问题2D预览可能不准确报错Parameter mouse is not declared. Injection of parameters into signal handlers is deprecated. Use JavaScript functions with formal parameters instead.报错QML Row: Cannot specify left, right, horizontalCenter, fill or centerIn anchors for items inside Row. Row will not function.报错QML Column: Cannot specify top, bottom, verticalCenter, fill or centerIn anchors for items inside Column. Column will not function.自己写的登录页面的例子 QtQuick.Controls 哪个版本支持TreeModel 和 TreeItem
在QtQuick.Controls模块中并没有直接提供TreeModel和TreeItem。TreeModel和TreeItem是基于Qt的Models模块自定义的模型和项用于在QML中创建树状结构的数据。
若想在QML中使用TreeModel和TreeItem你需要自己实现这些模型和项然后将其在QtQuick.Controls中使用。这并不依赖于QtQuick.Controls模块的特定版本而是依赖于你自己的实现代码。
你可以参考Qt的Models模块文档自己实现TreeModel和TreeItem。在应用中可以将这些自定义的模型与QtQuick.Controls的相关组件如TreeView、TreeViewItem结合使用来展示树状结构的数据。
qt5.12开始TreeItem 类被删除无法使用
从 Qt 5.12 开始TreeView 组件的 API 发生了重大变化TreeItem 类被删除并使用命令式的 TreeView 模型进行代替。
delegate 什么时候可以用
在 QtQuick.Controls 中delegate 是一个可用的属性可以在不同的版本中使用。根据 Qt 官方文档的最新记录在 Qt 5.12 或更高版本中的 QtQuick.Controls 中都可以使用 delegate 属性。
需要注意的是delegate 是针对一些特定的控件如 ListView、ComboBox 等的属性用于自定义每个项的外观。不是所有的控件都具有 delegate 属性。
如果你在使用 QtQuick.Controls 1.4 版本可以尝试检查你所使用的控件是否具有 delegate 属性并按照该版本的文档进行使用。 搜【帮助-》索引-》输入delegate查看】
treeview组件中没有此属性
Qt5.15中没有 import QtQuick.Controls 1吗哪个版本有control1
在 Qt 5.15 中QtQuick.Controls 模块的版本仅从 2 开始可用并没有 QtQuick.Controls 1 的导入语句。 只有从 Qt 5.0 到 Qt 5.14才提供了 QtQuick.Controls 1 模块。 QtQuick.Controls 2 是自从 Qt 5.7 版本引入的。
所以像TreeViewCalendar的使用一般用controls1即Qt5.14 【之前不懂这个自定义日历和表格总是出现确实某个模块的报错来来回回卸载安装了无数次希望这次别让我失望】
qml如何两种版本的controls混用QML之在QtQuick.Controls 2项目中使用QtQuick.Controls模块中的控件
参考https://blog.csdn.net/zbw1185/article/details/81058486 两种方法 1、类型重命名 正确的做法是将使用到的QtQuick.Controls模块进行类型重命名然后再调用其中的控件。
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4 as Controls14
import QtQuick.Controls.Styles 1.4 as Styles14
import QtQuick.Controls 2.2Window {visible: truewidth: 640height: 480title: qsTr(Hello World)//qml1Controls14.Button {x: 100y: 200text: A buttonstyle: Styles14.ButtonStyle {background: Rectangle {implicitWidth: 100implicitHeight: 25border.width: control.activeFocus ? 2 : 1border.color: #888radius: 4gradient: Gradient {GradientStop { position: 0 ; color: control.pressed ? #ccc : #eee }GradientStop { position: 1 ; color: control.pressed ? #aaa : #ccc }}}}}//qml2Button{x: 300y: 200text: B button}
}
2、将控件封装成组件 MyButton.qml
import QtQuick 2.0
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4Button {x: 100y: 200text: A buttonstyle: ButtonStyle {background: Rectangle {implicitWidth: 100implicitHeight: 25border.width: control.activeFocus ? 2 : 1border.color: #888radius: 4gradient: Gradient {GradientStop { position: 0 ; color: control.pressed ? #ccc : #eee }GradientStop { position: 1 ; color: control.pressed ? #aaa : #ccc }}}}
}
直接调用
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2Window {visible: truewidth: 640height: 480title: qsTr(Hello World)//qml1MyButton{onClicked:{console.log(aaaa)}}//qml2Button{x: 300y: 200text: B button}
}
QML Row: Cannot specify left, right, horizontalCenter, fill or centerIn anchors for items inside Row
Row中子元素按照控件属性由左向右分布通过spacing控制子元素间距因此通过anchor.left和anchor.right控制Row中子元素的水平位置会发生冲突但仍然可以通过anchor.top和anchor.bottom控制子元素的垂直分布。如果希望用anchor进行定位就用Item替换Row。
原文链接https://blog.csdn.net/weixin_43692244/article/details/103686333
qml 虚线边框绘制 调整参数实现设计要求的效果
Rectangle{width: 400height: 100anchors.centerIn: parentColumn{width:1height:parent.heightspacing: 5clip: trueanchors.left: parent.leftRepeater{anchors.fill: parentmodel: parent.height/10delegate:Rectangle{width: 1height: 10color: black}}}Column{width:1height:parent.heightspacing: 5clip: trueanchors.right: parent.rightRepeater{anchors.fill: parentmodel: parent.height/10delegate:Rectangle{width:1height: 10color: black}}}Row{width: parent.widthheight: 1spacing: 5clip: trueanchors.top: parent.topRepeater{anchors.fill: parentmodel: parent.width/10delegate:Rectangle{width: 10height: 1color: black}}}Row{width: parent.widthheight: 1spacing: 5clip: trueanchors.bottom: parent.bottomRepeater{anchors.fill: parentmodel: parent.width/10delegate:Rectangle{width: 10height: 1color: black}}}
}
参考https://blog.csdn.net/zs1123/article/details/124948191
关闭文件系统视图 消息提示框 MessageDialog {id: dialogtitle: May I have your attention pleasetext: Its so cool that you are using Qt Quick.onAccepted: {console.log(And of course you could only agree.)Qt.quit()}Component.onCompleted: visible true
}Popup
closePolicy : enumeration 此属性决定弹出窗口关闭的情况 Popup.NoAutoClose Popup 只会在手动指示时关闭。 Popup.CloseOnPressOutside当鼠标在其外部按下时 Popup 将关闭。 Popup.CloseOnPressOutsideParent当鼠标在其父级之外按下时 Popup 将关闭。 Popup.CloseOnReleaseOutside当鼠标离开 Popup 时 Popup 将关闭。 Popup.CloseOnReleaseOutsideParent当鼠标在其父级之外释放时 Popup 将关闭。 Popup.CloseOnEscape当 Popup 具有活动焦点时按下退出键 Popup 将关闭。
modal : bool确定弹出窗口是否是模态的 dim : bool显示弹出窗口是否使背景变暗 Overlay弹出窗口覆盖
Dialog相关
属性基本与Pupup一致 1.去掉默认的边框 解决加入background属性里面为空也可 Dialog {id: wanGatewayApplywidth: 259//控制整个弹窗的大小height: 160x: (bodyRect.width - width) / 2 // 水平居中y: (bodyRect.height - height) / 2 -100// 垂直居中modal: true// title: Title //标题// standardButtons: Dialog.Ok | Dialog.Cancel //按钮background: Rectangle{
// implicitWidth: 100
// implicitHeight: 100
// anchors.centerIn: parent
// border.color: #fff}onAccepted: console.log(Ok clicked)onRejected: console.log(Cancel clicked)Rectangle{anchors.fill: parentcolor: #fff}}如何给下边框设置阴影效果
gpt搜索的
import QtQuick 2.15
import QtGraphicalEffects 1.15Rectangle {width: 200height: 40color: #FFFFFFRectangle {width: parent.widthheight: 1color: grayDropShadow {anchors.bottom: parent.bottomcolor: #000000radius: 5samples: 10horizontalOffset: 0verticalOffset: 0}}
}实践
import QtQuick 2.15
//import QtQuick.Controls 2.15
import QtGraphicalEffects 1.15//边框效果一般用这个
//************************************************ 顶部区域
Rectangle {id: headerBoxcolor: #fffwidth: parent.widthheight: 60Image {id: mLogox: 55width: 139height: 60source: images/mLogo.png}
}
// 顶部-外部阴影-----于要操作的组件同级
DropShadow {anchors.fill: headerBoxhorizontalOffset: 0verticalOffset: 3radius: 8.0samples: 16color: #DCDCDCsource: headerBox
}
Qt Design Studio 4.2.0怎么引入Calendar组件并使用Calendar 要在 Qt Design Studio 4.2.0 中引入 Calendar 组件并使用它你需要按照以下步骤进行操作步骤 1创建新项目
1.打开 Qt Design Studio。
2.在“选择模板”对话框中选择适合你需求的项目模板并创建一个新项目。步骤 2导入 Calendar 组件
1.在项目资源管理器中右键单击 Imports 文件夹选择 Add Existing File添加现有文件。
2.在文件选择对话框中导航到你的 Qt 安装目录中的 qml/QtQuick/Controls.2 文件夹。
3.选择 Calendar.qml如果有 CalendarStyle.qml也选择它文件并导入到项目中。步骤 3使用 Calendar 组件
1.在 MainWindow.qml 或你希望使用 Calendar 的 QML 文件中添加以下 import 语句
import QtQuick.Controls 2.15
2.在该文件中使用以下代码来创建和使用 Calendar 组件
Rectangle {width: 400height: 300Calendar {id: calendaranchors.centerIn: parent}
}现在你已经成功在 Qt Design Studio 4.2.0 中引入并使用了 Calendar 组件。你可以根据需要自定义和配置 Calendar 组件的属性和行为。如果你需要更复杂的操作例如处理日期选择事件或更改样式请参考官方的 Qt Quick Controls 2 文档和示例以获取更多关于 Calendar 组件的使用信息。引入QtQuick.Controls.Styles 2.15显示module not found
import QtQuick 2.15
import QtQuick.Controls 2.15
import WinIFDesigner 1.0
import Qt5Compat.GraphicalEffects
import Qt.labs.qmlmodels 1.0
import QtQuick.Layouts 1.11//import QtQuick.Controls 1.6
//import QtQuick.Controls.Styles 1.4
import QtQuick.Controls.Styles 2.15Qt Design Studio 4.2.0 并没有引入 QtQuick.Controls.Styles 模块。QtQuick.Controls.Styles 是一个用于自定义 Qt Quick Controls 2 外观的模块但它不是 Qt Design Studio 的一部分。 查询结果
achors.rightMargin失效
1.Rectangle,Image 结合Image 距离右侧设置距离 解决 anchors.right: parent.right anchors.rightMargin: 8 同时使用
Rectangle{width: 212height: 32anchors.verticalCenter: parent.verticalCenterborder.color: #D9D9D9TextInput {id: textInputx:8anchors.verticalCenter: parent.verticalCenterverticalAlignment: Text.AlignVCentertext: qsTr(2023-5-20 103025)font.pointSize: 10}Image {id: startDatesource: ./images/dateanchors.verticalCenter: parent.verticalCenteranchors.right: parent.rightanchors.rightMargin: 8}
}2.Rectangle,Text 结合text距离右侧设置距离
Rectangle{id: apnNamewidth: 76height: 32border.color: #333Text {text: qsTr(APN)anchors.fill: parentverticalAlignment: Text.AlignVCenterhorizontalAlignment:Text.AlignRightanchors.rightMargin: 20color: #333font.family: PingFang SCfont.pointSize: 10}
}label Label {text: Hello worldfont.pixelSize: 22font.italic: true}
下拉框
可参考 https://blog.csdn.net/tanxuan231/article/details/124990398 https://www.5axxw.com/questions/content/3xqy76 ComboBox {textRole: keymodel: ListModel {ListElement { key: First; value: 123 }ListElement { key: Second; value: 456 }ListElement { key: Third; value: 789 }}}自定义下拉框 import QtQuickimport QtQuick.ControlsComboBox {id: controlmodel: [First, Second, Third]delegate: ItemDelegate {width: control.widthcontentItem: Text {text: control.textRole? (Array.isArray(control.model) ? modelData[control.textRole] : model[control.textRole]): modelDatacolor: #21be2bfont: control.fontelide: Text.ElideRightverticalAlignment: Text.AlignVCenter}highlighted: control.highlightedIndex index}indicator: Canvas {id: canvasx: control.width - width - control.rightPaddingy: control.topPadding (control.availableHeight - height) / 2width: 12height: 8contextType: 2dConnections {target: controlfunction onPressedChanged() { canvas.requestPaint(); }}onPaint: {context.reset();context.moveTo(0, 0);context.lineTo(width, 0);context.lineTo(width / 2, height);context.closePath();context.fillStyle control.pressed ? #17a81a : #21be2b;context.fill();}}contentItem: Text {leftPadding: 0rightPadding: control.indicator.width control.spacingtext: control.displayTextfont: control.fontcolor: control.pressed ? #17a81a : #21be2bverticalAlignment: Text.AlignVCenterelide: Text.ElideRight}background: Rectangle {implicitWidth: 120implicitHeight: 40border.color: control.pressed ? #17a81a : #21be2bborder.width: control.visualFocus ? 2 : 1radius: 2}popup: Popup {y: control.height - 1width: control.widthimplicitHeight: contentItem.implicitHeightpadding: 1contentItem: ListView {clip: trueimplicitHeight: contentHeightmodel: control.popup.visible ? control.delegateModel : nullcurrentIndex: control.highlightedIndexScrollIndicator.vertical: ScrollIndicator { }}background: Rectangle {border.color: #21be2bradius: 2}}}
.ui.qml不支持function
鼠标响应事件
例子
MouseArea {anchors.fill: parentacceptedButtons: Qt.LeftButton | Qt.RightButtononClicked: function(mouse){if (mouse.button Qt.RightButton)contextMenu.popup()}onPressAndHold: function(mouse){if (mouse.source Qt.MouseEventNotSynthesized)contextMenu.popup()}Menu {id: contextMenuMenuItem { text: Cut }MenuItem { text: Copy }MenuItem { text: Paste }}}参考https://blog.csdn.net/qq_41895747/article/details/104597293
Menu介绍
参考https://blog.csdn.net/hw5230/article/details/131191517
自定义菜单组件官方文档 import QtQuickimport QtQuick.ControlsMenu {id: menuAction { text: qsTr(Tool Bar); checkable: true }Action { text: qsTr(Side Bar); checkable: true; checked: true }Action { text: qsTr(Status Bar); checkable: true; checked: true }MenuSeparator {contentItem: Rectangle {implicitWidth: 200implicitHeight: 1color: #21be2b}}Menu {title: qsTr(Advanced)// ...}topPadding: 2bottomPadding: 2delegate: MenuItem {id: menuItemimplicitWidth: 200implicitHeight: 40arrow: Canvas {x: parent.width - widthimplicitWidth: 40implicitHeight: 40visible: menuItem.subMenuonPaint: {var ctx getContext(2d)ctx.fillStyle menuItem.highlighted ? #ffffff : #21be2bctx.moveTo(15, 15)ctx.lineTo(width - 15, height / 2)ctx.lineTo(15, height - 15)ctx.closePath()ctx.fill()}}indicator: Item {implicitWidth: 40implicitHeight: 40Rectangle {width: 26height: 26anchors.centerIn: parentvisible: menuItem.checkableborder.color: #21be2bradius: 3Rectangle {width: 14height: 14anchors.centerIn: parentvisible: menuItem.checkedcolor: #21be2bradius: 2}}}contentItem: Text {leftPadding: menuItem.indicator.widthrightPadding: menuItem.arrow.widthtext: menuItem.textfont: menuItem.fontopacity: enabled ? 1.0 : 0.3color: menuItem.highlighted ? #ffffff : #21be2bhorizontalAlignment: Text.AlignLeftverticalAlignment: Text.AlignVCenterelide: Text.ElideRight}background: Rectangle {implicitWidth: 200implicitHeight: 40opacity: enabled ? 1 : 0.3color: menuItem.highlighted ? #21be2b : transparent}}background: Rectangle {implicitWidth: 200implicitHeight: 40color: #ffffffborder.color: #21be2bradius: 2}}
菜单组件Menu应用 MouseArea{id:moouseIdanchors.fill: parentonClicked: {dialog.open();}
}Menu {id: dialogy: moouseId.heightAction { text: Cut }Action { text: Copy }Action { text: Paste }MenuSeparator { }Menu {title: Find/ReplaceAction { text: Find Next }Action { text: Find Previous }Action { text: Replace }}
}或者用MenuItem
Button {id: fileButtontext: FileonClicked: menu.open()Menu {id: menuy: fileButton.heightMenuItem {text: New...}MenuItem {text: Open...}MenuItem {text: Save}}
}
点击事件出现Dialog弹框
但自带的边框去不掉暂没解决
Rectangle {id: addRectwidth: 82height: 32border.color: #4267B2color: #4267B2radius: 4Image {id: addImgsource: images/addWhite.pngwidth: 13height: 13x: 15anchors.verticalCenter: parent.verticalCenter}Text {id: addTexttext: qsTr(新建)x: 38font.pointSize: 10color: #fffanchors.verticalCenter: parent.verticalCenter}//点击MouseArea{anchors.fill: parentonClicked: {dialog.open();}}//弹窗内容Dialog {id: dialogx:0y:34
// implicitWidth: 185;
// implicitHeight: 326;width: 185;height: 326;modal: true;
// standardButtons: Dialog.Ok | Dialog.Cancel
// onAccepted: console.log(Ok clicked)
// onRejected: console.log(Cancel clicked)/* 内容样式 */Rectangle{id:dialogConanchors.fill: parentcolor: #fffColumn {spacing: 15Text {id: iconTextfont.pointSize: 10font.family: iconfonttext: 11111color: #333}Text {id: iconText2font.pointSize: 10font.family: iconfonttext: 2222color: #333}}}}
}弹框中modal属性 pointSize和pixelSize区别
font.pointSize: 14
font.pixelSize: 14通过设置PointSize的字体来说字体大小是随着屏幕大小以及分辨率自适应的无须处理字体的缩放
通过设置PixelSize的字体来说所占分辨率大小固定在相同尺寸上更高分辨率的屏幕上由于其单位长度内的像素点数更多即像素密度更大字体会看起来小一些.
要处理这种情况一种办法就是所有字体都用PointSize来表示大小但对于已经采用PixelSize的字体来说就要对其进行控制缩放。
通过qss来实现
PointSize: font-size:16pt;
PixelSize: font-size:16px;一直被字体自适应这个问题困扰尤其是使用pixelSize单位字体在不同的分辨率电脑上会显示不一样的效果有的大有的中等有的贼小这是因为使用pixelSize会导致字体给固定住多大就是大多不会自己变化遇到分辨率高的就变小低的就贼大刚好和设计原型差不多的就看着很漂亮。
而使用pointSize虽然会自己放大缩小但是和设计稿的字体差距很大而且也会存在相应问题而我更倾向于使用pixelSize既然它固定了我可以根据比例自己放大缩小即可所以本文重点就要解决的就是如何获取放大缩小的比例使像素字体无论再什么设备上都能看起来是中等好看的
思路 要想获取到字体的缩放因子就得知道宽高的原始像素对比现设备的原始像素分别得到不同的宽高比例值获取最小值这就是缩放比例因子然后乘以当前的字体大小就可以等比缩放字体了当然还可以增加一个缩放基础因子初始化为1这个需要人为的调整有的可能1不太好看需要调整到1.1就会更佳下面给出具体的获取代码 horizontalRatio 变化的U参考https://it.cha138.com/ios/show-6287196.html
表格自定义内容中delegate类型
delegate: CheckBox delegate: SpinBox delegate: TextField
delegate: DelegateChooser
{DelegateChoice{column: 0delegate: CheckBox{implicitWidth: header.width*0.1implicitHeight:40checked: model.displayonToggled: model.display checked}}DelegateChoice{column: 1delegate: SpinBox{implicitWidth: 140implicitHeight:40value: model.displayonValueModified: model.display value}}DelegateChoice{delegate: TextField{text: model.displayselectByMouse: trueimplicitWidth: 140implicitHeight:40onAccepted: model.display text}}
}三四五列没设置column默认一样
Rectangle隐藏下边框
Rectangle{width: parent.widthheight: 32color: #F5F5F5border.color: #D9D9D9// border.width: 1//隐藏下边框Rectangle{width: parent.widthheight: 1color: #F5F5F5anchors.bottom: parent.bottom}}表格只显示下边框 delegate: DelegateChooser
{DelegateChoice{column: 0delegate: Rectangle{id:checkBoxRectimplicitWidth: header.width*0.03implicitHeight:50border.width: 1border.color:#E8E8E8color:#fff
// CheckBox{
// anchors.centerIn: parent
// checked: model.display
// onToggled: model.display checked
// }//加这一层覆盖掉checkBoxRect内容Rectangle{anchors{fill: checkBoxRectbottomMargin: checkBoxRect.border.width}color: #fffCheckBox{anchors.centerIn: parentchecked: model.displayonToggled: model.display checked}}}}DelegateChoice{}.........}QML 布局详解
在QML中布局分为以下几种绝对坐标、锚布局、定位器、布局管理器。 1.设置x、y、width、height能够定位一个组件的位置这种方法扩展性太差。 Button{width: 50height: 50x: 10y:10background: Rectangle{color:red}}2.锚布局 3.定位器 Row和Column差不多
4.Flow流式布局 示例Flow项自动将子Text项并排放置当超出父控件边界时会自动换行。
5.Grid网格布局 可以控制绘制几行几列。
参考https://blog.csdn.net/wzz953200463/article/details/129718412
Rectangle
圆角属性radius 放发缩小scale 改变元素的原点位置transformOrigin: “TopLeft” 移动transform: Translate { y: -40 }//上移
Rectangle底部颜色和宽度
Rectangle {id: rectanchors.fill: parentborder.width: 5//底边宽度color: grayborder.color: red//底边颜色
}
Rectangle {anchors {fill: rectbottomMargin: rect.border.width}color: gray
}button
background属性 Button{id:b1width: 50height: 50anchors.centerIn: parent //在Window的中心background: Rectangle{color:red}}Button自定义 import QtQuickimport QtQuick.ControlsButton {id: controltext: qsTr(Button)contentItem: Text {text: control.textfont: control.fontopacity: enabled ? 1.0 : 0.3color: control.down ? #17a81a : #21be2bhorizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenterelide: Text.ElideRight}background: Rectangle {implicitWidth: 100implicitHeight: 40opacity: enabled ? 1 : 0.3border.color: control.down ? #17a81a : #21be2bborder.width: 1radius: 2}}树形结构
可参考下help中的查询文档
TreeView {id: myTreeanchors.fill: parentheaderVisible: falsebackgroundVisible : false //隐藏背景style: treeViewStylemodel: root.treeModelselection: selTableViewColumn {width: myTree.widthtitle: Namerole: name}itemDelegate: Item {Image {id: nodeTextImageanchors.verticalCenter: parent.verticalCentersource: }Text {id: nameTextanchors.left: nodeTextImage.rightanchors.leftMargin: 5anchors.verticalCenter: parent.verticalCentercolor: whiteelide: styleData.elideModetext: styleData.valuefont.pointSize: 16}Drag.active:itemMouse.drag.activeDrag.dragType: Drag.Automatic; //选择自动开始拖动Drag.supportedActions: Qt.CopyAction; //选择复制数据到DropAreaDrag.onDragFinished: { //拖拽结束}MouseArea {id: itemMouseanchors.fill: parenthoverEnabled: truedrag.target: nameTextproperty bool isExpand: true //点击展开树节点onClicked: {sel.setCurrentIndex(styleData.index, 0x0010) //点击Item设置选中当前节点if (isExpand) {emit: myTree.expand(styleData.index)isExpand false;mControl.updateNodeName(styleData.index)}else {emit: myTree.collapse(styleData.index)isExpand true;
边的阴影怎么实现
切记DropShadow要放在需要阴影模块的同级否者样式错乱
import Qt5Compat.GraphicalEffects
Rectangle {id: headerBoxcolor: #ffffffwidth: parent.widthheight: 60
}
//外部阴影
DropShadow {anchors.fill: headerBoxhorizontalOffset: 7verticalOffset: 3radius: 8.0samples: 16color: #b9b9b9source: headerBox
}// 顶部-内部阴影
InnerShadow {anchors.fill: headerBoxcached: truehorizontalOffset: 0verticalOffset: -3radius: 16samples: 32color: #666smooth: truesource: headerBox}参考 https://blog.csdn.net/luoyayun361/article/details/65428939 https://blog.csdn.net/Marrrrrco/article/details/125204965
TextInput的使用方法
参考https://baijiahao.baidu.com/s?id1707087196065011808wfrspiderforpc https://blog.csdn.net/hahaha113461/article/details/129723861
TextInput禁止输入模式
readOnly: trueTextInput文字居中方式
https://baijiahao.baidu.com/s?id1707087196065011808wfrspiderforpc TextInput{anchors.fill: parent//anchors.leftMargin: 8text: 192.168.222font.pointSize: 10font.family: PingFang SCverticalAlignment: Qt.AlignCenter //或者用verticalAlignment: TextInput.AlignVCenter//horizontalAlignment: Qt.AlignHCenter//或者用verticalAlignment: TextInput.AlignHCenter
}TextInput{anchors.fill: parentanchors.leftMargin: 8text: 192.168.222font.pointSize: 10font.family: PingFang SCverticalAlignment: Qt.AlignCenter //或者用verticalAlignment: TextInput.AlignVCenter//horizontalAlignment: Qt.AlignHCenter//或者用verticalAlignment: TextInput.AlignHCenter
}Text文字右对齐 Rectangle{id: ipNamewidth: 60height: 32border.color: blueText {text: qsTr(IP地址)anchors.fill: parentverticalAlignment: Text.AlignVCenterhorizontalAlignment:Text.AlignRight//anchors.rightMargin: 4}
}Text文字居中方式
1创建一个rectangle让text在rectangle中居中。
Window {width: 400height: 400title: qsTr(Hello World)visible: trueRectangle{anchors.centerIn: parentwidth: parent.width/2height: parent.height/2color:yellowText {id: nameanchors.centerIn: parenttext: qsTr(text)font.pixelSize: 30}}
}2只创建一个text设置对其方式。
Window {width: 400height: 400title: qsTr(Hello World)visible: trueText {width: parent.widthheight: parent.heightid: nametext: qsTr(text)font.pixelSize: 30horizontalAlignment: Text.AlignHCenter//(1)verticalAlignment: Text.AlignVCenter//(2)}
}参考https://blog.csdn.net/ipfpm/article/details/88687398
anchors.fill和anchors.centerIn区别
anchors.centerIn:parent,是将子控件放在父控件的正中心子控件的宽高是自己设置的anchors.fillparent, 是在子控件的大小设置与父控件大小一样特别是mouseArea中经常使用anchors.fillparent这是为了指定鼠标事件接受的范围。如果是两个矩形控件颜色不同那么子控件会完全覆盖父控件全是子控件的颜色显示。
还有一点就是parent是指父类直接相关的父类。anchors.fill指定的父类是必须是直接的父类如果不是会报错的。
参考https://blog.csdn.net/lichen18848950451/article/details/79185611
自定义组件
新建一个自定义文件文件名为Button.qml
import QtQuick 2.0
Rectangle {id: rootproperty alias text: label.textsignal clickedwidth: 116; height: 26color: lightsteelblueborder.color: slategreyText {id: labelanchors.centerIn: parenttext: Start}MouseArea {anchors.fill: parentonClicked: {root.clicked()}}
}下面我们需要修改 main.qml 来使用这个组件
import QtQuick 2.0Rectangle {width: 360height: 360Button {id: buttonx: 12; y: 12text: StartonClicked: {status.text Button clicked!}}Text {id: statusx: 12; y: 76width: 116; height: 26text: waiting ...horizontalAlignment: Text.AlignHCenter}
}在 main.qml 中我们直接使用了Button这个组件就像 QML 其它元素一样。由于 Button.qml 与 main.qml 位于同一目录下所以不需要额外的操作。但是如果我们将 Button.qml 放在不同目录比如构成如下的目录结果
app|- QML| |- main.qml|- components|- Button.qml那么我们就需要在 main.qml 的import部分增加一行import …/components才能够找到Button组件。
有时候选择一个组件的根元素很重要。比如我们的Button组件。我们使用Rectangle作为其根元素。Rectangle元素可以设置背景色等。但是有时候我们并不允许用户设置背景色。所以我们可以选择使用Item元素作为根。事实上Item元素作为根元素会更常见一些。 参考https://www.bookstack.cn/read/qt-study-road-2/15edfd7a031f980e.md
自定义属性
// 自定义属性
property int times: 24
// 属性别名
property alias anotherTimes: times
//声明一个默认属性
default property var defaultText参考https://www.bookstack.cn/read/qt-study-road-2/ffdc17d45ca95b72.md
Item
Item是QML中最基本的元素所有其他元素都继承自它。它代表了一个可视化的对象可以在界面上显示出来。
Item的属性包括位置、大小、旋转角度、缩放比例等。还可以设置颜色、透明度、边框等外观属性。此外Item还可以响应用户输入事件例如鼠标点击、键盘按键等。
在QML中通常使用Item作为容器来组织其他元素例如Rectangle、Text等。可以将多个Item嵌套在一起形成复杂的界面布局。
下面是一个简单的例子展示了如何使用Item创建一个红色矩形
import QtQuick 2.0
Item {width: 100height: 100color: red
}参考https://blog.csdn.net/m0_60259116/article/details/130270827
anchors详解
https://baijiahao.baidu.com/s?id1706546262009663031wfrspiderforpc https://zhuanlan.zhihu.com/p/477876610?utm_id0
什么时候用Item什么时候用Rectangle什么时候用Row什么时候用RowLayout
什么时候用Item就是你要做一个组件这个组件是一个复合的组件组件有部分是有显示项。这种情况下就用Item。如果你的元素是作为一个整体以后不需要变动和更改那么就用Rectangle。
什么时候用Row布局当布局里面的所有元素的尺寸宽高都明确的时候用Row布局你可以精确细化到每个元素的宽高。 RowLayout布局自带了一个自适应的布局方式比如填充满然后比如元素的比例。如果一个行中只有部分元素的尺寸是已知的那么可以考虑用此布局。
参考https://www.cnblogs.com/xianqingzh/p/4569176.html
lable用法
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5Window {visible: truewidth: 640height: 480title: qsTr(Hello World)Label{x:100y:100width: 100height: 50text: qsTr(我是标签) //可被翻译color: red //设置Label字体颜色font.pixelSize: 22 //字体大小font.italic: true //斜体//字体对其方式horizontalAlignment:Text.AlignHCenteverticalAlignment:Text.AlignVCenter//设置背景颜色background: Rectangle{color: gray}}
}Column
Column可以将它的子元素排列成一列 1.使用 2.使用spacing设置Column内部元素之间的间隔。 3.设置Column子元素四周的空白使用leftPadding、topPadding、rightPadding和bottomPadding。 4.除了以上属性,Column元素还有三个属性的值是Transition(过渡)这三个属性是populate、add、和move。 它们分别用来设置Column子元素创建时、添加时和移动时的过渡效果添加动画。 参考https://baijiahao.baidu.com/s?id1706627580638269232wfrspiderforpc
Qt中font.pointSize和font.pixelSize的区别
pixel size 是所占的像素大小 这样有一个缺点 有些显示器的分辨率(dpi)比较大 那么单位长度中的像素点就比较多 这样一个字所占的长度就会比较少
point size 则规定了实际中我们肉眼看到的字体的大小 他和pixel无关的他和显示器无关 不管在什么样上的显示器上 规定大小是多少就是多少。 注意 1、当font.pointSize和font.pixelSize同时指定时font.pixelSize生效。 2、在移动平台建议使用font.pointSize。 因为像Android智能手机屏幕尺寸和像素多种多样使用font.pointSize可以更好的适应屏幕在不同屏幕上看起来大小比较正常。而使用pixelSize可能会导致有的手机上看不见有些手机上特别大的情况。
参考https://blog.csdn.net/bzs2510568513/article/details/120056541
Qt中fileMode
背景图片设置
Image {id: imageBgfillMode: Image.PreserveAspectFitsource: images/loginBg.pngwidth: parent.widthheight: parent.height
}Image.Stretch图片拉伸自适应默认的
Image.PreserveAspectFit按比例缩放不裁剪
Image.PreserveAspectCrop均匀缩放必要时裁剪
Image.Tile像贴瓷砖一样
Image.TileVertically水平拉伸垂直平铺
Image.TileHorizontally垂直拉伸水平平铺
Image.Pad原始图像不做处理参考https://blog.csdn.net/qq78442761/article/details/89891547
隐藏虚拟键盘
在WinIFDesigner.qmlproject文件中注释下面两行代码即可
qt design studioA开发工具中 新建的.ui.qml中引入ColumnLayout后无法启动【Run Project】
加入下面一句代码后可以显示无法启动暂未知原因 import QtQuick.Layouts 5.15
代码如下
import QtQuick 2.15
import QtQuick.Controls 2.15
import WinIFDesigner 1.0
import QtQuick.Layouts 5.15//可以显示无法启动Rectangle {width: Constants.widthheight: Constants.heightcolor: #F0F2F5ColumnLayout {id: columnLayout// anchors.fill: parentwidth: parent.widthheight: parent.heightspacing: 0Rectangle {id: rectanglewidth: parent.widthheight: 60color: #ffffff}}
}用Column布局可以正常运行但是加上以下四句运行失效
//anchors.top: 60//有以下四行不能运行注释后正常 //anchors.bottom: 0 //anchors.right: 0 //anchors.left: 0
import QtQuick 2.15
import QtQuick.Controls 2.15
import WinIFDesigner 1.0
Rectangle {width: Constants.widthheight: Constants.heightcolor: #F0F2F5Column {id: columnanchors.fill: parentRectangle {id: rectanglewidth: parent.widthheight: 60color: #ffffff}Rectangle {id: rectangle2width: parent.widthheight: parent.height - 60//anchors.top: 60//有以下四行不能运行注释后正常//anchors.bottom: 0//anchors.right: 0//anchors.left: 0}}
}报错module “QtQuick.Layouts” version 5.15 is not installed
拖拽组件默认是import QtQuick.Layouts 5.15 将版本改为1.11即可
//import QtQuick.Layouts 5.15
import QtQuick.Layouts 1.11参考官方文档https://doc.qt.io/qt-5/qtquicklayouts-overview.html
qt design studio中用ColumnLayout失效用Column正常显示【没法用ColumnLayoutRowLayout】
引入版本问题import QtQuick.Layouts 1.11 可以正常显示
qt design studio的2D预览和运行不一致的问题2D预览可能不准确
报错Parameter “mouse” is not declared. Injection of parameters into signal handlers is deprecated. Use JavaScript functions with formal parameters instead.
原因自Qt6开始QML中使用信号处理函数参数时需要显式捕获参数写法如下 原来
onClicked:{if (mouse.button Qt.RightButton)contextMenu.popup()
}改为
onClicked: function(mouse){if (mouse.button Qt.RightButton)contextMenu.popup()
}报错QML Row: Cannot specify left, right, horizontalCenter, fill or centerIn anchors for items inside Row. Row will not function.
Row 有次报错可用Item代替 Row中子元素按照控件属性由左向右分布通过spacing控制子元素间距因此通过anchor.left和anchor.right控制Row中子元素的水平位置会发生冲突但仍然可以通过anchor.top和anchor.bottom控制子元素的垂直分布。如果希望用anchor进行定位就用Item替换Row。
参考https://blog.csdn.net/weixin_43692244/article/details/103686333
报错QML Column: Cannot specify top, bottom, verticalCenter, fill or centerIn anchors for items inside Column. Column will not function.
Column正在尝试将MouseArea锚定到Image和Text旁边但MouseArea指定其锚点来填充其父级。 这两件事是矛盾的因此你得到错误的原因。 如果将MouseArea移出Column并进入SmileyDelegate的基本Item您可能会发现它的效果与预期的一样。
Column is trying to anchor your MouseArea to next to the Image and the Text, but the MouseArea specifies its anchor to fill its parent. Those two things are contradictory, and hence why you get the error. You’ll probably find it works as you expect if you move the MouseArea out of Column and into the base Item for SmileyDelegate. 参考https://m.656463.com/wenda/cwQMLlwfwColumnndxzdtopbottomver_283
自己写的登录页面的例子
用column排列子元素为Rectangle可以布局但是间隔只能设置一样的暂时找不到解决方案 所以本例子中没有用column用的父元素为Rectangle 。
/*
This is a UI file (.ui.qml) that is intended to be edited in Qt Design Studio only.
It is supposed to be strictly declarative and only uses a subset of QML. If you edit
this file manually, you might introduce QML code that is not supported by Qt Design Studio.
Check out https://doc.qt.io/qtcreator/creator-quick-ui-forms.html for details on .ui.qml files.
*/
import QtQuick 2.15
import QtQuick.Controls 2.15
import WinIFDesigner 1.0
import Qt5Compat.GraphicalEffectsRectangle {id: rectangleBoxwidth: Constants.widthheight: Constants.heightcolor: Constants.backgroundColor// 背景图片Image {id: imageBgfillMode: Image.PreserveAspectFitsource: images/loginBg.pngwidth: parent.widthheight: parent.height}// 登录页面Rectangle {id: loginRectBoxwidth: 448height: 324anchors.horizontalCenter: parent.horizontalCenteranchors.verticalCenter: parent.verticalCentercolor: #ffffffItem {id: loginTitleheight: 90width: parent.widthText {id: loginTitleTextanchors.horizontalCenter: parent.horizontalCenteranchors.verticalCenter: parent.verticalCentertext: qsTr(WinIFDesigner)font.bold: truefont.pointSize: 20color: #4267B2font.family: PingFang SC}}Rectangle {id: loginContx: 0y: 92width: parent.widthheight: parent.height - 92// 账号Rectangle {id: userContwidth: 380height: 48anchors.horizontalCenter: parent.horizontalCenterborder.color: #D9D9D9clip: true //clip是从Item那里继承下来的让绘制限定在它自己的矩形范围内。 失效Image {id: userNameImgx: 16source: images/userNum.pngwidth: 20height: 20anchors.verticalCenter: parent.verticalCenter}Text {id: userTextPlaceholder// visible: !(userInput.activeFocus || userInput.length 0)visible: !(userInput.length 0)anchors.fill: parentverticalAlignment: Text.AlignVCenterleftPadding: 50text: qsTr(请输入账号)font.family: PingFang SCfont.pixelSize: 16color: #999999}TextInput {id: userInputanchors.fill: parentleftPadding: 50verticalAlignment: TextInput.AlignVCenter //文字显示位置// text: adminfont.pointSize: 16focus: trueselectByMouse: true //鼠标可选中 -复制剪切// echoMode: TextInput.Password //密码形式 TextInput.Normal:显示文本TextInput.Password:密码TextInput.NoEcho啥也不显示TextInput.PasswordEchoOnEdit编辑时显示文本}}// 密码Rectangle {id: pswContwidth: 380height: 48y: 72anchors.horizontalCenter: parent.horizontalCenterborder.color: #D9D9D9clip: true //clip是从Item那里继承下来的让绘制限定在它自己的矩形范围内。 失效Image {id: pswImgx: 16source: images/userPsw.pngwidth: 20height: 20anchors.verticalCenter: parent.verticalCenter}Text {id: pswTextPlaceholdervisible: !(pswTextInput.length 0)anchors.fill: parentverticalAlignment: Text.AlignVCenterleftPadding: 50text: qsTr(请输入密码)font.family: PingFang SCfont.pixelSize: 16color: #999999}TextInput {id: pswTextInputanchors.fill: parentleftPadding: 50verticalAlignment: TextInput.AlignVCenter //文字显示位置// text: 123456font.pointSize: 16focus: trueselectByMouse: true //鼠标可选中 -复制剪切echoMode: TextInput.Password //密码形式 TextInput.Normal:显示文本TextInput.Password:密码TextInput.NoEcho啥也不显示TextInput.PasswordEchoOnEdit编辑时显示文本}}Rectangle {id: loginBtnContwidth: 380height: 48anchors.horizontalCenter: parent.horizontalCenterborder.color: #D9D9D9y: 160color: #4267B2Text {id: loginBtnTexttext: qsTr(登录)font.pointSize: 20anchors.horizontalCenter: parent.horizontalCenteranchors.verticalCenter: parent.verticalCentercolor: #fff}}}}// 边框阴影DropShadow {anchors.fill: rectangle1horizontalOffset: 7verticalOffset: 3radius: 8.0samples: 16color: #80000000source: rectangle1}
}