如何将 QKeyEvent 发送到特定的 qml 项目?
How to send QKeyEvent to specific qml item?
我正在尝试将自定义 QKeyEvent
(如 Keypress 键 B)发送到我的 [=35] 中的项目(如 TextField
) =]QML 文件。这是我写的,它不起作用。似乎项目 (TextField
) 没有得到我的事件(我假设这是因为没有 B
字符附加到我的 TextField
的文本)。我在 ClickHandler
class 中捕获 click
signal 然后在 handleClick
slot,我尝试 post 自定义 event
到我的 focused item
,这里是 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);
}
main.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();
}
main.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";
}
}
}
如果您实施 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());
我正在尝试将自定义 QKeyEvent
(如 Keypress 键 B)发送到我的 [=35] 中的项目(如 TextField
) =]QML 文件。这是我写的,它不起作用。似乎项目 (TextField
) 没有得到我的事件(我假设这是因为没有 B
字符附加到我的 TextField
的文本)。我在 ClickHandler
class 中捕获 click
signal 然后在 handleClick
slot,我尝试 post 自定义 event
到我的 focused item
,这里是 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);
}
main.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();
}
main.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";
}
}
}
如果您实施 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());