Qt Quick Hello World hacking

/*********************************************************************************************
 *                             Qt Quick Hello World hacking
 *  说明:
 *      本代码是Qt生成Quick应用程序的时候自动生成Hello World程序;
 *
 *                                  2015-5-17 深圳 晴 南山平山村 曾剑锋
 ********************************************************************************************/

                             \\\\\\\\\-*- 目录 -*-/////////
                             |     一、main.cpp
                             |     二、main.qml
                             |     三、MainForm.ui.qml
                             \\\\\\\\\\\\\\\//////////////

一、main.cpp
    #include <QApplication>
    #include <QQmlApplicationEngine>

    int main(int argc, char *argv[])
    {
        /**
         * The QApplication class manages the GUI application‘s control flow and main settings.
         *     初始化并配置GUI界面环境
         */
        QApplication app(argc, argv);

        /**
         * QQmlApplicationEngine provides a convenient way to load an application from a single QML file.
         *     创建QML引擎(engine)
         */
        QQmlApplicationEngine engine;
        /**
         * QStringLiteral: Creating a QString from it is free in this case, and the generated string data
         *     is stored in the read-only segment of the compiled object file.
         *     这是一个宏,用于创建一个字符串,该字符串存放在自读数据区
         * QUrl: The most common way to use QUrl is to initialize it via the constructor by passing a QString.
         *        Otherwise, setUrl() can also be used.
         *     最常用于初始化一个QUrl的是给其构造函数传一个字符串,此外也可以使用setUrl()
         * engine.load: Loads the root QML file located at filePath:.
         *     加载用QML引擎加载要显示的界面
         */
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

        /**
         * Enters the main event loop and waits until exit() is called.
         *     进入主事件循环,并等待直到exit()函数被调用
         */
        return app.exec();
    }

二、main.qml
    import QtQuick 2.4
    import QtQuick.Controls 1.3
    import QtQuick.Window 2.2
    import QtQuick.Dialogs 1.2

    /**
     * ApplicationWindow is a Window that adds convenience for positioning items, such as MenuBar, ToolBar,
     * and StatusBar in a platform independent manner.
     *     被main.cpp调用,显示的主窗口
     */
    ApplicationWindow {
        title: qsTr("Hello World")     //窗口的标题: Hello World
        width: 640                     //窗口宽:640(单位不知道)
        height: 480                    //窗口高:480(单位不知道)
        /**
         * Note: By default, an ApplicationWindow is not visible.
         *     窗口设为可见
         */
        visible: true

        /**
         * 为窗口添加菜单栏,我怎么感觉应该叫菜单条   :)
         */
        menuBar: MenuBar {
            /**
             * 观察这里,同一级别只有一个Menu,也就意味着这个菜单只有一个菜单选项
             */
            Menu {
                title: qsTr("&File")         //唯一的一个菜单的名字叫:File。你可以通过Alt+F快捷键来操作
                /**
                 * 观察这里,同一级别,有两个子菜单选项
                 */
                MenuItem {
                    text: qsTr("&Open")        //第一个子菜单的名字是:Open。你可以通过Alt+O快捷键来操作
                    /**
                     * 1. onTriggered: 是代表点击了一次Open这个子菜单
                     * 2. messageDialog.show: 代表会弹出信息框
                     * 3. qsTr: Wherever your script uses "quoted text" for text that will be presented to the user,
                     *     ensure that it is processed by the QCoreApplication::translate() function. Essentially
                     *     all that is necessary to achieve this is to use the qsTr() script function.
                     *     意思也就是说如果你要显示字符串,就用这个函数处理一下再显示的意思
                     */
                    onTriggered: messageDialog.show(qsTr("Open action triggered"));
                }
                MenuItem {
                    text: qsTr("E&xit")        //第一个子菜单的名字是:Exit。你可以通过Alt+E快捷键来操作
                    onTriggered: Qt.quit(); //退出
                }
            }
        }

        /**
         * 这里其实是去找同一级目录下的MainForm.ui.qml文件,而MainForm.ui.qml文件中必须是一个Item类型的控件
         */
        MainForm {
            anchors.fill: parent    //设置填充方式
            /**
             * 设置MainForm.ui.qml中3个按键的单击事件,弹一个信息框
             */
            button1.onClicked: messageDialog.show(qsTr("Button 1 pressed"))
            button2.onClicked: messageDialog.show(qsTr("Button 2 pressed"))
            button3.onClicked: messageDialog.show(qsTr("Button 3 pressed"))
        }

        MessageDialog {
            id: messageDialog    //消息对话框的变量名
            title: qsTr("May I have your attention, please?")  //信息框的标题

            /**
             * 我个人认为这是动态绑定一个函数,这个函数是显示函数(show)
             */
            function show(caption) {
                messageDialog.text = caption;    //信息框显示的内容
                messageDialog.open();            //让信息框显示
            }
        }
    }

