【问题标题】:How to send QKeyEvent to specific qml item?如何将 QKeyEvent 发送到特定的 qml 项目?
【发布时间】:2015-12-01 07:13:31
【问题描述】:

我正在尝试将自定义 QKeyEvent(如 Keypress key B)发送到我的 QML 文件中的项目(如 TextField)。这是我写的,它不起作用。似乎项目 (TextField) 没有收到我的事件(我假设这是因为我的 TextField 的文本中没有附加 B 字符)。我在ClickHandler 类中捕获click signal,然后在我的handleClick slot 中,我尝试将自定义event 发布到我的@987654330 @ 这里是TextField

ClickHandler.h

class ClickHandler : public QObject
{
    Q_OBJECT
    QQmlApplicationEngine* engine;
    QApplication* app;
public:
    explicit ClickHandler(QQmlApplicationEngine *,QApplication* app);

signals:

public slots:
    void handleClick();
};

ClickHandler.cpp:

#include "clickhandler.h"
#include <QMessageBox>
#include <QQuickItem>

ClickHandler::ClickHandler(QQmlApplicationEngine* engine, QApplication* app)
{
    this->engine = engine;
    this->app = app;

}

void ClickHandler::handleClick()
{
    QObject* root = engine->rootObjects()[0];
    QQuickItem *item = (root->property("activeFocusItem")).value<QQuickItem *>();
    if (item == NULL)
        qDebug() << "NO item";
    QKeyEvent* event = new QKeyEvent(QKeyEvent::KeyPress,Qt::Key_B,Qt::NoModifier);
    QCoreApplication::postEvent(item,event);
}

ma​​in.cpp:

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    QObject* root = engine.rootObjects()[0];
    ClickHandler* ch = new ClickHandler(&engine, &app);
    QQuickItem* button = root->findChild<QQuickItem*>("button");

    QObject::connect(button, SIGNAL(clicked()), ch, SLOT(handleClick()));

    return app.exec();
}

ma​​in.qml:

ApplicationWindow {
    objectName: "rootWindow"
    visible: true
    Column {
        TextField {
            id: textId1
            objectName: "text1"
            text: "text1 message"
            focus: true
        }

        TextField {
            id: textId2
            objectName: "text2"
            text: "text2 message"
        }
        Button {
            objectName: "button"
            text : "Click me";
        }
    }
}

【问题讨论】:

  • 嗨@Shnd!你解决了你最后的问题吗?你看到我的回答了吗?我看到你在这里使用activeFocusItem,所以我想你的一些老问题已经解决了。如果是这样,请将这些问题标记为已解决或再次寻求帮助。
  • 是的@Tarod。实际上,感谢您和其他一些人,我解决了许多问题。我正在努力寻找一些空闲时间来为这些问题写下我自己的答案。但是把这一切放在一边,谢谢你所有的时间你提出了我的问题并给了我很好的回应。 :)

标签: qt qml qkeyevent


【解决方案1】:

如果您实现onPressed 处理程序,您可以看到问题出在哪里。

ApplicationWindow {
    objectName: "rootWindow"
    visible: true
    Column {
        TextField {
            id: textId1
            objectName: "text1"
            text: "text1 message"
            focus: true

            Keys.onPressed: {
                console.log("textId1: " + event.key + " : " + event.text)
            }
        }

        TextField {
            id: textId2
            objectName: "text2"
            text: "text2 message"

            Keys.onPressed: {
                if (event.key === Qt.Key_B) {
                    console.log("I like B's but not QString's")
                }
            }
        }

...

此代码打印qml: textId1: 66 :,因为该键的文本为空。

您需要使用以下方式创建事件:

Qt::Key key = Qt::Key_B;
QKeyEvent* event = new QKeyEvent(QKeyEvent::KeyPress,
                                 key,
                                 Qt::NoModifier,
                                 QKeySequence(key).toString());

【讨论】:

  • 谢谢,这正是问题所在。你能告诉我为什么使用 Qt::Key_B 是不够的吗?这是否意味着您要发送按键事件 B(无论 B 键对项目意味着什么)?如果你想将键 B 映射到其他文本,你能给我一个实际的例子吗?!
  • 我刚刚更新了答案。实际上,在QKeyEvent 中设置文本并不总是必要的。您可以让key 执行不同的任务。例如,获取左光标键Qt.Key_Left 并移动Item。关于你的最后一个问题,恐怕我没有一个很好的例子可以给你:(我想这真的取决于你想要达到的目标。
猜你喜欢
  • 2015-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
相关资源
最近更新 更多