热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

QML编程:C++交互实现资料定时刷新的思路

通过C++进行后端开发,完成即时通讯,实现即时资料的定时刷新和前端进行交互开发,是开发过程中必须要考虑和解决的问题。一种方式是,利用QML为我们提供了Timer控件,实现高度定制的控件,定时

  通过C++进行后端开发,完成即时通讯,实现即时资料的定时刷新和前端进行交互开发,是开发过程中必须要考虑和解决的问题。一种方式是,利用QML为我们提供了Timer控件,实现高度定制的控件,定时对资料库进行查询,来实现定时刷新资料的需求,但是这种处理方式,效率是一个很大的问题,导致每个元件都需要绑定一个定时器,在大批量的资源更新时,CPU占比很高,不是很可取。第二种处理方式,是在每个页面去提供Timer定时器,对每个页面的资料去进行一个批量的处理,这样效率相对较高,但自由灵活度不够,每个页面都需要导出控件,然后针对不同的控件去申请刷新资料。第三种方式,即提供一个全局的定时器(思路可以扩展到多个不同时间间隔的定时器),然后针对元件进行定制。本文介绍的思路即为第三种实现方式。代码实现如下
  

//time notifier header file

#define TIMENOTIFIER_H
#include 
#include 
#include 

class QTimer;
class TimeNotifier:public QObject
{
    Q_OBJECT
public:
    TimeNotifier(QObject* parent=nullptr);
    //提供函数接口
    Q_INVOKABLE void doSomething(){ }
signals:
    //超时通知信号
    void timeNotifier();
public slots:
    //提供信号槽接口
    void onTimeOut();
private:
    QTimer* timer;
};

//提供一个单例模式下的注册函数
static QObject *timenotifier_qobject_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
 {
     Q_UNUSED(engine)
     Q_UNUSED(scriptEngine)

     TimeNotifier *timeNotifier = new TimeNotifier();
     return timeNotifier;
 }

#endif // TIMENOTIFIER_H
//time notifier imcomplement file
#include "timenotifier.h"
#include <QTimer>
#include <QDebug>
TimeNotifier::TimeNotifier(QObject* parent):QObject(parent),timer(0)
{
    timer=new QTimer(this);
    timer->setInterval(100);//100ms
    timer->start();
    connect(timer, SIGNAL(timeout()),this, SLOT(onTimeOut()));
}

void TimeNotifier::onTimeOut()
{
    emit timeNotifier();
}

qmlRegisterSingletonType:单例对象注册函数,通过提供uri和类型名称,主版本号,次版本号,注册回调函数完成一个lie
完成上述代码的编辑,就可以通过调用该函数

qmlRegisterSingletonType("Qt.Customer.Singleton", 1, 0, "TimeNotifier", \
 timenotifier_qobject_singletontype_provider);

完成一个全局定时器的注册任务,这样在我们QML工程中就可以使用TimeNotifier这个全局的对象了。


// CustomSlider.qml
import QtQuick 2.0
import QtQuick.Controls 2.2
import QtQuick.Controls.Material 2.2
import QtQuick.Controls.Material.impl 2.3
import Qt.Customer.Singleton 1.0

