03-05-2013 01:19 AM
I want to create Timeout slot:
file TestYourReflex.hpp
#ifndef TestYourReflex_HPP_
#define TestYourReflex_HPP_
#include <QObject>
#include <QTimer>
#include <QTime>
namespace bb { namespace cascades { class Application; }}
/*!
* @brief Application pane object
*
*Use this object to create and init app UI, to create context objects, to register the new meta types etc.
*/
class TestYourReflex : public QObject
{
Q_OBJECT
public:
TestYourReflex(bb::cascades::Application *app);
virtual ~TestYourReflex() {};
public slots:
void Timeout(int a);
};
#endif /* TestYourReflex_HPP_ */
file TestYourReflex.cpp
// Default empty project template #include "TestYourReflex.hpp" #include <bb/cascades/Application> #include <bb/cascades/QmlDocument> #include <bb/cascades/AbstractPane> #include <bb/cascades/Container> #include <bb/cascades/Color> using namespace bb::cascades; TestYourReflex::TestYourReflex(bb::cascades::Application *app) : QObject(app) { // create scene document from main.qml asset // set parent to created document to ensure it exists for the whole application lifetime QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(th is); // create root object for the UI AbstractPane *root = qml->createRootObject<AbstractPane>(); QTimer *timer = new QTimer(this); timer->setInterval(1000); timer->start(); connect(timer, SIGNAL(timeout()), this, SLOT(Timeout(seed))); // set created root object as a scene app->setScene(root); } void TestYourReflex::Timeout(QTimer *timer){ qDebug() << "timeout"; timer->stop(); }
when the Timeout() function doesn't have param QTimer * timer this code work very well. but when I pass QTimer *timer param like above. the code doesn't work and a got this ERROR
NOTICE Object::connect: No such slot TestYourReflex::Timeout(seed) in ../src/TestYourReflex.cpp:23
any one can help me
Solved! Go to Solution.
03-05-2013 03:19 AM - edited 03-05-2013 03:22 AM
You can't pass additional parameters to slots. Their signatures must be the same as signal signatures.
It's allowed to omit signal parameters in slots, but changing any of the types or passing additional data is not allowed.
In this case a possible solution is declaring QTimer * in the header file as a member variable.
Declare other variables which you need to access in multiple functions the same way.
Don't forget to remove 'int a' in slot params or it won't work:
class QTimer;
class TestYourReflex : public QObject
{
Q_OBJECT
public:
TestYourReflex(bb::cascades::Application *app);
virtual ~TestYourReflex() {};
public slots:
void onTimeout();
protected:
QTimer *timer_;
};
In .cpp:
timer_ = new QTimer(this); timer_->setInterval(1000); timer_->start();
QObject::connect(timer_, SIGNAL(timeout()), this, SLOT(onTimeout()));
...
void TestYourReflex::onTimeout(){
qDebug() << "timeout";
timer_->stop();
}
P.s. There's also QTimer::singleShot(1000, this, SLOT(onTimeout())) which fires the timer only once and doesn't require declaring a variable.
P.s.s. A Qt convention is to start variable and method names with lowercase letters. Uppercase is for types, so they can be easily distinguished from variables.
03-05-2013 05:21 AM
03-05-2013 06:21 AM