在 QML 中创建 C++ 导入类型的实例

文件列表:

Project1.pro

QT += quick
CONFIG += c++
CONFIG += declarative_debug
CONFIG += qml_debug # 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. SOURCES += main.cpp \
colormaker.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 HEADERS += \
colormaker.h

colormaker.h

#ifndef COLORMAKER_H
#define COLORMAKER_H #include <QObject>
#include <QColor> class ColorMaker : public QObject
{
Q_OBJECT Q_ENUMS(GenerateAlgorithm)
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
Q_PROPERTY(QColor timeColor READ timeColor) public:
explicit ColorMaker(QObject *parent = nullptr);
~ColorMaker(); enum GenerateAlgorithm {
RandomRGB,
RandomRed,
RandomGreen,
RandomBlue,
LinearIncrease
}; QColor color() const;
void setColor(const QColor & color);
QColor timeColor() const; Q_INVOKABLE GenerateAlgorithm algorithm() const;
Q_INVOKABLE void setAlgorithm(GenerateAlgorithm algorithm); signals:
void colorChanged(const QColor & color);
void currentTime(const QString &strTime); public slots:
void start();
void stop(); protected:
void timerEvent(QTimerEvent *ev); private:
GenerateAlgorithm m_algorithm;
QColor m_currentColor;
int m_nColorTimer;
}; #endif // COLORMAKER_H

colormaker.cpp

