|
我的解决方案:
创建一个c++类:模拟键盘按键类,功能发送键盘事件到指定的UI对象如图:
通过点击UI上的两个按钮:‘UP’ 'DOWN'
可以实现键盘‘up’ 'down'的功能:使焦点上下移动
模拟按键类如下:
CKeyEvent.h
- class CKeyEvent : public QObject
- {
- Q_OBJECT
-
- Q_ENUMS(CKEY_ID)
-
- public:
- enum CKEY_ID
- {
- CKEY_UP = Qt::Key_Up,
- CKEY_DOWN= Qt::Key_Down,
- CKEY_MAX
- };
- CKeyEvent(QObject *parent = NULL);
- ~CKeyEvent();
-
- Q_INVOKABLE void sendCkeyPressEvent(QObject* receiver,Qt::Key CkeyId);
- Q_INVOKABLE void sendCkeyReleaseEvent(QObject* receiver,Qt::Key CkeyId);
-
- private:
- QObject* m_receiver;
- };
复制代码 类的实现:
CKeyEvent.cpp
- CKeyEvent::CKeyEvent(QObject *parent):QObject(parent)
- {
- }
- CKeyEvent::~CKeyEvent()
- {
- }
- void CKeyEvent::sendCkeyPressEvent(QObject* receiver,Qt::Key CkeyId)
- {
- QKeyEvent keyPressEvent(QEvent::KeyPress, CkeyId, Qt::NoModifier);
- QGuiApplication::sendEvent(receiver, &keyPressEvent);
- }
- void CKeyEvent::sendCkeyReleaseEvent(QObject* receiver,Qt::Key CkeyId)
- {
- QKeyEvent keyReleaseEvent(QEvent::KeyRelease, CkeyId, Qt::NoModifier);
- QGuiApplication::sendEvent(receiver, &keyReleaseEvent);
- }
复制代码 主函数:main.cpp
- int main(int argc, char *argv[])
- {
- QGuiApplication app(argc, argv);
-
-
- //注册C++类到QML
- //qmlRegisterType<CKeyEvent>("CKeyEvent", 1, 0, "CKeyEvent");
-
- QQmlApplicationEngine engine;
-
- CKeyEvent *Ckey = new CKeyEvent();
- engine.rootContext()->setContextProperty("cKeyCDP",Ckey);
- engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
-
- return app.exec();
- }
复制代码 QML:main.qml
- Window {
- id : mainWindows
- visible: true
- width: 1024
- height: 768
- title: qsTr("Hello World")
- GridMenu {
- id: gridMenu
- y: 320; width: parent.width; height: 320
- activeFocusOnTab: true
- }
- Button
- {
- id :keyUp
- anchors.top:keyFocus.top
- anchors.topMargin: 20
- anchors.left : keyFocus.right
- anchors.leftMargin: 20
- width: 100
- height: 60
- text: qsTr("up")
- onClicked:
- {
- cKeyCDP.sendCkeyPressEvent(mainWindows,Qt.Key_Up);
- cKeyCDP.sendCkeyReleaseEvent(mainWindows,Qt.Key_Up);
- }
- }
- Button
- {
- id :keyDown
- anchors.top:keyUp.bottom
- anchors.topMargin: 20
- anchors.left : keyFocus.right
- anchors.leftMargin: 20
- width: 100
- height: 60
- text: qsTr("down")
- onClicked:
- {
- cKeyCDP.sendCkeyPressEvent(mainWindows,Qt.Key_Down);
- cKeyCDP.sendCkeyReleaseEvent(mainWindows,Qt.Key_Down);
- }
- }
- }
复制代码 GridMenu.qml:
- FocusScope {
- property alias interactive: gridView.interactive
- onActiveFocusChanged: {
- //if (activeFocus)
- //mainView.state = "showGridViews"
- }
- Rectangle {
- anchors.fill: parent
- clip: true
- gradient: Gradient {
- GradientStop { position: 0.0; color: "#193441" }
- GradientStop { position: 1.0; color: Qt.darker("#193441") }
- }
- GridView {
- id: gridView
- anchors.fill: parent; anchors.leftMargin: 20; anchors.rightMargin: 20
- cellWidth: 152; cellHeight: 152
- focus: true
- model: 12
- //KeyNavigation.up: tabMenu
- //KeyNavigation.down: listMenu
- //KeyNavigation.left: contextMenu
- delegate: Item {
- id: container
- width: GridView.view.cellWidth; height: GridView.view.cellHeight
- Rectangle {
- id: content
- color: "transparent"
- antialiasing: true
- anchors.fill: parent; anchors.margins: 20; radius: 10
- Rectangle { color: "#91AA9D"; anchors.fill: parent; anchors.margins: 3; radius: 8; antialiasing: true }
- Image { source: "images/qt-logo.png"; anchors.centerIn: parent }
- }
- MouseArea {
- id: mouseArea
- anchors.fill: parent
- hoverEnabled: true
- onClicked: {
- container.GridView.view.currentIndex = index
- container.forceActiveFocus()
- }
- }
- states: State {
- name: "active"; when: container.activeFocus
- PropertyChanges { target: content; color: "#FCFFF5"; scale: 1.1 }
- }
- transitions: Transition {
- NumberAnimation { properties: "scale"; duration: 100 }
- }
- }
- }
- }
- }
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|