You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Monday, November 26th 2012, 10:14pm

Callback function everytime QApplication::exec() loops once.

Hi guys,
I am trying to build a game in Qt 4.8.1 and I was wondering if there was a way I could have QApplication::exec() call a function everytime it loops once. That way the game can do whatever it needs to do during that loop (like render or play sound). Do I need to implement my own class that inherits from QApplication (QCoreApplication)? Or can I simply just have SLOT that is called?

2

Tuesday, November 27th 2012, 9:26am

an event loop need some event to treat,
possibly this could satisfy your needs:

Quoted from "QApplication documentation"

int QApplication::exec () [static]
...
To make your application perform idle processing, i.e., executing a special function whenever there are no pending events, use a QTimer with 0 timeout.
More advanced idle processing schemes can be achieved using processEvents().
...

3

Tuesday, November 27th 2012, 6:53pm

Ok, thank you! I guess I need to learn how to read the documentation better (lol). I have one last question and it is: How do I connect a signal from a QObject to a regular C/C++ function? For instance: QTimer::timeout() -> MyFunc()

Can this be done? Or does MyFunc() need to be a slot?

EDIT: I figured it out although now I have another problem. Creating a QTimer with a timeout of 0 doesn't do anything :/
My function is never called. It just runs QApplication.exec() and sits there.

Here is my code that I'm trying to get to run:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <QApplication>
#include <QTimer>
#include <QMetaMethod>
#include "Engine/fusion.h"
using namespace NEngine;

CPhysics g_Physics;
CGraphicsManager g_GfxMan;
CAudioManager g_SndMan;
CMemoryPoolManager g_MemPoolMan;
CResourceManager g_ResMan;
CScript g_Script;
CConsole g_Console;
CWindow *pWnd;

void StartEngine();
void StopEngine();

void RunTick();

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

	QTimer *pTimer = new QTimer();
	app.connect(pTimer, SIGNAL(timeout()), SLOT(RunTick()));

	pWnd = new CWindow();
	pWnd->show();

	StartEngine();

	pTimer->start(1);
	int iRtnVal = app.exec();

	StopEngine();

	return iRtnVal;
}

void StartEngine()
{
	g_Physics.Start();
	g_GfxMan.Start();
	g_SndMan.StartManager();

	if(g_ResMan.SetPath("Data/asteroid.rock") == EError_None)
	{
    	if(g_ResMan.LoadResources() != EError_None)
    	{
        	StopEngine();
        	return;
    	}
	}

	g_Console.StartConsole();
}

void StopEngine()
{
	g_Console.StopConsole();
	g_ResMan.CloseResources();
	g_SndMan.CloseManager();
	g_GfxMan.Close();
	g_Physics.Close();
}

void RunTick()
{
	// Get any keyboard input.
	char cKey = pWnd->ReturnLastKey();
}

This post has been edited 1 times, last edit by "snow_56767" (Nov 27th 2012, 7:49pm)


4

Thursday, November 29th 2012, 11:35am

app.connect(pTimer, SIGNAL(timeout()), SLOT(RunTick()));


Is RunTick a SLOT? Nop. Its a normal function.

To Call slots from signals you have to inherit Object in the object that contains that function and declare it SLOT:

class MyObject : public QObject {

Q_OBJECT

public:
MyObject(QObject *parent = 0) : QObject(parent){}

public slots:
void RunTick();
}


In MyObject body (constructor or something else to initialize things):
QTimer *pTimer = new QTimer(this);
connect(pTimer, SIGNAL(timeout()), this, SLOT(RunTick()));