PDA

View Full Version : keyPressEvent doesn't work



8Observer8
24th August 2014, 11:42
Hi

This is my steps:

- I created the "Qt Widget Application"
- I opened the "MainWindow.ui" file and put on it the "mdiArea"
- I created the "Qt Designer Form Class" and wrote in the "MainWindow.h" and "MainWindow.cpp" files:


private:
Ui::MainWindow *ui;
FirstWindow *m_firstWindow;
SecondWindow *m_secondWindow;
ThirdWindow *m_thirdWindow;


MainWindow.cpp


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

m_firstWindow = new FirstWindow;
m_secondWindow = new SecondWindow;
m_thirdWindow = new ThirdWindow;

QMdiSubWindow *w1 = ui->mdiArea->addSubWindow( m_firstWindow );
ui->mdiArea->addSubWindow( m_secondWindow );
ui->mdiArea->addSubWindow( m_thirdWindow );

ui->mdiArea->cascadeSubWindows();

w1->resize( 500, 500 );
}

- I opened "FirstWindow.h" and "FirstWindow.cpp" files and wrote:
FirstWindow.h


private:
Ui::FirstWindow *ui;
void keyPressEvent( QKeyEvent *event );
};

FirstWindow.cpp


void FirstWindow::keyPressEvent(QKeyEvent *event)
{
qDebug() << "keyPressEvent";
}

- I ran the application and pressed on the keys. But I didn't see the text "keyPressEvent" on the "Application Output"

Thank you!

anda_skoa
24th August 2014, 13:15
Does the FirstWindow instance have focus when you press a key?

Key events are delivered to the widget which has keyboard focus, see QApplication::focusWidget().

Cheers,
_

8Observer8
24th August 2014, 16:58
I clicked on the window with the name "First Window" and press the "Space" key. Nothing happened.

10577

Added after 11 minutes:

You are right. I added the button on the window:


void FirstWindow::on_pushButton_clicked()
{
qDebug() << focusWidget()->windowTitle();
}


Output:

""

I don't understand why.

anda_skoa
24th August 2014, 21:15
Window title is not really helpful.
Check the value of fodusWidget() vs. "this"

Cheers,
_

budda
25th August 2014, 07:59
not sure if this helps but
in your header file I thought event method classes were supposed to be:


protected:
void keyPressEvent( QKeyEvent *event );

private:

public:

wysota
25th August 2014, 08:11
I clicked on the window with the name "First Window" and press the "Space" key. Nothing happened.

Did you set QWidget::focusPolicy for the widget appropriately? If widget has Qt::NoFocus then it won't accept focus and thus won't receive key events.

8Observer8
26th August 2014, 09:17
I wrote it the constructor "setFocusPolicy( Qt::StrongFocus );"


FirstWindow::FirstWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::FirstWindow)
{
ui->setupUi(this);
setFocusPolicy( Qt::StrongFocus );
}

I added on the window the button:


void FirstWindow::on_pushButton_clicked()
{
if ( focusWidget() == this ) {
qDebug() << "focusWidget() == this";
} else {
qDebug() << "focusWidget() != this";
}
}


Output:


focusWidget() != this

wysota
26th August 2014, 09:36
When you click the button, it is most likely that the button gets the focus. Set the button's focus policy to Qt::NoFocus and try again. Better yet reimplement focusInEvent() and focusOutEvent() for your widget and print debug statements there to see when it receives and loses focus.

8Observer8
27th August 2014, 05:29
Yes, I see! I added Qt::NoFocus for the button:


FirstWindow::FirstWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::FirstWindow)
{
ui->setupUi(this);
setFocusPolicy( Qt::StrongFocus );
ui->pushButton->setFocusPolicy( Qt::NoFocus );
}


I added focusInEvent() and focusOutEvent():


#include "FirstWindow.h"
#include "ui_FirstWindow.h"
#include "QDebug"

FirstWindow::FirstWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::FirstWindow)
{
ui->setupUi(this);
setFocusPolicy( Qt::StrongFocus );
ui->pushButton->setFocusPolicy( Qt::NoFocus );
}

FirstWindow::~FirstWindow()
{
delete ui;
}

void FirstWindow::keyPressEvent( QKeyEvent *event )
{
qDebug() << "FirstWindow: keyPressEvent";
}

void FirstWindow::focusInEvent( QFocusEvent *event )
{
qDebug() << "FirstWindow: focusInEvent";
}

void FirstWindow::focusOutEvent( QFocusEvent *event )
{
qDebug() << "FirstWindow: focusOutEvent";
}

void FirstWindow::on_pushButton_clicked()
{
if ( focusWidget() == this ) {
qDebug() << "focusWidget() == this";
} else {
qDebug() << "focusWidget() != this";
}
}


When I click on the button I see:


focusWidget() == this


I will write in the constructor this insruction "setFocusPolicy( Qt::StrongFocus );" for all my MDI subwidnows