Qt Quick快速入门笔记

news2025/6/6 13:34:02

Qt Quick快速入门笔记

  • 基本的程序结构
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
  	// 引擎加载qml
    engine.load(url);

    return app.exec();
}
import QtQuick 2.12
import QtQuick.Window 2.12

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

查看qml渲染效果

选中对应qml-> 顶部工具 ->外部-> Qt Quick -> Qt Qucik Preview

基础组件体验

Item

Item是Qt Quick所有可视元素的基类,定义了大部分属性,包括。x,y,z,width,height,anchors,按键响应等。可以将多个Item嵌套在一起形成复杂界面布局.z数值越大,越在图层上面

Item {
   
   focus: true
   // 回车按键响应
   Keys.onReturnPressed: console.log("Pressed return");
}

Rectangle

矩形区域,提供颜色、边框、圆角属性等

Rectangle {
   
     width: 100
     height: 100
     color: "red"
     // color: Qt.rgba(0.4,0.5,0.8,1)
     border.color: "black"
     border.width: 5
     radius: 10
     anchors.centerIn: parent
     Component.onCompleted:
     {
   
         console.log("Rectangle Completed!")
     }
 }

anchors 锚布局

指定控件与其他控件之间的位置关系centerIn、left、right、top、bottom、horizontalCenter、verticalCenter等

Rectangle {
   
    id: test
     width: 100
     height: 100
     color: Qt.rgba(0.4,0.5,0.8,1)
     border.color: "black"
     border.width: 5
     radius: 10
     anchors.centerIn: parent
 }

Button {
   
  // Button 左边靠着Rectangle 间隔20,顶部y持平
    anchors.left: test.right
    anchors.leftMargin: 20
    anchors.top: test.top
    text: "Ok"
    onClicked: console.log("click button")
}

Text

Text支持多种文本格式html、markdown

Text {
   
  text: "Hello World!"
  font.family: "Helvetica"
  color: "red"
   // 字体自适应
  fontSizeMode: Text.Fit
  font.pixelSize:  50 // 字体最大尺寸
  minimumPixelSize: 12 // 字体最小尺寸
}

Label

Label
{
   
    id: username
    font.pixelSize: 22
    font.italic: true
    color: "steelblue"
    text: "账号:"
}

TextField

TextField
{
   
    placeholderText: "请输入用户名"
    x: 10
    y: 10
    background: Rectangle
    {
   
        implicitHeight: 40
        implicitWidth: 200
        color: "red"
        border.color: "black"
    }
    // 密码样式
    echoMode: TextInput.Password
}

TextInput

TextInput与TextField类似

TextInput没有背景是透明的

TextField有背景色

MouseArea {
   
  anchors.fill: parent
  onClicked: () => {
   
    // 获取验证结果
     console.log("result = ", testValidator.acceptableInput)
  }
}

TextInput
{
   
  id: testValidator
  x: 10
  y: 10
  width: 100
  height: 30
  // 验证输入
  validator: IntValidator{
    bottom: 100; top: 999}
}

TextEdit

MouseArea {
   
    anchors.fill: parent
    onClicked: () => {
   
       console.log("selectionStart = ", edit.selectionStart)
       console.log("selectionEnd = ", edit.selectionEnd)
       console.log("selectedText = ", edit.selectedText)
       
       // 搜索文本
       var tex = edit.getText(0, edit.text.length)
       console.log("tex = ", tex)
       let index = tex.indexOf("el")
       console.log("index = ", index)
    }
}

TextEdit {
   
    id: edit
    width: 240
    text: "<b>Hello</b> <i>World!</i>"
    font.family: "Helvetica"
    font.pointSize: 20
    color: "blue"
    focus: true
    // persistentSelection: true
}

TextArea

TextArea
{
   
 x: 10
 y: 20
 background: Rectangle
 {
   
     implicitWidth: 100
     implicitHeight: 80
     border.color: "blue"
     border.width: 2
 }

 placeholderText: "请输入多行文本"
 font.pixelSize: 18
 color: "red"
}

// 滚动条
ScrollView {
   
    width: 100
    height: 200
    TextArea
    {
   
     x: 10
     y: 20
     background: Rectangle
     {
   
         implicitWidth: 100
         implicitHeight: 80
         border.color: "blue"
         border.width: 2
     }

     placeholderText: "请输入多行文本"
     font.pixelSize: 18
     color: "red"
    }
}

Button

Button系列包括:CheckBox(勾选框)、DelayButton(延时按钮)、RadioButton(单选框)、RoundButton(圆形按钮)、Switch(开关)、ToolButton

Button {
   
    anchors.centerIn: parent
    text: "Ok"
    onClicked: console.log("click button")
}

