PDA

View Full Version : Problem about QTimer



pawer
21st January 2008, 17:45
I have several classes in my program, one of them is the main GUI and other is an XML-RPC (from XML-RPC for C/C++) class that have several methods. All thinks goes ok, but when I start the QTimer in the GUI class from the XMLRPC clas it says "QTimer can't be stop/started from other thread". If I put signals and slots does nothing. WTF?



cambiaTimer::cambiaTimer ( rssani_lite *rss, QObject *parent ) : QObject ( parent ),rpc_ani ( rss ) {
connect ( this,SIGNAL ( cambiarTimer ( int ) ), rss, SLOT ( cambiaTimer ( int ) ) );
}

void cambiaTimer::execute ( xmlrpc_c::paramList const& paramList,xmlrpc_c::value * const retvalP ) {

emit cambiarTimer ( 1 );
std::cerr << "RPC llamada" << std::endl;
*retvalP = xmlrpc_c::value_int ( 0 ) ;
}




void rssani_lite::cambiaTimer(int tiempo) {
qDebug() << "Timer cambiado a : " << tiempo << endl;
this->tiempo = tiempo;
timer.start ( tiempo * 60 * 1000 );
}




class rssani_lite : public QObject {
Q_OBJECT

public:
rssani_lite ( QObject* parent = 0);
~rssani_lite();

public slots:
void cambiaTimer(int tiempo);
....
}


Thanks in advance.

jpn
21st January 2008, 17:49
Are you using QThread somewhere in your app?

pawer
21st January 2008, 17:56
No, I create 2 classes in main function:



int main ( int argc, char **argv ) {
QCoreApplication app(argc, argv);
.....
rssani_lite *rss = new rssani_lite();
new rssxmlrpc(rss);
}




rssxmlrpc:: rssxmlrpc ( rssani_lite *rss ) {
....
xmlrpc_c::methodPtr const cambiaTimerP ( new cambiaTimer ( rss ) );
....
myRegistry.addMethod ( "rssani.cambiaTimer", cambiaTimerP );

xmlrpc_c::serverAbyss myAbyssServer (
myRegistry,
8080, // TCP port on which to listen
"/tmp/xmlrpc_log" // Log file
);

myAbyssServer.run();
assert ( false );
}

wysota
21st January 2008, 22:56
Maybe one of these two classes:

rssani_lite *rss = new rssani_lite();
new rssxmlrpc(rss);
starts a new thread? The effect you observe suggests rssani_lite is the one. The timer object is a member of the rssani_lite class but it does not live in the thread spawned by that class.

pawer
22nd January 2008, 11:41
$ ./rssani_lite
RPC llamada
Timer cambiado a : 1

QObject::killTimer: timers cannot be stopped from another thread
QObject::startTimer: timers cannot be started from another thread
QObject::killTimer: timers cannot be stopped from another thread
QObject::startTimer: timers cannot be started from another thread

This is the output if I call the method directly. I don't know the signal behaviour, because the slot is not called.

Thanks for your answers.

wysota
22nd January 2008, 13:02
Signals and slots have nothing to do with it. You just have to start the timer from within the proper thread.