PDA

View Full Version : Catch a Key, like designer



aekilic
6th May 2009, 09:58
Dear All

I started a thread yesterday, but I dont know why and how, it has been moved to newbei

What I would like to do is, like in designer, I would like to create a line, and when a press a line it should write on the line what I have pressed.

Is the a example code for this, or where could I find the codes for the designer.

spirit
6th May 2009, 10:03
I started a thread yesterday, but I dont know why and how, it has been moved to newbei

because it's newbie question, just install event filter to your line edit and process QKeyEvent.

wagmare
6th May 2009, 10:03
can u give more information about ur need ..

aekilic
6th May 2009, 10:10
What I need is

I would like to know which key has been pressed for a qline

spirit
6th May 2009, 10:12
here is an example, is it hard to write it by youself?


...
lineEdit = new QLineEdit();
lineEdit->installEventFilter(this);
...
bool MyWidget::eventFilter(QObject *o, QEvent *e)
{
if (o == lineEdit && e->type() == QEvent::KeyPress) {
const QKeyEvent *ke = static_cast<QKeyEvent *>(e);
const QKeySequence ks(ke->modifiers() + ke->key());
lineEdit->setText(ks.toString());
}
return QWidget::eventFilter(o, e);
}

nothing special, right?
that's why you question is newbie. ;)

wagmare
6th May 2009, 10:13
use keypressEvent and check with the respected key ... QKeyEvent has a function
bool operator== ( QKeyEvent * e, QKeySequence::StandardKey key ) where standard keys will help u ..

aekilic
6th May 2009, 10:20
Dear spirit

Thank you very much for your reply yes you right it was easy but the problem is, we wrote a program which is open source there is a company who is selling a similar program whoes owner is treatening us everyday that we made a program like this. So I am not really thinking 100% sorry

aekilic
7th May 2009, 07:39
I got another problem for this, the code you have sent is working fine spirit for CTRL

When I press shift + r , it writes to the line SHIFT+RR or for ALT it writes ALT+Rr

Can any ona tell me the reason?

spirit
7th May 2009, 07:43
try this


lineEdit->setText(ks.toString(QKeySequence::NativeText));

aekilic
7th May 2009, 07:53
no I still get

Shift+HH when I press shift+h

spirit
7th May 2009, 07:54
what system do you use?
under Windows this code works fine.

wagmare
7th May 2009, 07:56
in linus also the same problem arise .. if i press key "a" it shows "Aa" ...

spirit
7th May 2009, 07:57
from Qt Assistant


QKeySequence::NativeText 0 The key sequence as a platform specific string. This means that it will be shown translated and on the Mac it will resemble a key sequence from the menu bar. This enum is best used when you want to display the string to the user.
QKeySequence::PortableText 1 The key sequence is given in a "portable" format, suitable for reading and writing to a file. In many cases, it will look similar to the native text on Windows and X11.

aekilic
7th May 2009, 07:58
I use vista Qt4.5.0

spirit
7th May 2009, 08:13
take a look implementation of QtKeySequenceEdit in QTDIR\tools\shared\qtpropertybrowser\qttreepropert ybrowser.cpp.

aekilic
7th May 2009, 08:36
I accidentaly use this code


if (o == lineDeNo && e->type() == QEvent::KeyPress) {
const QKeyEvent *ke = static_cast<QKeyEvent *>(e);
const QKeySequence ks(ke->modifiers() + ke->key());
lineDeNo->setText(ks.toString(QKeySequence::NativeText));
}
if (o == lineMa && e->type() == QEvent::KeyPress) {
const QKeyEvent *ke = static_cast<QKeyEvent *>(e);
const QKeySequence ks(ke->modifiers() + ke->key());
lineMa->setText(ks.toString());
lineDeNo->setText(ks.toString(QKeySequence::PortableText));
}

When editting lineDoNo it doesnt work but When I edit lineMa, lineDeNo works fine,

I try

if (o == lineDeNo && e->type() == QEvent::KeyPress) {
const QKeyEvent *ke = static_cast<QKeyEvent *>(e);
const QKeySequence ks(ke->modifiers() + ke->key());
lineDeNo->setText(ks.toString(QKeySequence::PortableText));
}

