PDA

View Full Version : About press esc key to exit fullscreen



kenchan
2nd December 2012, 15:31
Hi All,
i have use designer to design a mainwidow which have a stackedWidget ,and the stackedWidget has several pages. i want to the page display fullscreen , and then pressed 'esc' key to exit fullscreen. but didn't respond. didn't print "esc". what's problem?



void MainWindow::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Escape && !event->isAutoRepeat())
{qDebug() << "esc";

if(ui->stackedWidget->isFullScreen())
{
ui->stackedWidget->setWindowFlags(Qt::SubWindow);
ui->stackedWidget->showNormal();
}
}
}

Please give me a advice!

Thanks and Best Regards!

ken

Added after 1 4 minutes:

i have set the focus, but didn't exit fullscreen also.

wysota
2nd December 2012, 22:37
What is the focus policy of your MainWindow instance?

kenchan
3rd December 2012, 01:14
Hi wysota,
i have try to use all of options about focusPolicy such as StrongFocus,wheelFocus, tabFocus, clickfocus .but didn't respond also.

please give me some advice to handle it.

TKS.

ken

anda_skoa
3rd December 2012, 01:29
Hmm, what about using a QAction that has Escape as its short cut key sequence?

You can then also add it to the menu, making it discoverable, to the context menu, etc.

Cheers,
_

kenchan
3rd December 2012, 01:57
Hi anda_skoa,

your mean is add 'esc' QAction to the menu?

i have try add eventfilter, cann't print 'esc', didn't use 'esc' key also. code as below:

bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *k = (QKeyEvent *)event;

if(k->key() == Qt::Key_Escape)
{qDebug() << "esc";

if(ui->stackedWidget->isFullScreen())
{
ui->stackedWidget->setWindowFlags(Qt::SubWindow);
ui->stackedWidget->showNormal();
}
return true;
}
}
else
{
return QWidget::eventFilter(object, event);
}
}

please give some example if you can.

TKS.

ken

wysota
3rd December 2012, 02:04
Hi wysota,
i have try to use all of options about focusPolicy such as StrongFocus,wheelFocus, tabFocus, clickfocus .but didn't respond also.

please give me some advice to handle it.
For key events to be delivered to a widget, there is a requirement that the widget needs to have focus. For that two things need to happen:

1. the widget needs to accept focus (done through a focus policy other than NoFocus)
2. the widget needs to have focus currently (done e.g. by clicking on the widget)

If you didn't fulfill step 2, then doing step 1 is not sufficient.

kenchan
3rd December 2012, 04:11
Hi wysota,

the step 2 happened when the programming running? if yes, i have clicked the widget before press 'esc' key.

and the step 1 I have seted StrongFocus . pls see as below:

ui->stackedWidget->setFocusPolicy(Qt::StrongFocus);

TKS.

ken

Added after 1 51 minutes:

Hi All,

i have completed used eventfilter.

thank you very much for giving me advice!

ken

wysota
3rd December 2012, 10:43
and the step 1 I have seted StrongFocus . pls see as below:

ui->stackedWidget->setFocusPolicy(Qt::StrongFocus);

That sets focus on the stacked widget, not on the main window.

anda_skoa
3rd December 2012, 10:44
Hi anda_skoa,

your mean is add 'esc' QAction to the menu?


Yes, a QAction with the escape key being its shortcut QKeySequence



i have try add eventfilter, cann't print 'esc', didn't use 'esc' key also. code as below:


That is another option, make sure to install it on the QApplication instance.

Cheers,
_