当前位置: 首页 > news >正文

单例模式(下)

文章目录

  • 文章介绍
  • 步骤安排及单例讲解
  • step1:注册单例类型(main.cpp)
  • step2:定义类和私有构造函数(keyboardinputmanager.h)
  • step3:(keyboardinputmanager.cpp)
  • step4:在qml中调用keyboardinputmanager类(main.qml)
  • 重点步骤
    • main.cpp
    • keyboardinputmanager.h
    • keyboardinputmanager.cpp
    • main.qml

文章介绍

给一个单例模式的例子,实现将键盘输入数据打印到界面文本框的操作

步骤安排及单例讲解

step1:注册单例类型(main.cpp)
step2:定义类和私有构造函数(keyboardinputmanager.h)
step3:(keyboardinputmanager.cpp)
step4:在qml中调用keyboardinputmanager类(main.qml)
单例模式的优点:非常适合在 QML 中集成复杂的后端逻辑,使得前端界面可以直接调用后端逻辑的单例实例进行数据处理或业务逻辑运算。

step1:注册单例类型(main.cpp)

main.cpp中只用关注 qmlRegisterSingletonInstance("com.example", 1, 0, "KeyboardInputManager", KeyboardInputManager::instance());;
这行代码将 KeyboardInputManager注册为 QML 中的单例类型

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "KeyboardInputManager.h"int main(int argc, char *argv[])
{QGuiApplication app(argc, argv);QQmlApplicationEngine engine;qmlRegisterSingletonInstance("com.example", 1, 0, "KeyboardInputManager", KeyboardInputManager::instance());engine.load(QUrl(QStringLiteral("qrc:/main.qml")));if (engine.rootObjects().isEmpty())return -1;return app.exec();
}

step2:定义类和私有构造函数(keyboardinputmanager.h)

#ifndef KEYBOARDINPUTMANAGER_H
#define KEYBOARDINPUTMANAGER_H#include <QObject>// KeyboardInputManager 类:管理键盘输入的单例类
class KeyboardInputManager : public QObject
{Q_OBJECT
public:// 获取单例实例的方法static KeyboardInputManager* instance(){// 静态局部变量,确保单例static KeyboardInputManager instance;return &instance;}signals:// 按键事件信号,传递按键文本void keyPressed(const QString &key);protected:// 事件过滤器方法,用于捕获和处理键盘事件bool eventFilter(QObject *obj, QEvent *event) override;private:// 私有构造函数,确保单例模式KeyboardInputManager();// 私有析构函数~KeyboardInputManager();// 禁用拷贝构造函数KeyboardInputManager(const KeyboardInputManager&) = delete;// 禁用赋值运算符KeyboardInputManager& operator=(const KeyboardInputManager&) = delete;
};#endif // KEYBOARDINPUTMANAGER_H

step3:(keyboardinputmanager.cpp)

#include "KeyboardInputManager.h"
#include <QGuiApplication>
#include <QKeyEvent>// 构造函数:安装事件过滤器以捕获键盘事件
KeyboardInputManager::KeyboardInputManager()
{// 安装事件过滤器,将当前实例作为事件过滤器QGuiApplication::instance()->installEventFilter(this);
}// 析构函数:默认析构函数
KeyboardInputManager::~KeyboardInputManager() {}// 事件过滤器:捕获并处理键盘事件
bool KeyboardInputManager::eventFilter(QObject *obj, QEvent *event)
{// 检查事件类型是否为键盘按下事件if (event->type() == QEvent::KeyPress) {// 将事件转换为键盘事件QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);// 发射 keyPressed 信号,将按键文本传递给连接的槽函数emit keyPressed(keyEvent->text());// 返回 true 表示事件已处理return true;} else {// 调用父类的事件过滤器处理其他类型的事件return QObject::eventFilter(obj, event);}
}

step4:在qml中调用keyboardinputmanager类(main.qml)

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Window 2.15
import com.example 1.0Window {visible: truewidth: 640height: 480title: qsTr("Keyboard Input")TextArea {id: textAreaanchors.fill: parentwrapMode: Text.Wrapfocus: trueKeys.onPressed: {KeyboardInputManager.keyPressed(event.text)}}Connections {target: KeyboardInputManageronKeyPressed: {textArea.text += key}}
}

重点步骤

main.cpp

在这里插入图片描述

keyboardinputmanager.h

在这里插入图片描述

keyboardinputmanager.cpp

在这里插入图片描述

main.qml

在这里插入图片描述

http://www.lryc.cn/news/384836.html

相关文章:

  • 合约期VS优惠期,搞明白他们的区别才能避免很多坑!
  • 函数式反应式编程(FRP)在Scala中的实践与探索
  • NGINX配置web文件服务
  • deepspeed docker集群实现多机多卡训练----问题记录及解决方案资源汇总
  • 恢复 IntelliJ IDEA 中消失的菜单栏
  • 漏洞利用开发基础学习记录
  • 云通SIPX,您的码号资源智能调度专家!
  • 04-Mysql 索引,事务
  • U盘提示格式化怎么搞定?本文有5种方法(内含教程)
  • day02-登录模块-主页鉴权
  • git rebase的使用
  • LICEcap-开源GIF 屏幕录制工具
  • 【Java Web】会话管理
  • RestTemplate修改默认转换器,使用FastJsonConverter
  • 什么是div移动指令?如何用vue自定义指令实现?
  • Golang | Leetcode Golang题解之第187题重复的DNA序列
  • 智能猫砂盆到底是不是智商税?解救上班族双手的测评合集来了
  • java 数据新增、更新、删除监听,并记录日志或其他业务
  • developer.android.com在国内无法正常访问解决方法
  • 大学物理(下)笔记
  • Mind+在线图形编程软件(Sractch类软件)
  • 数智化招采供应链平台七大优点
  • Java面试题:对比HTTP的GET和POST方法,并讨论它们的使用场景
  • webpack+webpack server入门
  • Java内存模型以及多线程并发深度剖析
  • 【JS问题】require相对路径引入模块
  • SAP ABAP 常用实用类
  • 笔记本电脑录屏,教你3个方法,简单录屏
  • A-8 项目开源 qt1.0
  • CNC数控机床如何通过工业智能网关实现远程运维?天拓四方