Reputation: 584
I got a program in which I connect with a QSignalMapper multiple signal from object to a slot in the main program:
class A()
{
private:
QSignalMapper * signalMapperRead_;
std::vector<Service*> services_;
public:
void initConnection()
{
signalMapperRead_ = new QSignalMapper();
connect(signalMapperRead_, SIGNAL(mapped(int)), this, SLOT(readyToSendToService(int)));
for ( size_t i = 0 ; i < services_.size() ; ++i )
{
connect(services_.at(i), SIGNAL(readyToSendToServer()), signalMapperRead_, SLOT(map()));
signalMapperRead_->setMapping(services_.at(i), (int)i);
}
}
int run()
{
initConnection();
for(;;)
{
//do stuff;
}
}
};
int main()
{
QApplication app(argc, argv);
A * a = new A();
a->run();
return app.exec
}
then, as the program is a kind of server i make him loop, and waiting for new client, ...
But the slot is never called. I am thinking that maybe it's because the program is always in the loop and never check if a signal has been emitted.
Can you please help me
Upvotes: 0
Views: 980
Reputation: 584
I have found a work-around. Beside I am not sure it's very efficient, my solution is to start a QEventLoop
at every cycle of my for(;;)
loop, and I made this QEventLoop
quit on a QTimer
timeout
Upvotes: -1
Reputation: 76240
But the slot is never called.
The Qt documentation about QApplication::exec
says:
Enters the main event loop and waits until exit() is called [...]. It is necessary to call this function to start event handling. The main event loop receives events from the window system and dispatches these to the application widgets. [...] Generally, no user interaction can take place before calling exec(). [...]
This means that it's the exec
method that takes care of the signal-slot system. You are calling A::run
(before the exec
function) which contains an for(;;)
infinite loop which will block the real loop from being executed leading to your signals to be lost.
Upvotes: 1
Reputation: 904
Don't use your own loop, create a QApplication and call its exec() method.
You have to call QApplication::exec() for Qt to deliver signals.
Edit for changed code: Just remove the for(;;)-Loop, it's uneccessary. QApplication::exec() has its own loop.
Upvotes: 2