文章目錄
- 文章介紹
- 步驟安排及單例講解
- 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}}
}