but it doesnt work...

spirit
7th May 2009, 08:40
do you install event filters for both lineedits?

aekilic
7th May 2009, 08:47
yes, I do install for both lines

spirit
7th May 2009, 08:49
ok, can you attach compilable example which contains this bug?

aekilic
7th May 2009, 08:57
do you mean the code for this area?

spirit
7th May 2009, 08:58
minimal compilable example which reproduce your problem.

aekilic
7th May 2009, 09:09
lineDeNo->installEventFilter(this);
lineMa->installEventFilter(this);




bool formAyarlar::eventFilter(QObject *o, QEvent *e)
{
if (o == lineDeNo && e->type() == QEvent::KeyPress) {
const QKeyEvent *ke = static_cast<QKeyEvent *>(e);
const QKeySequence ks(ke->modifiers() + ke->key());
lineDeNo->setText(ks.toString(QKeySequence::NativeText));
}else if (o == lineMa && e->type() == QEvent::KeyPress) {
const QKeyEvent *ke = static_cast<QKeyEvent *>(e);
const QKeySequence ks(ke->modifiers() + ke->key());
// lineMa->setText(ks.toString());
lineMa->setText(ks.toString(QKeySequence::PortableText));
}
}

wysota
7th May 2009, 09:20
This is not compilable :) A compilable example is something that includes a main() and the piece of code you want tested so that if one writes qmake && make it will compile.

And please don't start multiple threads on the same subject. Moving to newbie (again).

spirit
7th May 2009, 09:29
this how should look like "minimal compilable example"


#include <QtGui>

class TestWidget: public QWidget
{
Q_OBJECT

public:
TestWidget(QWidget *parent = 0)
: QWidget(parent)
{
QVBoxLayout *vbl = new QVBoxLayout(this);
m_lineEditNativeText = new QLineEdit();
m_lineEditPortableText = new QLineEdit();

m_lineEditNativeText->installEventFilter(this);
m_lineEditPortableText->installEventFilter(this);

vbl->addWidget(m_lineEditNativeText);
vbl->addWidget(m_lineEditPortableText);
}

protected:
bool eventFilter(QObject *o, QEvent *e)
{
if (e->type() == QEvent::KeyPress) {
const QKeyEvent *ke = static_cast<QKeyEvent *>(e);
const QKeySequence ks(ke->modifiers() + ke->key());
if (o == m_lineEditNativeText)
m_lineEditNativeText->setText(ks.toString(QKeySequence::NativeText));
else if (o == m_lineEditPortableText)
m_lineEditPortableText->setText(ks.toString());
}
return QWidget::eventFilter(o, e);
}

private:
QLineEdit *m_lineEditNativeText;
QLineEdit *m_lineEditPortableText;
};

int main(int argc, char **argv)
{
QApplication app(argc, argv);

TestWidget tw;
tw.show();

return app.exec();
}

#include "main.moc"

works fine, but symbol duplicated.

spirit
7th May 2009, 09:38
intresting thing, if modify my code in this way


if (o == m_lineEditNativeText) {
m_lineEditNativeText->setText(ks.toString(QKeySequence::NativeText));
m_lineEditPortableText->setText(ks.toString());
}// else if (o == m_lineEditPortableText)
// m_lineEditPortableText->setText(ks.toString());

then a string in m_lineEditPortableText contains text without duplicating letter.

spirit
7th May 2009, 09:47
crap, it's my mistake, eventFilter should look like


bool eventFilter(QObject *o, QEvent *e)
{
if (e->type() == QEvent::KeyPress) {
const QKeyEvent *ke = static_cast<QKeyEvent *>(e);
const QKeySequence ks(ke->modifiers() + ke->key());
if (o == m_lineEditNativeText) {
m_lineEditNativeText->setText(ks.toString(QKeySequence::NativeText));
return true;
} else if (o == m_lineEditPortableText) {
m_lineEditPortableText->setText(ks.toString());
return true;
}
}
return QWidget::eventFilter(o, e);
}

:o

aekilic
7th May 2009, 09:52
Let me try

aekilic
7th May 2009, 10:01
yes works perfect thank you very much spirit you are my life saver!