myinputpanelcontext.cpp
2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "myinputpanelcontext.h"
#include "myinputpanel.h"
#include <QDebug>
MyInputPanelContext::MyInputPanelContext()
{
inputPanel = new MyInputPanel;
connect(inputPanel, SIGNAL(characterGenerated(QChar)), SLOT(sendCharacter(QChar)));
}
//! [0]
MyInputPanelContext::~MyInputPanelContext()
{
delete inputPanel;
}
//! [1]
bool MyInputPanelContext::filterEvent(const QEvent* event)
{ //点击输入栏时发生,软件盘弹出事件
if (event->type() == QEvent::RequestSoftwareInputPanel) {
// updatePosition();//更新位置
inputPanel->move(450,200);
inputPanel->show();//显示
inputPanel->clearLineEditData();
return true;
} else if (event->type() == QEvent::CloseSoftwareInputPanel) {
inputPanel->hide();
return true;
}
return false;
}
//! [1]
QString MyInputPanelContext::identifierName()
{
return "MyInputPanelContext";
}
void MyInputPanelContext::reset()
{
}
bool MyInputPanelContext::isComposing() const
{
return false;
}
QString MyInputPanelContext::language()
{
return "en_US";
}
//! [2]
void MyInputPanelContext::sendCharacter(QChar character)
{
// QPointer<QWidget> w = focusWidget();
//获取输入窗口指针
QPointer<QWidget> w = inputPanel->getFocusedWidget();
if (!w)
return;
qDebug() << "send"<<endl;
qDebug() << "character" << character << endl;
if(character == 8)
qDebug() << "8" << endl;
//发送按键值直接到输入窗口
//QKeyEvent参数:按键类型,编码,修饰符,最后才是发送值
QKeyEvent keyPress(QEvent::KeyPress, character.unicode(), Qt::NoModifier, QString(character));
QApplication::sendEvent(w, &keyPress);//w为事件接收者
if (!w)
return;//
//发送释放信号
QKeyEvent keyRelease(QEvent::KeyRelease, character.unicode(), Qt::NoModifier, QString());
QApplication::sendEvent(w, &keyRelease);
}
//! [2]
//! [3]
void MyInputPanelContext::updatePosition()
{
QWidget *widget = focusWidget();//返回有光标的窗口部件的指针,这里是两个QLineEdit窗口部件的指针
if (!widget)
return;
QCursor cursor;
QPoint focus = cursor.pos();
// QRect widgetRect = widget->rect();//返回窗口部件的矩形对象
//QPoint panelPos = QPoint(widgetRect.left()+10, widgetRect.bottom() + 12);//以输入光标点处的x,y坐标,来确定输入面板的放置位置信息
QPoint panelPos = QPoint(focus.x() + 16, focus.y() +20);//以输入光标点处的x,y坐标,来确定输入面板的放置位置信息
// qDebug() << panelPos <<endl;
// panelPos = widget->mapToGlobal(panelPos);
qDebug() << panelPos <<endl;
inputPanel->move(panelPos);//移到设置点处
}