// buttonStyle案例
Component {
   
    id: mystyle

    // 自定义按钮外观
    ButtonStyle {
   
        background: Rectangle {
   
            implicitHeight: 45
            implicitWidth: 150
            radius: 10
            border.width: 2
            border.color: "red"
            //color: "blue"
        }
    }
    
     Button {
   
        text: "测试数据"
        style: mystyle
    }
}

CheckBox

// CheckBox 示例
Rectangle
{
   
    width: 150
    height: 100
    color: "yellow"
    border.color: "orange"
    border.width: 2

    CheckBox {
   
        x: 0
        y: 0
        text: "JavaScript"
        onCheckedChanged:
        {
   
            console.log("status = ", checked)
        }
    }

    CheckBox {
   
        x: 0
        y: 40
        text: "java"
    }
}


// 互斥单选box
ButtonGroup
{
   
    id: testButtonGroup
    exclusive: true
}
CheckBox {
   
  ButtonGroup.group: testButtonGroup
  x: 0
  y: 0
  text: "JavaScript"
}

CheckBox {
   
  ButtonGroup.group: testButtonGroup
  x: 0
  y: 40
  text: "java"
}

Repeater

Column
{
   
    spacing: 10
    width: 100
    height: 400
    anchors.centerIn: parent

    Repeater
    {
   
        id: testRepeater
        model: 5
        property var titles: ["ID:", "姓名", "班级"]
        property var values: ["02220455", "张三", "6班"]

        Row
        {
   
            spacing: 3
            Text {
   
                text: testRepeater.titles[index]
                color: "black"
                font: {
   
                    bold: true
                    pointSize: 16
                }
            }

            Text {
   
                text: testRepeater.values[index]
                color: "red"
                font: {
   
                    bold: true
                    pointSize: 16
                }
            }
        }
    }
}

GroupBox

GroupBox用于组织和分组其他控件的容器

Item {
   
    width: 300
    height: 200
    anchors.centerIn: parent

    GroupBox
    {
   
        title: "groupbox"
        anchors.centerIn: parent
        width: 250
        height: 180

        CheckBox {
   
            x: 0
            y: 0
            text: "JavaScript"
        }

        CheckBox {
   
            x: 0
            y: 40
            text: "java"
        }
    }
}

RadioButton

Item {
   
    width: 300
    height: 200
    anchors.centerIn: parent

    GroupBox
    {
   
        title: "groupbox"
        anchors.centerIn: parent
        width: 250
        height: 180

        RadioButton {
   
            x: 0
            y: 0
            text: "JavaScript"
        }

        RadioButton {
   
            x: 0
            y: 40
            text: "java"
        }
    }
}

Slider

Rectangle {
   
    width: 100
    height: 500

    Slider {
   
        id: slider
        from: 1
        value: 25
        to: 100
    }

    MouseArea
    {
   
        anchors.fill: parent
        onClicked: () => {
   
             console.log("value = " , slider.value.toFixed(2))
          	// position 范围是0-1
             console.log("position = " , slider.position)
         }
    }
}

Image

// 加载本地图片
Image {
   
    asynchronous: true
    source: "qrc:/03.png"  // qrc中的图片资源
}

// 充满父视图的背景
Image {
   
      anchors.fill: parent
      source: ":/test/333.jpg"  // qrc中的图片资源
  }

// 网络图片
Image {
   
  	id: imageView
    width: 100
    height: 40
    asynchronous: true
    source: "http://www.baidu.com/img/pcdoodle_2a77789e1a67227122be09c5be16fe46.png"
    //fillMode: Image.PreserveAspectCrop
    onStatusChanged:
    {
   
        console.log("imageView statu = ", imageView.status)
    }
}

BusyIndicator

// 加载器
// 图片还在加载的时候显示加载器
BusyIndicator {
   
     running: imageView.status === Image.Loading
     anchors.centerIn: parent
 }

Image {
   
    id: imageView
    width: 100
    height: 40
    asynchronous: true
    source: "http://www.baidu.com/img/pcdoodle_2a77789e1a67227122be09c5be16fe46.png"
    //fillMode: Image.PreserveAspectCrop
    onStatusChanged:
    {
   
        console.log("imageView statu = ", imageView.status)
    }
}

FileDialog

FileDialog {
   
    id: fileDialog
    title: "Please choose a file"
    folder: shortcuts.home
    onAccepted: {
   
        console.log("You chose: " + fileDialog.fileUrls)

    }
    onRejected: {
   
        console.log("Canceled")
    }
}

Button
{
   
    text: "打开"
    onClicked: fileDialog.visible = true
}

ComboBox

ComboBox {
   
  	// 默认选择
    currentIndex: 2
    displayText: "语言: " + currentText
    model: ["java", "javaScript", "C++"]
}

