Hello Quick World



  • 参考视频:Getting started with Qt: Hello Quick World

    我的环境是Qt5.11,跟视频中的差别还是有一些的。

    主要功能

    • 在界面上显示Hello World
    • 点击push me按钮,Hello World变为Hello Qt

    源码

    建好工程后,修改源代码

    hello-quick-world.pro

    QT += quick
    CONFIG += c++11
    
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which as been marked deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    SOURCES += \
            main.cpp
    
    RESOURCES += qml.qrc
    
    # Additional import path used to resolve QML modules in Qt Creator's code model
    QML_IMPORT_PATH =
    
    # Additional import path used to resolve QML modules just for Qt Quick Designer
    QML_DESIGNER_IMPORT_PATH =
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    
    

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    
    

    上面两个文件是不需要修改的

    主要修改的代码在qml文件里

    main.qml

    import QtQuick 2.11
    import QtQuick.Window 2.11
    import QtQuick.Controls 2.2
    
    Window {
        id: window
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        Button {
            id: button
            text: qsTr("Push Me")
            anchors.verticalCenter: parent.verticalCenter
            anchors.horizontalCenter: parent.horizontalCenter
            onClicked: {
                label.text = qsTr("Hello Qt")
            }
        }
    
        Label {
            id: label
            text: qsTr("Hello World")
            anchors.horizontalCenter: button.horizontalCenter
        }
    }
    
    

    截图

    最后的运行效果

    image

    自己做的时候留下的部分截图

    image
    image
    image
    image
    image
    image
    image
    image
    image

    思考与总结

    qml的hello world我写过很多遍了,现在要我解释每一行代码后面的含义,我还真说不上来。重新试一试qml,是想着能不能在接下来空闲的一段时间,做一个类似火绒的界面出来。火绒现在是我电脑里必备的一款软件,记得之前有同学用Qt/qml做过360的一个界面,现在Qt都发展到5.11,是时候更新一把。

    GitHub地址: https://github.com/PikachuHy/qml-demo/tree/master/hello-quick-world


Log in to reply
 

走马观花

最近的回复

  • H
    Toou 2D 拿来即用,为简单而生。

    简称T2D,是一款采用自身模块规范编写的轻量级UI框架,遵循Qt书写与组织形式门槛极低无需深入学习简单易用可拿来即用,丰富的控件模块适合界面的快速开发,让程序人员拥有更多的精力来实现业务逻辑与算法。

    统一交互规范,丰富的Ui控件几十种常用控件放弃了Qt Controls 及 Controls 2 来提高性能。

    完善的主题系统,业务逻辑与界面主题设计分离,可通过简单修改变量自定义主题皮肤。灵活的多主题皮肤绑定机制、在不需要重启App即实现一键换肤

    ini皮肤配置规则与每一个控件融合。可在应用内配置也可在应用外动态扩展配置。

    框架自动化安装支持动态库、静态库多模式编译。使用方便更安全更自由。

    提供丰富Demo、全面的帮助文档,Api查阅快速方便。项目必备开源框架!

    已经集成最新版 Font Awesome 4.7

    开源地址:https://github.com/ShowFL/Toou-2D

    read more
  • 刚刚毕业,工作用QT开发,以后有问题多多请教各位前辈😬 抱拳了。

    read more

关注我们

微博
QQ群