#include "colormaker.h"
#include <QTimerEvent>
#include <QDateTime> ColorMaker::ColorMaker(QObject *parent)
: QObject(parent),
m_algorithm(RandomRGB),
m_currentColor(Qt::black),
m_nColorTimer()
{
qsrand(QDateTime::currentDateTime().toTime_t());
} ColorMaker::~ColorMaker()
{ } QColor ColorMaker::color() const
{
return m_currentColor;
} void ColorMaker::setColor(const QColor &color)
{
m_currentColor = color;
emit colorChanged(m_currentColor);
} QColor ColorMaker::timeColor() const
{
QTime time = QTime::currentTime();
int r = time.hour();
int g = time.minute() * ;
int b = time.second() * ;
return QColor::fromRgb(r, g, b);
} ColorMaker::GenerateAlgorithm ColorMaker::algorithm() const
{
return m_algorithm;
} void ColorMaker::setAlgorithm(ColorMaker::GenerateAlgorithm algorithm)
{
m_algorithm = algorithm;
} void ColorMaker::start()
{
if (m_nColorTimer == ) {
m_nColorTimer = startTimer();
}
} void ColorMaker::stop()
{
if ( m_nColorTimer > ) {
killTimer(m_nColorTimer);
m_nColorTimer = ;
}
} void ColorMaker::timerEvent(QTimerEvent *ev)
{
if ( ev->timerId() == m_nColorTimer ) {
switch (m_algorithm) {
case RandomRGB:
m_currentColor.setRgb(qrand() % , qrand() % , qrand() % );
break;
case RandomRed:
m_currentColor.setRed(qrand() % );
break;
case RandomGreen:
m_currentColor.setGreen(qrand() % );
break;
case RandomBlue:
m_currentColor.setBlue(qrand() % );
break;
case LinearIncrease:
{
int r = m_currentColor.red() + ;
int g = m_currentColor.green() + ;
int b = m_currentColor.blue() + ;
m_currentColor.setRgb(r % , g % , b % );
}
break;
default:
break;
} emit colorChanged(m_currentColor);
emit currentTime(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
} else {
QObject::timerEvent(ev);
}
}

main.cpp

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

main.qml

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3 import an.qt.ColorMaker 1.0 ApplicationWindow {
visible: true;
width: 640;
height: 480;
title: qsTr("This is My Test"); MainForm {
anchors.fill: parent;
}
}

MainForm.qml

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3 import an.qt.ColorMaker 1.0 Rectangle {
id: mainForm;
width: 640;
height: 480; Text {
id: timeLabel;
anchors.left: parent.left;
anchors.leftMargin: 4;
anchors.top: parent.top;
anchors.topMargin: 4;
font.pixelSize: 24;
} ColorMaker {
id: colorMaker;
color: Qt.green;
} Rectangle {
id: colorRect;
anchors.centerIn: parent;
width: 200;
height: 200;
color: "blue";
} Button {
id: start;
text: "start";
anchors.left: parent.left;
anchors.leftMargin: 4;
anchors.bottom: parent.bottom;
anchors.bottomMargin: 4;
onClicked: {
colorMaker.start();
}
} Button {
id: stop;
text: "stop";
anchors.left: start.right;
anchors.leftMargin: 4;
anchors.bottom: start.bottom;
onClicked: {
colorMaker.stop();
}
} function changeAlgorithm(button, algorithm)
{
switch(algorithm) {
case 0:
button.text = "RandomRGB";
break;
case 1:
button.text = "RandomRed";
break;
case 2:
button.text = "RandomGreen";
break;
case 3:
button.text = "RandomBlue";
break;
case 4:
button.text = "LinearIncrease";
break;
default:
button.text = "null";
break;
}
} Button {
id: colorAlgorithm;
text: "RandomRGB";
anchors.left: stop.right;
anchors.leftMargin: 4;
anchors.bottom: start.bottom;
onClicked: {
var algorithm = (colorMaker.algorithm() + 1) % 5;
changeAlgorithm(colorAlgorithm, algorithm);
colorMaker.setAlgorithm(algorithm);
}
} Button {
id: quit;
text: "quit";
anchors.left: colorAlgorithm.right;
anchors.leftMargin: 4;
anchors.bottom: start.bottom;
onClicked: {
Qt.quit();
}
} Component.onCompleted: {
colorMaker.color = Qt.rgba(0, 180, 120, 1);
colorMaker.setAlgorithm(ColorMaker.LinearIncrease);
changeAlgorithm(colorAlgorithm, colorMaker.algorithm());
} Connections {
target: colorMaker;
onCurrentTime: {
timeLabel.text = strTime;
timeLabel.color = colorMaker.timeColor;
}
} Connections {
target: colorMaker;
onColorChanged: {
colorRect.color = color;
}
}
}

运行效果:

在 QML 中创建 C++ 导入类型的实例的更多相关文章

  1. Laravel 5.1 中创建自定义 Artisan 控制台命令实例教程

    1.入门 Laravel通过Artisan提供了强大的控制台命令来处理非浏览器业务逻辑.要查看Laravel中所有的Artisan命令,可以通过在项目根目录运行: php artisan list 对 ...

  2. 在 QML 中使用 C++ 类和对象

    Qt Quick 技术的引入,使得你能够快速构建 UI ,具有动画.各种绚丽效果的 UI 都不在话下.但它不是万能的,也有很多局限性,原来 Qt 的一些技术,比如低阶的网络编程如 QTcpSocket ...

  3. QT之在QML中使用C++类和对象

    QML其实是对ECMAScript的扩展,融合了Qt object系统,它是一种新的解释性语言,QML引擎虽然由Qt C++实现,但QML对象的运行环境说到底和C++对象的上下文环境是不通的,是平行的 ...

  4. sharepoint 2013创建外部内容类型并创建外部列表

    步骤: 1.如何:基于 SQL Server 表创建外部内容类型 How to: Create an External Content Type Based on a SQL Server Table ...

  5. [转] 请别再拿“String s = new String("xyz");创建了多少个String实例”来面试了吧

    这帖是用来回复高级语言虚拟机圈子里的一个问题,一道Java笔试题的. 本来因为见得太多已经吐槽无力,但这次实在忍不住了就又爆发了一把.写得太长干脆单独开了一帖. 顺带广告:对JVM感兴趣的同学们同志们 ...

  6. 请别再拿“String s = new String("xyz");创建了多少个String实例”来面试了吧---转

    http://www.iteye.com/topic/774673 羞愧呀,不知道多少人干过,我也干过,面壁去! 这帖是用来回复高级语言虚拟机圈子里的一个问题,一道Java笔试题的. 本来因为见得太多 ...

  7. 向ACCESS数据库中的表导入EXCEL表,在 System.Data.OleDb.OleDbException 中第一次偶然出现的“System.Data.dll”类型的异常

    向ACCESS数据库中的表导入EXCEL表,EXCEL表Sheet1中与ACCESS数据库表中的关键字对应的列的值,不能与数据库中表的主键项值重复,不然就会出现 在 System.Data.OleDb ...

  8. C++中结构体与类的区别(结构不能被继承,默认是public,在堆栈中创建,是值类型,而类是引用类型)good

    结构是一种用关键字struct声明的自定义数据类型.与类相似,也可以包含构造函数,常数,字段,方法,属性,索引器,运算符和嵌套类型等,不过,结构是值类型. 1.结构的构造函数和类的构造函数不同. a. ...

  9. Java归去来第4集:java实战之Eclipse中创建Maven类型的SSM项目

    一.前言 如果还不了解剧情,请返回第3集的剧情          Java归去来第3集:Eclipse中给动态模块升级 二.在Eclipse中创建Maven类型的SSM项目 2.1:SSM简介 SSM ...

随机推荐

  1. android studio提交到开源git时出现:fatal: refusing to merge unrelated histories的解决办法

    创建本地库和fetch远程分支这些前面的步骤这里略过.可以自行百度. 解决办法: 1.cmd进入项目的根目录. 2.执行下面的命令:git pull origin master --allow-unr ...

  2. C#:通过NuGet程序包下载CefSharp来加载谷歌浏览器

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 首先我讲明一下我要做的,公司有个C# wpf的项目需要我把一个开发好的网页嵌入到客户端当中,由于种种原因,我放 ...

  3. Javascript格式化并高亮xml字符串

    Javascript格式化并高亮xml字符串 两个关键点 使用DOMParser解析xml 递归遍历xml树,按格式输出每一个节点 关于使用DOMParser 此方法目前在IE9以上和其它浏览器里都是 ...

  4. ffmpeg 踩坑实录 安装与视频切片(一)

    这段时间一直在做一个关于视频处理的项目.其中有一块需要切片相关功能.于是采用了ffmpeg来完成相关需求. 第一,ffmpeg的安装. 首先下载官方包,我这里用的是ffmpeg-release-64b ...

  5. Python环境搭建和pycharm安装

    Python环境搭建和pycharm安装 本人安装环境为Windows10系统,下载的Python版本为3.4社区版本,可参考 1.下载Python3.4版本 官网:https://www.pytho ...

  6. VirtualBox虚拟机上安装windows7系统

    1.下载Windows7的镜像文件 http://www.xitongcheng.com/jiaocheng/win7_article_24156.html 2.在虚拟机上安装Windows7 htt ...

  7. Hexo+gitment

    Gitment是一个基于GitHub问题的评论系统,可以在没有任何服务器端实现的前端使用. 演示页面 中文简介 特征 入门 方法 定制 关于安全 特征 GitHub登录 Markdown / GFM支 ...

  8. php 操作 oracle lob 数据

    http://www.oracle.com/technetwork/articles/fuecks-lobs-095315.html Working with LOBs in Oracle and P ...

  9. 印度电商Snapdeal获投$1.34亿 eBay领投

    据消息人士透露,eBay领投1.337亿美元,投资印度最大在线购物网站Snapdeal,最终或有可能全权收购该网站.据悉,在此次投资中,大部分资金来自eBay. 今年1月,曾有报道称,Snapdeal ...

  10. python打印图形大全(详解)

    ,): shixin=chr() print(shixin) -------------------结果:2) for i in range(0,10): shixin=chr(9679) print ...