ComboBox {
   
    id: box
    anchors.centerIn: parent
    textRole: "key"
    valueRole: "value"
    model: ListModel
    {
   
        id: modelData
        ListElement {
   
            key: "first"; value: 1
        }
        ListElement {
   
            key: "second"; value: 2
        }
        ListElement {
   
            key: "third"; value: 3
        }
    }
    onActivated: (index) => {
   
       print("onActivated ", index)
       print("currentIndex ", currentIndex)
       print("currentValue ", currentValue)
       print("currentText ", currentText)
   }
}

Button
{
   
    text: "删除"
    anchors.top: box.bottom
    anchors.topMargin: 10
    anchors.horizontalCenter: parent.horizontalCenter
    onClicked:
    {
   
        modelData.remove(box.currentIndex, 1)
    }
}

ListView

Rectangle {
   
        id: topView
        width: 400
        height: 300
        color: "red"

        ScrollView
        {
   
            // 滚动条
            anchors.fill: parent
            ListView
            {
   
                id: listView
                anchors.fill: parent
                model:  ListModel
                {
   
                    id: listData
                    ListElement
                    {
   
                        name: "first"
                        number: "123"
                    }
                    ListElement
                    {
   
                        name: "second"
                        

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2399307.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Glide NoResultEncoderAvailableException异常解决

首先将解决方法提出来&#xff1a;缓存策略DiskCacheStrategy.DATA。 使用Glide加载图片&#xff0c;版本是4.15.0&#xff0c;有天发现无法显示gif图片&#xff0c;原始代码如下&#xff1a; Glide.with(context).load(本地资源路径).diskCacheStrategy(DiskCacheStrategy.A…

无人机巡检智能边缘计算终端技术方案‌‌——基于EFISH-SCB-RK3588工控机/SAIL-RK3588核心板的国产化替代方案‌

一、方案核心价值‌ ‌实时AI处理‌&#xff1a;6TOPS NPU实现无人机影像的实时缺陷检测&#xff08;延迟&#xff1c;50ms&#xff09;‌全国产化‌&#xff1a;芯片、操作系统、算法工具链100%自主可控‌极端环境适配‌&#xff1a;-40℃~85℃稳定运行&#xff0c;IP65防护等…

相机--相机成像原理和基础概念

教程 成像原理 基础概念 焦距&#xff08;物理焦距&#xff09; 镜头的光学中心到感光元件之间的距离&#xff0c;用f表示&#xff0c;单位&#xff1a;mm&#xff1b;。 像素焦距 相机内参矩阵中的 fx​ 和 fy​ 是将物理焦距转换到像素坐标系的产物&#xff0c;可能不同。…

2025-0604学习记录17——文献阅读与分享(2)

最近不是失踪了&#xff01;也不是弃坑了...这不是马上要毕业了嘛&#xff01;所以最近在忙毕业论文答辩、毕业去向填报、户档去向填报等等&#xff0c;事情太多了&#xff0c;没顾得上博客。现在这些事基本上都解决完了&#xff0c;也有时间静下心来写写文字了~ 想要写的内容…

图解浏览器多进程渲染:从DNS到GPU合成的完整旅程

目录 浅谈浏览器进程 浏览器进程架构的演化 进程和线程关系图示 进程&#xff08;Process&#xff09; 线程&#xff08;Thread&#xff09; 协程&#xff08;Coroutine&#xff09; 进程&线程&协程核心对比 单进程和多进程浏览器 单进程浏览器​编辑 单进程…

【计算机网络】第3章:传输层—TCP 拥塞控制

目录 一、PPT 二、总结 TCP 拥塞控制详解 ⭐ 核心机制与算法 1. 慢启动&#xff08;Slow Start&#xff09; 2. 拥塞避免&#xff08;Congestion Avoidance&#xff09; 3. 快速重传&#xff08;Fast Retransmit&#xff09; 4. 快速恢复&#xff08;Fast Recovery&…

idea不识别lombok---实体类报没有getter方法

介绍 本篇文章&#xff0c;主要讲idea引入lombok后&#xff0c;在实体类中加注解Data&#xff0c;在项目启动的时候&#xff0c;编译不通过&#xff0c;报错xxx.java没有getXxxx&#xff08;&#xff09;方法。 原因有以下几种 1. idea没有开启lombok插件 2. 使用idea-2023…

SAP学习笔记 - 开发15 - 前端Fiori开发 Boostrap,Controls,MVC(Model,View,Controller),Modules

上一章讲了Fiori开发的准备&#xff0c;以及宇宙至简之HelloWorld。 SAP学习笔记 - 开发14 - 前端Fiori开发 HelloWorld-CSDN博客 本章继续学习 Fiori 开发的知识&#xff1a; Bootstrap&#xff0c;Controls&#xff0c;MVC(Model&#xff0c;View&#xff0c;Controller&a…

基于SDN环境下的DDoS异常攻击的检测与缓解

参考以下两篇博客&#xff0c;最后成功&#xff1a; 基于SDN的DDoS攻击检测和防御方法_基于sdn的ddos攻击检测与防御-CSDN博客 利用mininet模拟SDN架构并进行DDoS攻击与防御模拟&#xff08;Ryumininetsflowpostman&#xff09;_mininet模拟dos攻击-CSDN博客 需求 H2 模拟f…

如何轻松地将文件从 PC 传输到 iPhone?

传统上&#xff0c;您可以使用 iTunes 将文件从 PC 传输到 iPhone&#xff0c;但现在&#xff0c;使用 iTunes 已不再是唯一的选择。现在有多种不同且有效的方法可以帮助您传输文件。在今天的指南中&#xff0c;您可以找到 8 种使用或不使用 iTunes 传输文件的方法&#xff0c;…

Bresenham算法

一 Bresenham 绘直线 使用 Bresenham 算法&#xff0c;可以在显示器上绘制一直线段。该算法主要思想如下&#xff1a; 1 给出直线段上两个端点 &#xff0c;根据端点求出直线在X,Y方向上变化速率 &#xff1b; 2 当 时&#xff0c;X 方向上变化速率快于 Y 方向上变化速率&am…

【从GEO数据库批量下载数据】

从GEO数据库批量下载数据 1&#xff1a;进入GEO DataSets拿到所需要下载的数据的srr.list&#xff0c;上传到linux&#xff0c; 就可以使用prefetch这个函数来下载 2&#xff1a;操作步骤如下&#xff1a; conda 安装sra-tools conda create -n sra-env -c bioconda -c co…

day 44

使用DenseNet预训练模型对cifar10数据集进行训练 import torch import torch.nn as nn import torch.optim as optim from torchvision import datasets, transforms, models from torch.utils.data import DataLoader import matplotlib.pyplot as plt import os# 设置中文字体…

NER实践总结,记录一下自己实践遇到的各种问题。

更。 没卡&#xff0c;跑个模型休息好几天&#xff0c;又闲又急。 一开始直接套用了别人的代码进行实体识别&#xff0c;结果很差&#xff0c;原因是他的词表没有我需要的东西&#xff0c;我是用的医学文本。代码直接在github找了改的&#xff0c;用的是BERT的Chinese版本。 然…

微信小程序实现运动能耗计算

微信小程序实现运动能耗计算 近我做了一个挺有意思的微信小程序&#xff0c;能够实现运动能耗的计算。只需要输入性别、年龄、体重、运动时长和运动类型这些信息&#xff0c;就能算出对应的消耗热量。 具体来说&#xff0c;在小程序里&#xff0c;性别不同&#xff0c;身体基…

iTunes 无法备份 iPhone:10 种解决方法

Apple 设备是移动设备市场上最先进的产品之一&#xff0c;但有些人遇到过 iTunes 因出现错误而无法备份 iPhone 的情况。iTunes 拒绝备份 iPhone 时&#xff0c;可能会令人非常沮丧。不过&#xff0c;幸运的是&#xff0c;我们有 10 种有效的方法可以解决这个问题。您可以按照以…

LangChain4J 使用实践

这里写目录标题 大模型应用场景&#xff1a;创建一个测试示例AIService聊天记忆实现简单实现聊天记录记忆MessageWindowChatMemory实现聊天记忆 隔离聊天记忆聊天记忆持久化 添加AI提示词 大模型应用场景&#xff1a; 创建一个测试示例 导入依赖 <dependency><groupI…

【C++】—— 从零开始封装 Map 与 Set:实现与优化

人生的态度是&#xff0c;抱最大的希望&#xff0c;尽最大的努力&#xff0c;做最坏的打算。 —— 柏拉图 《理想国》 目录 1、理论基石——深度剖析 BSTree、AVLTree 与 RBTree 的概念区别 2、迭代器机制——RBTree 迭代器的架构与工程实现 3、高级容器设计——Map 与 Set…

内网穿透之Linux版客户端安装(神卓互联)

选择Linux系统版本 获取安装包 &#xff1a;https://www.shenzhuohl.com/download.html 这里以Ubuntu 18.04为例&#xff0c;其它版本方法类似 登录Ubuntu操作系统&#xff1a; 打开Ubuntu系统终端&#xff0c;更新版本 apt-get update 安装运行环境&#xff1a; 安装C 运…

开疆智能Profinet转Profibus网关连接CMDF5-8ADe分布式IO配置案例

本案例是客户通过开疆智能研发的Profinet转Profibus网关将PLC的Profinet协议数据转换成IO使用的Profibus协议&#xff0c;操作步骤如下。 配置过程&#xff1a; Profinet一侧设置 1. 打开西门子组态软件进行组态&#xff0c;导入网关在Profinet一侧的GSD文件。 2. 新建项目并…