PDA

View Full Version : How can I avoid a QDialog close?



ricardo
10th May 2009, 23:26
Hi!

If I have QMainWindow I can rewrite
void CEditor::closeEvent(QCloseEvent *event) {
and return event->ignore(); to avoid closing.

How can I do something similar if I have a QDialog? (user press X button or hit ESC)

Thanks.

talk2amulya
10th May 2009, 23:41
closeEvent() is for all widgets, not just main window..so it should work with dialog too..if it doesnt, try setting window modality for dialog to true..that way it will become a TLW(top level widget), and closeEvent() would most certainly come to dialog

ricardo
11th May 2009, 00:18
It seems if I rewrite
void CSequencesDialog::closeEvent(QCloseEvent *event) {
works, but if I try to write
event->accept();
Or
event->ignore();

[/code]
1>.\CSequencesDialog.cpp(475) : error C2027: use of undefined type 'QCloseEvent'
1> c:\qt\2009.01\qt\include\qtgui\../../src/gui/kernel/qwidget.h(83) : see declaration of 'QCloseEvent'
1>.\CSequencesDialog.cpp(475) : error C2227: left of '->accept' must point to class/struct/union/generic type
1>.\CSequencesDialog.cpp(477) : error C2027: use of undefined type 'QCloseEvent'
1> c:\qt\2009.01\qt\include\qtgui\../../src/gui/kernel/qwidget.h(83) : see declaration of 'QCloseEvent'
1>.\CSequencesDialog.cpp(477) : error C2227: left of '->ignore' must point to class/struct/union/generic type
1>Build log was saved at "file://c:\Documents and Settings\Ricardo\Escritorio\iPhone\Kokochiko\Edito r\Editor\Debug\BuildLog.htm"
1>Editor - 4 error(s), 0 warning(s)
[code]

Any idea?

ricardo
11th May 2009, 00:21
Bad news. I read this on Qt docs:

"If the user presses the Esc key in a dialog, QDialog::reject() will be called. This will cause the window to close: The close event cannot be ignored."

So, is that impossible?

Thanks.

aamer4yu
11th May 2009, 06:28
May be you can try overriding QWidget::keyPressEvent or QWidget::keyReleaseEvent
and compare something like -

if(event->key() == Qy::Key_Esc)
event->ignore(); // or do nothing, dont call base call function for this case

ricardo
11th May 2009, 09:41
I have a similar problem if I use keyPressEvent. It seems parameter is not recognized.



>CSequencesDialog.cpp
1>.\CSequencesDialog.cpp(486) : error C2027: use of undefined type 'QKeyEvent'
1> c:\qt\2009.01\qt\include\qtgui\../../src/gui/kernel/qwidget.h(78) : see declaration of 'QKeyEvent'
1>.\CSequencesDialog.cpp(486) : error C2227: left of '->key' must point to class/struct/union/generic type
1>.\CSequencesDialog.cpp(486) : error C2653: 'Qy' : is not a class or namespace name
1>.\CSequencesDialog.cpp(486) : error C2065: 'Key_Esc' : undeclared identifier
1>.\CSequencesDialog.cpp(486) : error C2027: use of undefined type 'QKeyEvent'
1> c:\qt\2009.01\qt\include\qtgui\../../src/gui/kernel/qwidget.h(78) : see declaration of 'QKeyEvent'
1>.\CSequencesDialog.cpp(486) : error C2227: left of '->ignore' must point to class/struct/union/generic type
1>CEditor.cpp
1>Generating Code...

spirit
11th May 2009, 09:47
you forgot to include


#include <QKeyEvent>

ricardo
11th May 2009, 20:29
Thanks a lot, it worked.