C++ 如何将鼠标/光标添加到非触摸式手机的QML应用程序?
这是一个简单的基于Qt/QML的web浏览器“MeeBro”(Qt4.7)& QtWebKit 1.0),它由几个qml文件和一个javascript文件组成: 它有一个元素C++ 如何将鼠标/光标添加到非触摸式手机的QML应用程序?,c++,qt,touch,qml,emulation,C++,Qt,Touch,Qml,Emulation,这是一个简单的基于Qt/QML的web浏览器“MeeBro”(Qt4.7)& QtWebKit 1.0),它由几个qml文件和一个javascript文件组成: 它有一个元素矩形,其中设置了URL: property string urlString: "http://anssiko.github.com/" 你能告诉我如何添加一个可以被joystik移动的光标吗(◀, ▲, ▼, ▶) 可以通过点击joystik中心按钮点击链接吗 MeeBro基于Qt/QML演示: 此外,我认为此链接可能
矩形
,其中设置了URL:
property string urlString: "http://anssiko.github.com/"
你能告诉我如何添加一个可以被joystik移动的光标吗(◀, ▲, ▼, ▶) 可以通过点击joystik中心按钮点击链接吗
MeeBro基于Qt/QML演示:
此外,我认为此链接可能会有所帮助:您可以实现一个假光标,类似这样:
#include <QTest>
#include <QGuiApplication>
class FakeCursor : public QObject {
Q_OBJECT
Q_PROPERTY(QPoint pos READ pos WRITE setPos NOTIFY posChanged)
Q_PROPERTY(bool visible READ visible NOTIFY visibleChanged)
enum Direction { Up = 0, Down, Left, Right };
QPoint _pos;
qreal step;
bool _visible;
signals:
void posChanged();
void visibleChanged();
public:
FakeCursor() : step(1), _visible(true) {}
void setPos(QPoint p) {
if (p != _pos) {
_pos = p;
emit posChanged();
}
}
bool visible() const { return _visible; }
QPoint pos() const { return _pos; }
public slots:
void move(int d) {
switch (d) {
case Up: _pos.ry() -= step; break;
case Down: _pos.ry() += step; break;
case Left: _pos.rx() -= step; break;
case Right: _pos.rx() += step; break;
}
emit posChanged();
}
void setStep(qreal s) { if (s) step = s; }
void toggleVisible() { _visible = !_visible; emit visibleChanged(); }
void click() {
QWindow * w = QGuiApplication::allWindows()[0];
QTest::touchEvent(w, 0).press(0, _pos, w).release(0, _pos, w);
// try this instead if the touchevent doesn't work
// QTest::mouseClick(QGuiApplication::allWindows()[0], Qt::LeftButton, Qt::NoModifier, _pos);
}
};
然后,您可以在QML中的所有内容之上创建一个游标元素:
Rectangle {
width: 4
height: 4
radius: 2
color: "red"
x: Cursor.pos.x
y: Cursor.pos.y
visible: Cursor.visible
}
您可以使用Keys
attached属性来控制它。在下面的示例中,它被设置为使用键盘箭头和空格键,您可以将它们更改为手机的控件:
Item {
focus: true
Keys.onPressed: {
switch (event.key) {
case Qt.Key_Up: Cursor.move(0); break;
case Qt.Key_Down : Cursor.move(1); break;
case Qt.Key_Left: Cursor.move(2); break;
case Qt.Key_Right: Cursor.move(3); break;
case Qt.Key_Space : Cursor.click(); break;
}
}
Component.onCompleted: {
Cursor.setStep(2) // to set the speed
// Cursor.toggleVisible() // to show/hide it
// Cursor.pos = Qt.point(50,50) // to move it quickly to a needed position
}
}
您还需要将QT+=testlib
添加到项目文件中。如果模拟触摸事件不起作用,请尝试对其进行注释并使用注释后的鼠标单击事件。您可能还需要更改一些QtQuick类,因为此示例与Qt5有关,我没有Qt4
Item {
focus: true
Keys.onPressed: {
switch (event.key) {
case Qt.Key_Up: Cursor.move(0); break;
case Qt.Key_Down : Cursor.move(1); break;
case Qt.Key_Left: Cursor.move(2); break;
case Qt.Key_Right: Cursor.move(3); break;
case Qt.Key_Space : Cursor.click(); break;
}
}
Component.onCompleted: {
Cursor.setStep(2) // to set the speed
// Cursor.toggleVisible() // to show/hide it
// Cursor.pos = Qt.point(50,50) // to move it quickly to a needed position
}
}