PDA

View Full Version : event problem



windkracht8
13th August 2006, 13:54
Hello all,

strange thing I'm seeing in this code.
Somehow I have to catch the "QEvent::Close" event and call "qApp->quit()" . Otherwise the program won't close!

Anyone any idea how this is possible? It does call the event close, it want's to close, I ignore the event but nothing happens.

Cheers,
Bart.




bool frmMain::event(QEvent* event)
{
if(event->type() == QEvent::User){
bC* eTemp = (bC*)event;
iTemp = eTemp->rij();
iTemp2 = eTemp->col();
knopGeklikt(iTemp,iTemp2);
event->accept();
return true;
}
//this part is needed somehow
if(event->type() == QEvent::Close){
qApp->quit();
event->accept();
return true;
}
//up till here

event->ignore();
return false;
}

jpn
13th August 2006, 14:13
It is not recommended to reimplement the main event handler. Use specialized event handlers instead. Eg. closeEvent() for QEvent::Close type and customEvent() for type values starting from QEvent::User and so on.

Your form might not work very well because you are ignoring a bunch of events. QWidget base class implementation (which you have overridden with your own implementation) does a lot of handling based on the event type and later calls the specialized event handler (which is the one supposed to be overridden..)

So start with moving the block of code handling QEvent::User types to customEvent() and forget about overriding event()..

jacek
13th August 2006, 14:13
Anyone any idea how this is possible?
You don't invoke the base class implementation.

Try:
bool frmMain::event(QEvent* event)
{
if(event->type() == QEvent::User){
bC* eTemp = (bC*)event;
iTemp = eTemp->rij();
iTemp2 = eTemp->col();
knopGeklikt(iTemp,iTemp2);
event->accept();
return true;
}
return QWidget::event( event );
}

or better:
bool frmMain::customEvent(QEvent* event)
{
if(event->type() == QEvent::User){
bC* eTemp = (bC*)event;
iTemp = eTemp->rij();
iTemp2 = eTemp->col();
knopGeklikt(iTemp,iTemp2);
event->accept();
return true;
}
return QWidget::customEvent( event );
}

windkracht8
17th August 2006, 07:38
Qt is getting a little more clear now. Thanks for the respons.
One more question though:

This piece of code:


bool frmMain::customEvent(QEvent* event)
{
if(event->type() == QEvent::User){
bC* eTemp = (bC*)event;
iTemp = eTemp->rij();
iTemp2 = eTemp->col();
knopGeklikt(iTemp,iTemp2);
event->accept();
return true;
}
return QWidget::customEvent( event );
}

Gave the error that "virtual bool QObject::customEvent(QEvent* event)" was overwriting
"virtual void QObject::customEvent(QEvent* event)".
So I'm going with this at the moment:


void frmMain::customEvent(QEvent* event)
{
bC* eTemp = (bC*)event;
iTemp = eTemp->rij();
iTemp2 = eTemp->col();
knopGeklikt(iTemp,iTemp2);
event->accept();
}

Should be ok right?
I removed the "if(event->type() == QEvent::User)" statement becauss it looks obsolete, since customEvent doesn't get anything else then the "QEvent::User" and I only use one customevent.

Thanks all,
Bart.

jpn
17th August 2006, 07:54
Should be ok right?
I removed the "if(event->type() == QEvent::User)" statement becauss it looks obsolete, since customEvent doesn't get anything else then the "QEvent::User" and I only use one customevent.
Yep, then looks fine.

jacek
17th August 2006, 11:52
Gave the error that "virtual bool QObject::customEvent(QEvent* event)" was overwriting
"virtual void QObject::customEvent(QEvent* event)".
I don't know where I saw that bool :confused: