1

QKeyEventカスタム( Keypress key Bなど) をQML ファイル内のアイテム ( などTextField)に送信しようとしています。これは私が書いたもので、動作しません。アイテム ( ) がイベントを取得していないようです (のテキストに文字が追加されていないため、これを想定しています)。クラスでシグナルをキャプチャしてから、スロットでカスタムを投稿しようとしましたが、ここでは.TextFieldBTextFieldclick ClickHandlerhandleClick eventfocused itemTextField

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";
        }
    }
}
4

1 に答える 1

2

ハンドラーを実装すると、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());
于 2015-12-01T12:00:42.407 に答える