PDA

View Full Version : Some queries on the architecture of Qt character handling



balasaravanan
9th November 2011, 05:42
Hi All,


I have implemented a QT Key board plugin similar to the one given in Qt documentation

void QWSInputKbPrivate::readKeycode()
{

KEY_EVENT key[MAX_EVENTS_TO_READ];
int digit;
int events,current=0;
KEYPAD_KEYS keypad_key;
int pressed;
int keycode, unicode;

events = QT_READ(m_fd, reinterpret_cast<char *>(&key), MAX_EVENTS_TO_READ*sizeof(KEY_EVENT)) / sizeof(KEY_EVENT) ;

while (events!=current) {

keypad_key=digitToKeyArr[ key[current].column][key[current].row ];
pressed=key[current].key_press_time;
keycode = keysToQTArr[keypad_key].keycode;
unicode = keysToQTArr[keypad_key].unicode;

printf("readKeycode ---> key: %d press=%d, keycode=%d, unicode=%x %c\n", keypad_key, pressed, keycode, unicode, unicode);

m_handler->processKeyEvent(unicode, keycode, Qt::NoModifier, false, false);
current ++ ;
}
}


I am able to compile the key board plugin. I am able to set the QWS_KEYBOARD variable to this kbd plugin name. When starting an Qt application as server/client, the above printf function gets executed. So i understand that the character input hardware device is opened successfully and it is read correctly.


In the application, I have a single class deriving from QWidget. I create a instance of this class in the main program and show() it. This DiallerForm contains a single QLineEdit widget to get user input from key pad.

class DiallerForm : public QWidget
{
Q_OBJECT

public:
DiallerForm();

private slots:
void keyPressEvent(QKeyEvent *event);
bool event(QEvent *event);
private:
Ui::DiallerForm ui;
};

void DiallerForm::keyPressEvent(QKeyEvent *event) {
printf("DiallerForm::keyPressEvent \n");
}

bool DiallerForm::event(QEvent *event) {
switch(event->type()) {
case QEvent::KeyPress:
case QEvent::KeyRelease:
printf("DiallerForm::event - key press \n");
break;
default:
break;
}
return QWidget::event(event);
}

The main() code is something like this


int main(int argc, char **argv)
{
QApplication app(argc,argv);
DiallerForm *df = new DiallerForm();
df->show();
app.exec();
}

I have compiled this application and running it as a server .

What i am expecting is that keyPressEvent slot be called whenever the key is pressed in the key pad. Instead , I get only the event:KeyPress function being called. And also the characters that i press in keypad are not displayed in the QLineEdit.

I can see that the kbd plugin part of the code is able to read the device and send it out. But the application is not able to get the QKeyEvent and subsequently the QLineEdit is also not updated.

What am i missing here. Could any one throw some light on how the key pad handling has to be done.

wysota
9th November 2011, 09:54
Apparently your line edit doesn't have focus so it is the form that gets the event and since it can't accept focus, the event is ignored by QWidget::event(). The easiest solution is to make the line edit the focus proxy for the form. Alternatvely just set focus on the line edit.

balasaravanan
9th November 2011, 10:44
I set the focus proxy for the line edit in the constructor of the form. Still it appears that line edit is not able to receive the QKeyEvent. I guess there is something far more monstrous is happening. Just not able to figure out what is happening.

DiallerForm::DiallerForm()
{
ui.setupUi(this);
setWindowFlags(Qt::CustomizeWindowHint);

setFocusProxy(ui.dialTextEdit);
}


Can i expect the QKeyEvent to get delivered to the containing form ?

wysota
9th November 2011, 18:15
Can i expect the QKeyEvent to get delivered to the containing form ?
If it doesn't itself accept focus then no. You can make it accept focus (change the focus policy) and keyPressEvent() will probably be getting called.

balasaravanan
10th November 2011, 09:53
I have tried setting the focus policy for the form .
I tried setting the focus proxy to line edit.
I also tried gradKeyboard for the form.

None of the options display the keyed in characters in the line edit area. The QKeyEvent is not getting delivered for some reason i am not able to comprehend.

wysota
10th November 2011, 13:24
So far we are talking about receiving key events at all, not about displaying them in the line edit. If you want to display them in the line edit without additional coding then the line edit needs to have focus and not the form widget. It seems your line edit doesn't have focus or else you wouldn't be receiving any key related events in the form.

balasaravanan
11th November 2011, 04:28
Hi wysota,

Thanks for your replies. I really appreciate your help.

Please find the attached tar ball for the source code. It contains a simple form with a line edit and two buttons.

When i compile this code for x86, and run the program, i can see the keyboard input in the line edit. ie after i click on the line edit, the line edit comes to focus and gets all the keyboard inputs.

When i compile the same code for the embedded board and run the program after setting QWS_KEYBOARD and starting the program as server, i do not get the key board input in the line edit. But i can see that the key board driver is sending the correct key events. Also the cursor blink is seen the line edit (ie the line edit has focus) .

I am not sure if i need to have a separate program to run as server and start this program as a client in the embedded environment.

Does it have to do anything with the driver program and how it is programmed. The crux of the driver code is as follows.


void QWSInputKbPrivate::readKeycode()
{

KEY_EVENT key[MAX_EVENTS_TO_READ];
int digit;
int events,current=0;
KEYPAD_KEYS keypad_key;
int pressed;
int keycode, unicode;

events = QT_READ(m_fd, reinterpret_cast<char *>(&key), MAX_EVENTS_TO_READ*sizeof(KEY_EVENT)) / sizeof(KEY_EVENT) ;

while (events!=current) {

keypad_key=digitToKeyArr[ key[current].column][key[current].row ];
pressed=key[current].key_press_time;
keycode = keysToQTArr[keypad_key].keycode;
unicode = keysToQTArr[keypad_key].unicode;

printf("readKeycode ---> key: %d press=%d, keycode=%d, unicode=%x %c\n", keypad_key, pressed, keycode, unicode, unicode);

m_handler->processKeyEvent(unicode, keycode, Qt::NoModifier, false, false);
current ++ ;
}
}

Thanks and regards,
Bala

wysota
11th November 2011, 10:29
In my opinion your line edit doesn't have focus if you are receiving event() calls on the form.