Slider { id: control property alias slot: slot property int handleRadius: 10 handle: SliderHandle { x: control.leftPadding + (control.horizontal ? control.visualPosition * (control.availableWidth - width) : (control.availableWidth - width) / 2) y: control.topPadding + (control.horizontal ? (control.availableHeight - height) / 2 : control.visualPosition * (control.availableHeight - height)) value: control.value handleHasFocus: control.visualFocus handlePressed: control.pressed handleHovered: control.hovered width:handleRadius height:handleRadius }

    background: Rectangle { id: slot x: control.leftPadding + (control.horizontal ? 0 : (control.availableWidth - width) / 2) y: control.topPadding + (control.horizontal ? (control.availableHeight - height) / 2 : 0) implicitWidth: control.horizontal ? 200 : 48 implicitHeight: control.horizontal ? 48 : 200 width: control.horizontal ? control.availableWidth : 3 height: control.horizontal ? 3 : control.availableHeight color: control.Material.foreground scale: control.horizontal && control.mirrored ? -1 : 1 Rectangle { x: control.horizontal ? 0 : (parent.width - width) / 2 y: control.horizontal ? (parent.height - height) / 2 : control.visualPosition * parent.height width: control.horizontal ? control.position * parent.width : parent.width+2 height: control.horizontal ? parent.height+2 : control.position * parent.height color: control.Material.accentColor }
    }

    Connections{ target: TimeNotifier onTimeNotifier:{ console.log("height:" ,control.availableHeight) control.value=Math.random()*150;//此处仅进行测试用,可以替换为其他处理方式 }
    }
}

Connections:信号连接处理,target:信号触发来源,onTimeNotifier:信号处理函数, enabled:使能接受信号源触发,ignoreUnknownSignals:未知信号是否报错

//PageForm.ui.qml
import QtQuick 2.4
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.0

CategoryPageBackGround { GridLayout { anchors.fill: parent rows: 8 columns: 4 CustomSlider { id: customerSlider Layout.fillHeight: true Layout.fillWidth: true font.pixelSize: 8 font.family: "Verdana" Layout.preferredHeight: 11 Layout.preferredWidth: 78 to: 150 }

        CustomSlider { id: customerSlider4 Layout.fillHeight: true Layout.fillWidth: true Layout.preferredHeight: 11 Layout.preferredWidth: 78 to: 150 }

        CustomSlider { id: customerSlider16 Layout.fillHeight: true Layout.fillWidth: true Layout.preferredHeight: 11 Layout.preferredWidth: 78 to: 150 }

        CustomSlider { id: customerSlider20 Layout.fillHeight: true Layout.fillWidth: true Layout.preferredHeight: 11 Layout.preferredWidth: 78 to: 150 }

        CustomSlider { id: customerSlider1 Layout.fillHeight: true Layout.fillWidth: true Layout.preferredHeight: 11 Layout.preferredWidth: 78 to: 150 }

        CustomSlider { id: customerSlider5 Layout.fillHeight: true Layout.fillWidth: true Layout.preferredHeight: 11 Layout.preferredWidth: 78 to: 150 }

        CustomSlider { id: customerSlider17 Layout.fillHeight: true Layout.fillWidth: true Layout.preferredHeight: 11 Layout.preferredWidth: 78 to: 150 }

        CustomSlider { id: customerSlider21 Layout.fillHeight: true Layout.fillWidth: true Layout.preferredHeight: 11 Layout.preferredWidth: 78 to: 150 }

        CustomSlider { id: customerSlider2 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredHeight: 11 Layout.preferredWidth: 78 }

        CustomSlider { id: customerSlider6 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredHeight: 11 Layout.preferredWidth: 78 }

        CustomSlider { id: customerSlider18 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredHeight: 11 Layout.preferredWidth: 78 }

        CustomSlider { id: customerSlider22 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredHeight: 11 Layout.preferredWidth: 78 }

        CustomSlider { id: customerSlider3 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredHeight: 11 Layout.preferredWidth: 78 }

        CustomSlider { id: customerSlider7 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredHeight: 11 Layout.preferredWidth: 78 }

        CustomSlider { id: customerSlider19 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredHeight: 11 Layout.preferredWidth: 78 }

        CustomSlider { id: customerSlider23 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredHeight: 11 Layout.preferredWidth: 78 }

        CustomSlider { id: customerSlider8 Layout.fillHeight: true Layout.fillWidth: true Layout.preferredHeight: 11 Layout.preferredWidth: 78 to: 150 }

        CustomSlider { id: customerSlider12 Layout.fillHeight: true Layout.fillWidth: true Layout.preferredHeight: 11 Layout.preferredWidth: 78 to: 150 }

        CustomSlider { id: customerSlider24 Layout.fillHeight: true Layout.fillWidth: true Layout.preferredHeight: 11 Layout.preferredWidth: 78 to: 150 }

        CustomSlider { id: customerSlider28 Layout.fillHeight: true Layout.fillWidth: true Layout.preferredHeight: 11 Layout.preferredWidth: 78 to: 150 }

        CustomSlider { id: customerSlider9 Layout.fillHeight: true Layout.fillWidth: true Layout.preferredHeight: 11 Layout.preferredWidth: 78 to: 150 }

        CustomSlider { id: customerSlider13 Layout.fillHeight: true Layout.fillWidth: true Layout.preferredHeight: 11 Layout.preferredWidth: 78 to: 150 }

        CustomSlider { id: customerSlider25 Layout.fillHeight: true Layout.fillWidth: true Layout.preferredHeight: 11 Layout.preferredWidth: 78 to: 150 }

        CustomSlider { id: customerSlider29 Layout.fillHeight: true Layout.fillWidth: true Layout.preferredHeight: 11 Layout.preferredWidth: 78 to: 150 }

        CustomSlider { id: customerSlider10 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredHeight: 11 Layout.preferredWidth: 78 }

        CustomSlider { id: customerSlider14 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredHeight: 11 Layout.preferredWidth: 78 }

        CustomSlider { id: customerSlider26 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredHeight: 11 Layout.preferredWidth: 78 }

        CustomSlider { id: customerSlider30 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredHeight: 11 Layout.preferredWidth: 78 }

        CustomSlider { id: customerSlider11 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredHeight: 11 Layout.preferredWidth: 78 }

        CustomSlider { id: customerSlider15 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredHeight: 11 Layout.preferredWidth: 78 }

        CustomSlider { id: customerSlider27 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredHeight: 11 Layout.preferredWidth: 78 }

        CustomSlider { id: customerSlider31 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredHeight: 11 Layout.preferredWidth: 78 }

        CustomSlider { id: customerSlider32 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider33 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider34 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider35 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider36 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider37 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider38 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider39 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider40 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider41 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider42 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider43 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider44 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider45 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider46 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider47 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider48 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider49 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider50 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider51 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider52 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider53 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider54 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider55 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider56 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider57 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider58 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider59 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider60 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider61 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider62 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }

        CustomSlider { id: customerSlider63 Layout.fillHeight: true Layout.fillWidth: true to: 150 Layout.preferredWidth: 78 Layout.preferredHeight: 11 }
    }
}

效果图
100ms定时刷新


推荐阅读
  • 预备知识可参考我整理的博客Windows编程之线程:https:www.cnblogs.comZhuSenlinp16662075.htmlWindows编程之线程同步:https ... [详细]
  • Flex中使用filter过滤数据 ... [详细]
  • 开发笔记:Xunit测试使用个人小结
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Xunit测试使用个人小结相关的知识,希望对你有一定的参考价值。因工作中用到xunit测试,故总结下用法,以供个人参考使 ... [详细]
  • 一、如果使用默认的1521端口,让实例自动注册到该监听上,那么local_listener无需设置,listener.ora文件按照正常方 ... [详细]
  • JS加密解密
    leta=汪政..222RRRp767868^*%^*%344h哈哈;letb=udp.d(ud(a));//需要加密的内容letc=udp. ... [详细]
  • Qt 学习笔记(5)绘图   五子棋游戏
    在上一篇博客CQt学习笔记(4)绘图中介绍了Qt中的绘图方法,基于上一篇的博客的知识,使用QPainter设计一个五子棋的棋 ... [详细]
  • 本文整理了Java中java.lang.Iterable.iterator()方法的一些代码示例,展示了Iterable.iterator() ... [详细]
  • NGUIusingSystem;usingUnityEng ... [详细]
  • JS·经典·炫彩菜单(动画效果) for jquery
    CSS样式body{font-size:12px;}.menuBox{width:50%;height:auto;margin:0auto;}.menuBoxul{margin:0 ... [详细]
  • 做好了项上,其中包含有一个上传的功能。在开发环境和测试环境运行、测试都没什么问题。也许是由于本地的局域网的问题,一切都运行的比较快,但把它发布到外网的服务器上去时。就特别的慢。上传小的文件还算比 ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 本文介绍了一个适用于PHP应用快速接入TRX和TRC20数字资产的开发包,该开发包支持使用自有Tron区块链节点的应用场景,也支持基于Tron官方公共API服务的轻量级部署场景。提供的功能包括生成地址、验证地址、查询余额、交易转账、查询最新区块和查询交易信息等。详细信息可参考tron-php的Github地址:https://github.com/Fenguoz/tron-php。 ... [详细]
  • 注:根据Qt小神童的视频教程改编概论:利用最新的Qt5.1.1在windows下开发的一个小的时钟程序,有指针与表盘。1.Qtforwindows开发环境最新的Qt已经集 ... [详细]
  • DDOSDDOS的中文名叫分布式拒绝服务***,俗称洪水***DDoS***概念DoS的***方式有很多种,最基本的DoS***就是利用合理的服务请求来 ... [详细]
author-avatar
淡水鱼yw灬s
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有