1

I'm trying to send a custom QKeyEvent (like Keypress key B) to an item (like TextField) in my QML file. This is what I've wrote which it doesn't work. It seems that the item (TextField) doesn't get my event (I'm assuming this because no B characters appended to my TextField's text). I'm capturing click signal in my ClickHandler class and then in my handleClick slot, I try to post an custom event to my focused item which in here is a 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";
        }
    }
}
Shnd
  • 1,846
  • 19
  • 35
  • Hi @Shnd! Did you solve your last issues? Did you see my answers? I see you're using here `activeFocusItem` so I suppose some of your old questions are already solved. If so, please mark those questions as solved or ask for help again. – Tarod Dec 01 '15 at 12:05
  • Yes @Tarod. actually i solved many of my problems thanks to you and some other folks. I'm trying to find some spare time in order to write my own answer for those questions. But put all this aside, Thank you for all your time you have put on my questions and give me great responses. :) – Shnd Dec 03 '15 at 22:08

1 Answers1

3

If you implement the onPressed handler you can see where the problem is.

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")
                }
            }
        }

...

This code prints qml: textId1: 66 : because the text for that key is empty.

You need to create the event using something like this:

Qt::Key key = Qt::Key_B;
QKeyEvent* event = new QKeyEvent(QKeyEvent::KeyPress,
                                 key,
                                 Qt::NoModifier,
                                 QKeySequence(key).toString());
Tarod
  • 6,732
  • 5
  • 44
  • 50
  • Thank you, that was exactly what the problem was. Could you just tell me why it is not sufficient to use Qt::Key_B ? doesn't it mean you want to send key press event B (whatever key B means to the item)? Could you give me a practical example were you want to map key B to some other text ?! – Shnd Dec 03 '15 at 21:56
  • I've just updated the answer. Actually, set the text in `QKeyEvent` is not always necessary. You could just get the `key` to do different tasks. For example, get the left cursor key `Qt.Key_Left` and move an `Item`. About your last question, I'm afraid I don't have a good example to give you :( I suppose It really depends on what you want to achieve. – Tarod Dec 04 '15 at 07:49