PDA

View Full Version : strange behaviour with Qt::key_space



franco.amato
12th April 2010, 04:42
Hi,
I already posted this thread in the programming section and get no replies.
I hope ho have more luck here.
I'm implementing my:



void myWidget::keyPressEvent( QKeyEvent* pe )
{
..code ..
}

where I catch some keys. I have a problem with the space. I would run a routine when I press the space key so:



// start/stop
case Qt::Key_Space:
{
qDebug() << "WaveWidget::keyPressEvent";
bool playing;
m_wave->getCurrentChannel()->isPlaying( &playing );
if( playing == false )
playSound();
else
StopSound();
break;
}


It simply doesn't work. If I change Key_Space with another key I have no problems.
Which can be the problem?

Regards

wysota
12th April 2010, 10:21
So what exactly is the "strange behaviour"? You do know that the space key has a special meaning for dialogs and may be intercepted higher in the event processing chain, right?

franco.amato
12th April 2010, 15:49
So what exactly is the "strange behaviour"? You do know that the space key has a special meaning for dialogs and may be intercepted higher in the event processing chain, right?

Wysota the "strange behaviour" is that "nothing happens".
So I can not use the space?

JD2000
12th April 2010, 18:06
No, you can catch the space bar events, just make sure that the myWidget constructor gives the widget strong focus:
myWidget::myWidget(QWidget *parent)
{
setFocusPolicy(Qt::StrongFocus);
...
}

You may also need to call QKeyEvent::accept() but this should not be necessary.

franco.amato
12th April 2010, 19:15
No, you can catch the space bar events, just make sure that the myWidget constructor gives the widget strong focus:
myWidget::myWidget(QWidget *parent)
{
setFocusPolicy(Qt::StrongFocus);
...
}

You may also need to call QKeyEvent::accept() but this should not be necessary.

Hi I already set the focul policy to strongfocus but nothing change.
Seems the event is cached in another place

wysota
13th April 2010, 01:03
Wysota the "strange behaviour" is that "nothing happens".
Well... "nothing happens" is the default behaviour so I don't see anything strange in it :)

So I can not use the space?
Sure you can. Reimplement QObject::event() for your class or don't derive from QDialog and remember to give your widget (just make sure it's the right one) some sane focus policy.

franco.amato
13th April 2010, 03:31
Well... "nothing happens" is the default behaviour so I don't see anything strange in it :)
:-) OK


Sure you can. Reimplement QObject::event() for your class or don't derive from QDialog and remember to give your widget (just make sure it's the right one) some sane focus policy.

Hi my class derive from QWidget and I set

setFocusPolicy( Qt::StrongFocus ); as focus policy.
Can you explain me better what I have to reimplement?

nish
13th April 2010, 05:08
as suggested earlier, reimplement event() or eventFilter().

wagmare
13th April 2010, 06:39
use like this

bool cmylass:: eventFilter(QObject *ob, QEvent *e)
{
if(ob == logout && e->type() == QEvent::KeyPress) {
const QKeyEvent *ke = static_cast<QKeyEvent *>(e);
if(ke->key()==)Qt::Key_Space{
//do your coding std ..
}
return true;
}
return QWidget::eventFilter(ob, e);
}