Unsolved classes with QtimerEvent
-
Dear sir ,
I am calling a Q timer event function in main windowclass by Start timer(10), but when i am trying to call the same timer from different class then the function is not called.//this is timer function which is called from MainWindow*//
void MainWindow::timerEvent(QTimerEvent *event) //
{
qDebug()<< "i m calling myself";
}//but when i call from different class function is not called//
void GPRS::timerEvent(QTimerEvent *event) //
{
qDebug()<< "i m calling myself";
}
please help me regarding this issue. -
@qt_ankit_developer said in classes with QtimerEvent:
when i am trying to call the same timer from different class then the function is not called
How are you calling timer from different class?
-
- show us your GPRS header file. Also
- show us the method in which you are starting the timer in case GPRS class.
- show us where u r creating the GPRS object as well.
By all chance you are not starting timer in GPRS or not created the GPRS object itself. May it is destroyed as well.
-
This post is deleted! -
@qt_ankit_developer said in classes with QtimerEvent:
gprs_modem2
Your explanation confused us when u r doing what. How you are doing. Sample code will help instead of english.
In general - You have the following issues.
- gprs_modem2 is not inherited from QObject.
- QTimer will not call this function.
- You need to work with Signal/SLots in if you are using QTimer.
- timerEvent(..) is fired if you use the startTimer(...) method.
-
@dheerendra said in classes with QtimerEvent:
Dear sir ,
It is compulsary to inherit gprs_modem2 class from QObject. -
timer functionality is given the Qt through QObject. Simple C++ class does not have this kind of feature. What you written is plain vanilla C++ class. If you want the timer support from Qt, you should inherit from QObject. Instead of all this you can simply work QTimer class. It is easy to use.
-
@qt_ankit_developer
if you don't want to inherit from QObject, that is fine, QTimerEvent is explicit to QObject based class.Using QTimer you can even forgo the QObject inheritance. At least as long as you use the new Qt5 Signal/Slot syntax, with the old SIGNAL() SLOT() macros, you must have QObject as base class.
-
Dear sir,
I am attaching my code screen shot .I am calling function fun() [this function is in gprs class] from MainWindow & this fun() function start timer calling
but, it does not work. -
@qt_ankit_developer
that is because of the lifetime of the gpra object.You create it not on the heap inside the slot and as soon as the function ends, it get's deleted again. -> no time for the timer to actually do anything.
- either create it on the heap
- or make the object a class member
-
@J.Hilk
i am little bit confused that my timer is working in my main window class but when i call that timer from diffrent class it not works what i have to do to start a simple timer in different class .//this code is working in my main window class
connect(timer, SIGNAL(timeout()), this, SLOT(timerDone()));
timer->start(1000);
void MainWindow::timerDone()
{
qDebug() <<"timerdone";
}//this code is not working when same code used in difrent class
connect(timer, SIGNAL(timeout()), this, SLOT(timerDone()));
timer->start(1000);
void GPRS::timerDone()
{
qDebug() <<"timerdone";
}
please help regarding this problem -
- show your GPRS class
- Show where you are creating the GPRS object
- show where you are creating the timer object
- Show where you are doing the connect statement
-
in gprs.h:
#include <QTimer> class gprs: public QMainWindow { Q_OBJECT public: void startTimer(int interval) { mytimer.start(interval); } // - for calling from different class // .... private: QTimer mytimer; private slots: void on_timerTimeout(); // ... }
in gprs.cpp:
gprs::gprs(Qwidget *parent): QMainWindow(parent) { // ... connect(&mytimer, &QTimer::timeout, this, &gprs::on_timerTimeout); mytimer.start(500); } void gprs::on_timerTimeout() { // is timer event timeout }
-
I have tried this method but timer is not calling itself
-
@qt_ankit_developer You again put gprs as local variable in MainWindow constructor!
You are aware that it will be destroyed as soon as the constructor finishes? -
I have tried same with Qobject also by replacing Qmainwindow with Qobject but that doesnot work
-
Do the following.
- Inside the header file of MainWindow.h declare variable gprs *gprsHeapObject;
- Insid the mainwindow constructor
gprsHeapObject = new gprs - Now call gprsHeapObject->fun().
It should work. As @jsulm said, you are creating the local object inside the constructor. Local object will be destroyed once functions is over.
-
@qt_ankit_developer It's not about QMainWindow or QObject (QMainWindow is a QObject by the way).
You should read about scopes in C++, else you will do the same mistake again and again...