三、MainForm.ui.qml
    import QtQuick 2.4
    import QtQuick.Controls 1.3
    import QtQuick.Layouts 1.1

    /**
     * All visual items in Qt Quick inherit from Item. Although an Item object has no visual appearance,
     * it defines all the attributes that are common across visual items, such as x and y position,
     * width and height, anchoring and key handling support.
     *
     * The Item type can be useful for grouping several items under a single root visual item.
     *     个人感觉Item相当于是一个容器
     * Rectangle items are used to fill areas with solid color or gradients, and/or to provide a rectangular border.
     *     这里个人尝试了将Item换成Rectangle,没什么很大的变化,能够正常的显示,不过还是建议使用Item,因为貌似Rectangle更适合做有边框效果的事。
     */
    Item {
        width: 640                             //宽:640(单位未知)
        height: 480                            //高:480(单位未知)

        /**
         * Property aliases are properties which hold a reference to another property.
         *     别名属性设置,并设置对应的值,主要是为了方便外部引用
         */
        property alias button3: button3
        property alias button2: button2
        property alias button1: button1

        /**
         * 这里采用列布局
         */
        RowLayout {
            anchors.centerIn: parent        //对齐方式:居中

            /**
             * 接下来放置3个按钮(Button)
             */
            Button {
                id: button1                    //按钮的变量名
                text: qsTr("Press Me 1")    //按钮的显示文字
            }

            Button {
                id: button2
                text: qsTr("Press Me 2")
            }

            Button {
                id: button3
                text: qsTr("Press Me 3")
            }

        }
    }
时间: 2025-01-18 22:52:41

Qt Quick Hello World hacking的相关文章

Qt5官方demo解析集33——Qt Quick Examples - Window and Screen

本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集32--Qt Quick Examples - Threading 来到我们Qt Quick Examples的第二个例子了,之所以挑这个demo,主要是我们使用Qt开发界面(尤其是跨平台界面)时,本地屏幕信息与窗口调用是不可避免的课题. 这个例子便向我们展示了在QML中获取本地屏幕信息的方法. 项目树如图,其中share

Qt Quick 之 QML 与 C++ 混合编程详解

Qt Quick 技术的引入,使得你能够快速构建 UI ,具有动画.各种绚丽效果的 UI 都不在话下.但它不是万能的,也有很多局限性,原来 Qt 的一些技术,比如低阶的网络编程如 QTcpSocket ,多线程,又如 XML 文档处理类库 QXmlStreamReader / QXmlStreamWriter 等等,在 QML 中要么不可用,要么用起来不方便,所以呢,很多时候我们是会基于这样的原则来混合使用 QML 和 C++: QML 构建界面, C++ 实现非界面的业务逻辑和复杂运算. 请给

Qt on Android: Qt Quick事件处理之鼠标、键盘、定时器

在<Qt on Android: Qt Quick 事件处理之信号与槽>中介绍了 QML 中如何使用内建类型的信号以及如何自定义信号,这次我们来看看如何处理鼠标.键盘.定时器等事件.这些时间在处理时,通常是通过信号来完成的. 广而告之:我正在参加 CSDN 博文大赛,请给我的参赛文章<Qt on Android: Qt Quick 事件处理之信号与槽>投票,谢谢. 鼠标事件处理 桌面开发的话,难免要处理鼠标事件-- 变色矩形示例 看一个简单的处理鼠标事件的例子,先看代码(handl

为啥都不用Qt Quick Controls 2呢

为啥都不用Qt Quick Controls 2呢 https://github.com/qt/qtquickcontrols2/

从头学Qt Quick(3)-- 用QML写一个简单的颜色选择器

先看一下效果图: 实现功能:点击不同的色块可以改变文字的颜色. 实现步骤: 一.创建一个默认的Qt Quick工程: 二.添加文件Cell.qml 这一步主要是为了实现一个自定义的组件,这个组件就是我们看到的那个色块,很明显定义成组件可以则兼UI的复用. 1 import QtQuick 2.0 2 3 Item { 4 id: container; 5 property alias cellColor: rectangle.color; 6 signal clicked(color cellC

测试Qt Quick在各个平台上的3D渲染性能

测试Qt Quick在各个平台上的3D渲染性能 Qt是一个跨平台的GUI框架,它的QtQuick更是支持结合OpenGL原生的代码进行渲染.我想将我以前写的程序整合到QtQuick上来,看看渲染效果是否满意,于是写了一个小小的程序,来做一下渲染基准测试.运行结果出来,不容乐观呐. 蒋彩阳原创文章,首发地址:http://blog.csdn.net/gamesdev/article/details/43842131.欢迎同行前来探讨. 首先为了描述最基本的情况,我制作了一个带有纹理的立方体.它使用

Qt Quick核心编程从入门到精通

本文是个推荐文章,推荐foruok博主的Qt quick 核心编程的系列经典编程! foruok 博主 的Qt Quick系列文章: Qt Quick 简介 QML 语言基础 Qt Quick 之 Hello World 图文详解 Qt Quick 简单教程 Qt Quick 事件处理之信号与槽 Qt Quick事件处理之鼠标.键盘.定时器 Qt Quick 事件处理之捏拉缩放与旋转 Qt Quick 组件与对象动态创建详解 Qt Quick 布局介绍 Qt Quick 之 QML 与 C++

QML官方教程——Using the Qt Quick Particle System

附网址:http://qt-project.org/doc/qt-5/qtquick-effects-particles.html Using the Qt Quick Particle System-- 使用Qt Quick粒子系统 所有粒子系统的类型都可以在QtQuick.Particles模块文档中找到. 注意想要使用粒子模块中的类型,你需要使用下面这个代码进行引入: import QtQuick.Particles 2.0 · The ParticleSystem 粒子系统包含4个主要的

QML官方系列教程——Qt Quick Controls Overview

附网址:http://qt-project.org/doc/qt-5/qtquickcontrols-overview.html Qt Quick Controls Overview -- Qt Quick Controls概述 Qt Quick Controls提供了一套用来在Qt Quick中创建用户界面的UI控件. Getting Started -- 开始 在你的.qml文件中使用以下声明将这些QML类型引入到你的应用程序中. import QtQuick.Controls 1.2 ·