PDA

View Full Version : Connecting signal from main.cpp to application.cpp



Jon Heron
6th March 2014, 04:35
Excuse my c++ ignorance.
I am trying to attach the QCoreApplication::aboutToQuit() signal to my save function so I can save some data after the user closes the app on BB10 OS. My QCloseEvent doesn't get called on BB10 when the user swipes up and closes the app so I am trying this approach as suggested in the docs for BB10.
According to the docs for BB10 I have 3 seconds after the signal aboutToQuit() is emited which should be plenty of time for QSettings to save my data.
I put this line in my main.cpp

QObject::connect(&a, SIGNAL(aboutToQuit()), &w, SLOT(save()));
Here is my entire main.cpp for referance:

#include <QtGui/QApplication>
//#include <QtGui/QGuiApplication>
#include "biologger.h"
//#include <bb/Application>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

a.setOrganizationName("HeronBoy");
a.setOrganizationDomain("theHerons.ca");
a.setApplicationName("Bio Logger");

bioLogger w;
w.showMaximized();
QObject::connect(&a, SIGNAL(aboutToQuit()), &w, SLOT(save()));
return a.exec();
}

But my save function is never called?
The way I understand it the signal should be accessible from any public slots in the application and I have my save function declared as a pubic slot
public slots:
void save();.

Any help or insight on how to troubleshoot this would be appreciated.
Cheers,
Jon

anda_skoa
6th March 2014, 10:00
Do you get any runtime error at startup, i.e. does the connect work (it will print a warning if it does not).
It looks good but e.g. the class bioLogger could miss the Q_OBJECT macro, etc.

Another option you could try if you don't need the event loop



w.showMaximize()

int ret = app.exec();

w.save();

return ret;


Cheers,
_

Jon Heron
6th March 2014, 13:40
Thanks!
No, there is no error messages, not sure what the problem is... Is there any way to qDebug() << the signal from the app.cpp to see if its getting emitted?
Either way your event loop works great and its now saving properly!
Thanks again!
Cheers,
Jon

anda_skoa
6th March 2014, 15:39
No, there is no error messages, not sure what the problem is... Is there any way to qDebug() << the signal from the app.cpp to see if its getting emitted?

Not without changing Qt I think.



Either way your event loop works great and its now saving properly!


Ah, great :)

Cheers,
_

Lesiok
6th March 2014, 18:26
Read about QSignalSpy class.

Radek
7th March 2014, 06:59
The usual way of checking whether a slot is called is putting a break (or qDebug() or something similar) in the slot handler and seeing whether you reach the breakpoint (or whether you get a message or something similar). If you cannot modify bioLogger then create a class, say DebugClass, give the class a slot, say DebugSlot() and connect the signal to the DebugSlot(). Put qDebug() in the DebugSlot() and let DebugSlot() call w.save(). Run and see what will happen.