精华内容
下载资源
问答
  • 需要在ApplicationWindow下添加:visible: true

    需要在ApplicationWindow下添加:visible: true

    展开全文
  • qml 笔记

    2020-10-25 12:31:59
    qml 按键 打印 以及资源文件用法 1. Button Button{ x:260 y:150 width: 90 height: 60 text: "qml test" onClicked: { console.log("LM pressed") } } 打印就是 console.log(...

    说在盘前:切记qml文件,除了main.qml, 其它 .qml 文件必须首字母大写: 否则其它qml文件无法调用到

    • qml 按键 打印 以及资源文件用法

    1. Button

    Button{
              x:260
              y:150
              width: 90
              height: 60
              text: "qml test"
              onClicked:  {
                 console.log("LM pressed")
                    }
            }

    打印就是  console.log("Hello World")

    2. 资源文件用法

    所谓资源文件,即是将 .icon的图标放到 Button上面,使其更加形象。

    创建资源文件方法:

    1. 鼠标在工程栏的 Resources 上右键,点击 Add New
    2. 依次选择 Qt -> Qt Resource File
     

    3. 起个名称为 icon 即可,点击下一步

    4. 此时工程栏上面便出现了 icon.qrc 目录,如下:

    5. 拷贝 icon 图片到工程路径下

      5.1 在工程目录下新建 icon 文件夹,然后将需要的 .icon 格式文件拷贝进去
      5.2 在工程栏的 icon.qrc 上面右键,点击 Add Existing Directory...  并选择刚创建的icon文件夹
      此时便可以看到icon图片都加载了进来,如下所示:

    然后,鼠标在 app.icon 上面右键,即可复制URL 文件路径,如下:

    最后一步,将复制的资源URL,添加到 Button的属性内,如下:

    Button{
       x:160
       y:60
       width: 90
       height: 60
       text: "HelloWorld"
       icon.source: "qrc:/icon/app.ico"
       onClicked: console.log("pressed")
    }

     即上述代码中的 icon.source: "qrc:/icon/app.ico"   

     运行效果:

     

    布局摆放

    • 布局之 anchors 

     

    anchors 使用:

    Button{
            id: bt1
            anchors.centerIn: parent  //bt1放到界面中间
    
            width: 150
            height: 60
            text: "BT1"
            icon.source: "qrc:/icon/app.ico"
            onClicked: console.log("pressed-1")
           }
    
        Button{
            id: bt2
            anchors.top: bt1.bottom  //bt2的顶部放在bt1的底部
            anchors.left: bt1.left   //bt2的左边与bt1左边对齐
            anchors.topMargin: 5       //bt2与bt1上下间隔5
    
            width: 150
            height: 60
            text: "BT2"
            icon.source: "qrc:/icon/app.ico"
            onClicked: console.log("pressed-2")
           }

    效果:

    Rectangle 矩形框用法:

        Button{
            id: bt3
            x:10
            y:20
            width: 90
            height: 60
            text: "qml test"
            onClicked:  {
                console.log("LM pressed")
            }
        
         Rectangle{
            anchors.top:  bt3.bottom
            anchors.topMargin: 20
            width: 100
            height: 100
            color: "#ff0000"
            border.color: "black"
            border.width: 2
            radius: 6  //边角半径
         }

    效果:

    如果要制作圆形,只需要将 radius: 6 中的 6改为width 的1/2 即可,如下:

    Rectangle{
            anchors.top:  bt3.bottom
            anchors.topMargin: 20
            width: 100
            height: 100
            color: "#ff0000"
            border.color: "black"
            border.width: 2
            radius: 50  //边角半径为 1/2 width时,为圆形
         }

    效果:

    设置字体颜色

    • Label字体颜色
    Label {
            text: qsTr("You are on Page 1.")
            color: "#ff00ff"    //直接使用color 标签
            font.pointSize: 24
            anchors.centerIn: parent
        }
    • Button字体颜色
    Button {
            id: button
            x: 72
            y: 36
            text: qsTr('<font color="#ff00ff">Home</font>')  //通过 <font > 标签来设置
    
            background: Rectangle {   // 设置按键字体颜色
                      implicitHeight: button1.height
                      implicitWidth:  button1.width
                      color: button.down ? "#ff0000" : "#00ff00"
                  }
        }
    • 多界面之 StackView

    通过点击各个界面上的Button来实现界面的切换功能Demo如下:

    main.qml 文件内容:

    import QtQuick 2.12
    import QtQuick.Controls 2.5
    
    ApplicationWindow {
        id: window
        visible: true
        width: 640
        height: 480
        title: qsTr("Stack")
    
       /* 使用stackView需要添加如下语句 */
        StackView {
            id: stackView
            initialItem: "HomeForm.qml"
            anchors.fill: parent
        }
    }

    Page1Form.qml 文件:

    xxx
    
    Button {
            id: button
            x: 72
            y: 36
            text: qsTr('<font color="#ff00ff">Home</font>')
           
            onClicked: {
                stackView.pop(stackView.currentItem)  //先清除上一级界面
                stackView.push("HomeForm.qml")     //进入新的目录HomeForm.qml 界面
            }
        }
    
    xxx
    • 多界面之 SwipeView

    通过左右滑动界面来实现界面的切换功能,Demo如下:

    main.qml 文件内容:

    import QtQuick 2.12
    import QtQuick.Controls 2.5
    
    ApplicationWindow {
        id: window
        visible: true
        width: 640
        height: 480
        title: qsTr("Stack")
    
      /* 写一个下面的SwipeView 来实现Page1Form界面与Page2Form左右滑动 */
        SwipeView {
            id: swipeView
            anchors.fill: parent
            currentIndex: tabBar.currentIndex
        
        // Page1Form.qml 文件名
            Page1Form {
            }
        // Page2Form.qml 文件名
            Page2Form {
            }
        }
    }

     

    c++与qml结合 前后端分离

    Qt官方参考资料:
    https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html
    https://doc.qt.io/qt-5/qtqml-cppintegration-topic.html

    CSDN网友资料:
    https://blog.csdn.net/qq_40876689/article/details/107970174

     

     

    分离方法如下:

    1. 新建handle.cpp类来处理槽函数
    2. main.qml创建Button Click的信号
    3. handle.cpp 创建处理Button Click信号的槽函数
    4. 修改main.cpp 并连接信号与槽

    1. 新建handle.cpp类来处理槽函数

    在工程栏Sources文件夹右键, 新建一个cpp class类 名为handle.cpp 并继承QObject。

    编写槽函数,如下所示:

    handle.h 文件

    #ifndef HANDLE_H
    #define HANDLE_H
    
    #include <QObject>
    
    class handle : public QObject
    {
        Q_OBJECT
    public:
        explicit handle(QObject *parent = nullptr);
    
    signals:
    
    public slots:
        void cppSingleClickSlot();   //Button-1 的槽函数
        void cppSingleClickSlot2();  //Button-2 的槽函数
    
    };
    
    #endif // HANDLE_H

    handle.cpp 文件

    #include "handle.h"
    #include <QDebug>
    handle::handle(QObject *parent) : QObject(parent)
    {
    
    }
    
    void handle::cppSingleClickSlot()  //Button-1 槽函数
    {
        qDebug()<<"cpp clicked"<<endl;
    }
    
    void handle::cppSingleClickSlot2()  //Button-2 槽函数
    {
        qDebug()<<"cpp clicked 2"<<endl;
    }

    2. main.qml创建Button Click的信号

    main.qml

    import QtQuick 2.12
    import QtQuick.Controls 2.5
    
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("Tabs")
    
        signal bt1_singleClick()  //创建Button-1 的Click 信号
        signal bt2_singleClick()  //创建Button-2 的Click 信号
    
        Button{
               id: bt1
               anchors.centerIn: parent
    
               width: 150
               height: 60
               text: "BT1"
               icon.source: "qrc:/icon/app.ico"
               onClicked: bt1_singleClick()  //调用信号
              }
    
    
         Button{
              id: bt2
              anchors.top: bt1.bottom
              anchors.left: bt1.left
              anchors.topMargin:  5
    
              width: 150
              height: 60
              text: "BT2"
              icon.source: "qrc:/icon/app.ico"
              onClicked: bt2_singleClick()  //调用信号
            }
    }
    

    3. 修改main.cpp 并连接信号与槽

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlComponent>
    #include "handle.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        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);
    //    engine.load(url);
    
        QQmlEngine eng;
        QQmlComponent compent(&eng,QUrl(QStringLiteral("qrc:/main.qml")));
        QObject *obj = compent.create();
    
        handle h;
        QObject::connect(obj,SIGNAL(bt1_singleClick()),&h,SLOT(cppSingleClickSlot()));
        QObject::connect(obj,SIGNAL(bt2_singleClick()),&h,SLOT(cppSingleClickSlot2()));
    
        return app.exec();
    }

    将main.cpp 原来的调用注释掉,写入新的。

    运行结果如下图:

     

     

     

     

     

     

     

     

     

    展开全文
  • QML Applications

    2016-04-24 10:52:19
    什么是QML Qt 5.6.0 Reference Documentation Contents What is QML? What is Qt Quick? QML User Interfaces Buttons, Menus, and other Controls Special Effects Viewing Web Content in QML ...

    什么是QML
    Qt 5.6.0 Reference Documentation
    Contents
    What is QML?
    What is Qt Quick?
    QML User Interfaces
    Buttons, Menus, and other Controls
    Special Effects
    Viewing Web Content in QML Applications
    Sensors, Gestures, and Touch Interfaces
    Multimedia Content
    Mobile Devices
    Code Samples and Demos
    Advanced Application Development Topics
    Other QML Modules
    Related Topics
    QML Applications
    QML is a declarative language that allows user interfaces to be described in terms of their visual components and how they interact and relate with one another. It is a highly readable language that was designed to enable components to be interconnected in a dynamic manner, and it allows components to be easily reused and customized within a user interface. Using the QtQuick module, designers and developers can easily build fluid animated user interfaces in QML, and have the option of connecting these user interfaces to any back-end C++ libraries.
    What is QML?
    QML is a user interface specification and programming language. It allows developers and designers alike to create highly performant, fluidly animated and visually appealing applications. QML offers a highly readable, declarative, JSON-like syntax with support for imperative JavaScript expressions combined with dynamic property bindings.
    The QML language and engine infrastructure is provided by the Qt QML module. For in-depth information about the QML language, please see the Qt QML module documentation.
    The following pages contain more information about QML:
    First Steps with QML - begin using QML with these examples
    Creating Qt Quick Projects in Qt Creator
    The QML Reference - reference about the QML constructs and features
    QML Coding Conventions
    Glossary of QML Terms
    What is Qt Quick?
    Qt Quick is the standard library of types and functionality for QML. It includes visual types, interactive types, animations, models and views, particle effects and shader effects. A QML application developer can get access to all of that functionality with a single import statement.
    The QtQuick QML library is provided by the Qt Quick module. For in-depth information about the various QML types and other functionality provided by Qt Quick, please see the Qt Quick module documentation.
    QML User Interfaces
    For creating or customizing graphical user interfaces, Qt Quick adds visual types, animation types, and other QML types in addition to the standard QML types from Qt QML. Qt Quick Designer is integrated within Qt Creator and supports QtQuick 2 from Qt Creator version 2.7 and onwards.
    Visual types in QML
    Responding to User Input in QML
    Animations in QML
    Displaying Text in QML
    Layouts in QML
    Style and Theme Support
    Integrating JavaScript in QML
    Scalability
    Buttons, Menus, and other Controls
    For a set of basic UI controls, the Qt Quick Controls module implements several controls such as buttons, menus, and views. These controls mimic the native behavior found in different platforms such as Windows, OS X, and Linux.
    Qt Quick Controls Overview
    Styles
    Dialogs
    Layouts
    Extras
    Special Effects
    Several Qt modules provide types for creating special effects in applications. Their respective pages contain more information about specific uses.
    Particle Effects
    Graphical Effects - for creating image composition effects.
    Viewing Web Content in QML Applications
    The QML types, WebEngineView and WebView, render and display dynamic web content. The only difference between the two is that the later uses a native web view if available. Both these types can load a URL or an HTML string. To read more about them and view code samples, see Qt WebEngine and Qt WebView.
    Sensors, Gestures, and Touch Interfaces
    The Qt Sensors module allows applications to read information from sensors such as accelerometers and tilt sensors. There is a common QML API for different platforms and can be extended in C++.
    Qt Sensors QML Types
    Qt Sensors Examples
    Compatibility Map - lists support level for different mobile platforms
    Multimedia Content
    The Qt Multimedia module enables applications to handle various media content with a convenient set of QML types. These QML types can be extended in C++.
    Multimedia
    Audio Overview
    Video Overview
    Camera Overview
    Radio Overview
    Qt Audio Engine - for 3D positional audio playback and content management.
    Mobile Devices
    Several Qt modules provide QML APIs for networked and mobile devices. The QML types provide access to the Bluetooth, Near-Field Communications (NFC), and GPS-enabled devices.
    Qt Positioning
    Qt Location
    Qt Bluetooth
    Qt NFC
    For more information, visit the Networking and Connectivity and Mobile APIs pages.
    Code Samples and Demos
    To learn more about uses of QML code, there are several code samples which show how QML types are used. In addition, there are several demos which show how QML code is used in applications.
    Getting Started Programming with Qt Quick - a tutorial showing the creation of a simple QML text editor.
    Qt Quick Examples and Tutorials
    Advanced Application Development Topics
    Deploying QML Applications
    Performance Considerations and Suggestions
    Internationalization and Localization
    Testing and Debugging
    Prototyping with qmlscene
    Debugging QML Applications
    Qt Quick Test: QML Unit Testing Framework
    Other QML Modules
    Qt Quick only provides basic visual types and much of Qt’s functionality is exposed to QML through other modules. If you require the functionality of those modules, you should browse their QML documentation.
    The All QML APIs by Module contains a list of all QML modules in Qt 5.
    Related Topics
    Porting QML Applications to Qt 5

    展开全文
  • 首先输入打包命令: windeployqt test.exe --qmldir C:\Qt\Qt5.5.1_32\5.5\msvc2013\qml , 其中test.exe是自己exe的名字,qmldir后面是自己的qt安装路径; 打包完成之后,双击exe运行,发现提示This application ...

    首先输入打包命令: windeployqt test.exe --qmldir C:\Qt\Qt5.5.1_32\5.5\msvc2013\qml , 其中test.exe是自己exe的名字,qmldir后面是自己的qt安装路径;

    打包完成之后,双击exe运行,发现提示This application failed to start because it could not find or load the Qt platform plugin "windows".错误;解决方法如下:

    1.首先确认打包目录下是否有platforms/qwindows.dll文件,如果没有,则从qt安装目录中拷贝一份;

    然后重新测试程序;

     

    2.如果打包文件中存在qwindows.dll文件, 还是提示错误,则需要在打包目录下创建一个新文件夹plugins,然后将platforms/qwindows.dll这个文件夹整个移动到plugins内即可;

    展开全文
  • qml学习

    2021-08-01 15:19:54
    qml项目 打开 Qt Creator 并创建一个新的项目,我们再次选择 Qt Quick Application Empty 作为我们的模板。 如您所见,我们现在处于与 Hello World 应用程序完全相同的位置。 开始新项目时,我总是做的第一件事是...
  • qml 多界面切换——qml

    2021-04-16 17:08:27
    之前用QtWidgets开发时,我曾写过一篇博客,现在我用qml开发移动端,觉得也有必要记录一下多界面切换这个知识点。 界面切换的几种方式 多界面切换根据不同的场合,可用不同的切换方式,当然,你非要只用一种方式也...
  • QML笔记-4中方式运行qml文件

    千次阅读 多人点赞 2019-05-25 19:41:10
    使用QQmlApplicationEngine运行qml 使用qml工具运行 使用qmlScene工具运行qml文件 使用QtQuick Prototype运行qml文件 使用QQmlApplicationEngine运行qml 程序结构如下: 源码如下: main.cpp #...
  • qml布局

    2018-03-06 15:57:46
    qml的Column、Row、Grid布局与QTc++的布局一致,还有Flow布局,会自动排列不会重叠,Repeater用于用于创建大量类似的项目,比如格子什么的。锚线用于定位部件在父部件中的位置,anchor下的horizontalCenter、...
  • qml1与qml2

    2015-10-10 16:21:57
    Starting /root/Desktop/build-qml2-Desktop-Debug/qml2... QML debugging is enabled. Only use this in a safe environment. QQuickView only supports loading of root objects that derive from QQuickItem. ...
  • QML与C++交互

    万次阅读 多人点赞 2019-02-27 15:21:09
    Qt QML模块中的类使QML对象能够从C ++加载和操作,QML引擎与Qt元对象系统集成的本质使得C ++功能可以直接从QML调用。这允许开发混合应用程序,这些应用程序是通过混合使用QML,JavaScript和C ++代码实现的。 QML is...
  • qml 3

    2015-12-23 10:15:33
    C++程序中使用QML绑定机制 QML被定为一种可容易使用C++扩展,并可扩展C++的语言.使用Qt Declarative模块中的类可在C++中加载和操作QML中的组件,通过Qt的元对象系统,QML和C++对象可轻易的使用信号和槽机制进行通信....
  • 学习QML

    2017-07-14 21:37:00
    1 QML只用来做为界面的组织语言,具体的引擎相关的实现放到c++来先,然后注册给QML来用,做到界面和具体逻辑的分离 2 实现一个 属性编辑器,Model由引擎端来提供,然后通知UI进行更新界面; 使用QML plugin遇到...
  • QML学习

    2019-05-03 13:44:22
    Qt 5.12调试需要设置platform插件库的包含路径,否则会导致报错。...qml控件的展示需要用到Qt Quick中定义的控件,但需要设置导入控件路径, 以用于qml中的文件导入。若不设置,在运行时报错“mod...
  • Qt-QML-QML调用C++类

    千次阅读 2017-06-03 16:57:42
    QML 调用C++的两种方式 1. 注册成为全局属性,在哪里都可以用 2.注册成为一个QML组件,在需要的地方import,并生成空间
  • 文章目录QML开发笔记(二):创建QML项目方法一:直接创建Qt Quick模板方法二:创建Qt Widget模板后,更改为Qt Quick模板1、添加QML资源文件2、创建qml窗口界面3、更改pro文件的配置4、更改main.cpp调用界面的方式4、...
  • 在使用QML时,我们常常看到类似这样的语句: import QtQuick 2.0 import QtQuick 2.0 通过这些import语句,我们就能使用QML中的相关控件了。 其实这就是导入模块的意思,它包括3个部分:import关键字、模块名称...
  • QML编程入门

    万次阅读 2012-09-02 22:50:01
    欢迎来到声明式UI语言QML的世界.在本入门教程中,我们使用QML创建一个简单的文本编辑器.阅读这个教程后,就可以使用QML和Qt C++开发应用程序了. 安装 首先需要安装包含Qt Quick的Qt最新版本,现在是Qt4.7.安装教程...
  • 虽然做QT开发一直使用的是QWidget,但是了解QML也有一段时间了,加之以前做过Javascript开发几个月时间,对Javascript有一定的基础,最近一直想学习下QML相关内容。 QML内容之多,想入门得有个契入点,我选择了*....
  • QML 界面切换

    2021-07-20 09:51:49
    有关qml部分基础部分就写到这里了,非常的简单,后面不在记录基础的qml程序了,后期根据项目,在给大家讲解一些例子。 首先是最简单的方式,静态界面,就是加载全部数据,只是不显示,然后按键显示。 效果图如下:...
  • QML UI 与逻辑分开

    千次阅读 2019-01-09 07:39:02
    大多数开发者都希望创建一个可维护的应用程序,要达到该目的的方法之一就是将用户界面与业务逻辑分开,应用程序的 UI 应该用 QML 编写的几个原因如下: 声明性语言非常适合定义 UI QML 代码编写很简单,因为它比 ...
  • component, err := engine.LoadFile("main.qml") if err != nil { return err } context := engine.Context() context.SetVar("ctrl", &Control{Name: "Enter your name"}) window := component....
  • QML国际化

    2018-07-04 10:10:35
    通过添加.qm文件对程序中含有qsTr(“english”)的程序进行翻译,达到双语的目的。...3.生成ts文件,在qt命令行中使用lupdate命令:lupdate qml/translation.qrc -ts qml.ts 4.打开qt Linguist对qsTr的英文...
  • 主要分为三种 1)使用qmlscene工具,bin文件夹下例如有默认main.qml的文件  qmlscene main.qml即可完成加载,比较快速;... engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); engine的方式搭配根对象为
  • QML笔记Loader的使用方法Loader简介为什么要使用LoaderLoader的sourceComponent用法...Loader is used to dynamically load QML components. Loader can load a QML file (using the source property) or a Comp...
  • qml dockwidget窗口停靠

    2021-06-09 19:32:20
    前言 前面一篇文章介绍了KDDockWidgets的使用(文章在这里),其实主要目的就是为了用KDDockWidgets提供的Qt quick 下的窗口停靠功能。...那么,今天就来看一下如何用KDDockWidgets做自己的在qml 下的窗口停靠功
  • 1. 如何在python里获得qml里的对象? 1.1 获取根对象 QML: import QtQuick 2.12 import QtQuick.Controls 2.12 ApplicationWindow { id: window ...使用QQmlApplicationEngine类的rootObjects...engine.load('qml-test
  • QML入门教程

    千次阅读 2015-02-07 10:34:47
    欢迎来到声明式UI语言QML的世界.在本入门教程中,我们使用QML创建一个简单的文本编辑器.阅读这个教程后,就可以使用QML和Qt C++开发应用程序了. 安装 首先需要安装包含Qt Quick的Qt最新版本,现在是Qt4.7.安装...
  • QML笔记:QML基本概念及使用 Qt5中的Qt Qml和Qt Quick架构 Qt Qml模块本身并没有涉及图形显示,所有的图形处理都由Qt Quick模块完成。 Qt Quick 以QPA(Qt Platform Abstraction)为基础,之上经过了Qt Gui、...
  • qml: C++调用qml函数

    2018-05-09 11:16:00
    C++调用qml函数,是通过下面的函数实现的: bool QMetaObject::invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0 = ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 2,130
精华内容 852
关键字:

qml的load