PDA

View Full Version : QLineEdit and keyPressEvent(QKeyEvent *)



fear
2nd June 2008, 19:21
I've subclassed QLineEdit in order to obtain field that will be used for acquiring accelerators from user (sth like class derived from QListViewItem in QtDesigner). Unfortunately despite the fact I've reimplemented keyPressEvent(QKeyEvent *) I still cannot catch Ctrl+A and Ctrl+U. I browsed source code of QLineEdit but I still have no idea.



#ifndef LINEACCEL_H
#define LINEACCEL_H

#include <qkeysequence.h>
#include <qlineedit.h>

class LineAccel : public QLineEdit
{
Q_OBJECT

public:
LineAccel(const QKeySequence &, QWidget * parent = 0, const char * name = 0);
~LineAccel();

void setAccel(const QKeySequence &);
QKeySequence accel();

protected:
QPopupMenu * createPopupMenu();
void keyPressEvent(QKeyEvent *);

private:
int translateModifiers(int);

private:
QKeySequence ks;
};

#endif
#include "lineaccel.h"

LineAccel::LineAccel(const QKeySequence & accel, QWidget * parent, const char * name) : QLineEdit(parent, name)
{
setReadOnly(true);

setAccel(accel);
}

LineAccel::~LineAccel()
{
}

void LineAccel::setAccel(const QKeySequence & a)
{
ks = a;

setText(ks);
}

QKeySequence LineAccel::accel()
{
return ks;
}

QPopupMenu * LineAccel::createPopupMenu()
{
return 0;
}

void LineAccel::keyPressEvent(QKeyEvent * e)
{
int nextKey = e->key();

if (nextKey == QObject::Key_Control ||
nextKey == QObject::Key_Shift ||
nextKey == QObject::Key_Meta ||
nextKey == QObject::Key_Alt)
return;

int modifier = translateModifiers(e->state());

nextKey |= modifier;

setAccel(QKeySequence(nextKey));
}

int LineAccel::translateModifiers(int state)
{
int result = 0;

if (state & QObject::ShiftButton)
result |= QObject::SHIFT;

if (state & QObject::ControlButton)
result |= QObject::CTRL;

if (state & QObject::MetaButton)
result |= QObject::META;

if (state & QObject::AltButton)
result |= QObject::ALT;

return result;
}

fear
3rd June 2008, 09:26
Thing I know is that Ctrl+A is never passed to keyPressEvent. I've installed my event filter but it also didn't help.

wysota
3rd June 2008, 11:31
I'd suggest "stealing" Designer's code.

fear
3rd June 2008, 13:43
Designer uses QListViewItem. I switched to KListView because there is no hope to find out what's wrong with QLineEdit. Something is filtering Ctrl+A before sequence reaches recently installed event handler. Some magic behind ;[

wysota
3rd June 2008, 14:24
QObject::event() probably :)

And Ctrl+A is probably a shortcut to "Select all", so you might need to disable the shortcut.

fear
3rd June 2008, 19:12
But how disable shortcut?

event() - no.

I think event is first passed through event filter chain, then to event() and finally to specific event function:

event
|
event filter (n) -> event filter (n - 1) -> ... -> event filter (0) -> event() -> keyPressEvent(), where event filter (n) is latest installed event filter

I cannot catch this sequence at event filter (n) level so it's obvious I won't catch it at event() level.

wysota
3rd June 2008, 20:31
But how disable shortcut?
Hard to say without looking at the code. I'd say it's an action associated with the context menu of the widget.


I cannot catch this sequence at event filter (n)
What did you filter it with?