Help with QTimer scope
-
Hi
I'm working with QT creator in a project and it seems like i'm stuck trying to get a QTimer to work.
I have a MainWindow object in which i can create and use QTimers without any problems. Within the MainWindow i create an object, wc, of a class called weightChanger. Within wc i want a timer that connects to several slots within wc.
I've tried a lot of different ways, but i can't seem to get the timers to connect. In my last try i tried declaring the timers in my header file for the weightChanger class:
Afterwards i initialize them in the constructor of wc and connects them aswell. I made a timerTest() slot which just sends a message to a console when executed, but i can't seem to get it working in any ways. I've tried several other ways of implementing the QTimer within the weightChanger class, but without any luck yet.
Anyone got a hint to what i got wrong?
-
You should connect the signals before you start the timer. You set it for only 10 ms, so there is a chance that the timer will fire before your connection is made.
-
Also, make sure the connection succeeds (it returns a bool). And remember to declare timerTest() as a slot in your header file.
Also, I heavily recommend using the new connection syntax, it's much better.
-
Also, make sure the connection succeeds (it returns a bool). And remember to declare timerTest() as a slot in your header file.
Also, I heavily recommend using the new connection syntax, it's much better.
Hi @sierdzio,
I was just about to write the same ;)
@Christianvs please have a look here: https://wiki.qt.io/New_Signal_Slot_Syntax
-
Hi
I'm working with QT creator in a project and it seems like i'm stuck trying to get a QTimer to work.
I have a MainWindow object in which i can create and use QTimers without any problems. Within the MainWindow i create an object, wc, of a class called weightChanger. Within wc i want a timer that connects to several slots within wc.
I've tried a lot of different ways, but i can't seem to get the timers to connect. In my last try i tried declaring the timers in my header file for the weightChanger class:
Afterwards i initialize them in the constructor of wc and connects them aswell. I made a timerTest() slot which just sends a message to a console when executed, but i can't seem to get it working in any ways. I've tried several other ways of implementing the QTimer within the weightChanger class, but without any luck yet.
Anyone got a hint to what i got wrong?
@Christianvs To add to @sierdzio : why do you allocate both timers on the heap? Why not just:
private: QTimer weightChangeTimer; QTimer changeWeightTimer;
-
Thanks for the answers! I've tried following (i'm sorry about the messy code, in my attempt to get this working lines have been added, changed and commented out):
I am not sure if i get the new connect method right. In the case it should be the right way i connect it, i still seem to have the same problem as before.
@jsulm
I actually started out with initializing the Qtimers in the header file, but in my search for my mistake i tried allocating on the heap -
Thanks for the answers! I've tried following (i'm sorry about the messy code, in my attempt to get this working lines have been added, changed and commented out):
I am not sure if i get the new connect method right. In the case it should be the right way i connect it, i still seem to have the same problem as before.
@jsulm
I actually started out with initializing the Qtimers in the header file, but in my search for my mistake i tried allocating on the heap@Christianvs What is ROS_INFO_STREAM?
Can you try qDebug() instead to verify the slot is called? -
Thanks for the answers! I've tried following (i'm sorry about the messy code, in my attempt to get this working lines have been added, changed and commented out):
I am not sure if i get the new connect method right. In the case it should be the right way i connect it, i still seem to have the same problem as before.
@jsulm
I actually started out with initializing the Qtimers in the header file, but in my search for my mistake i tried allocating on the heap@Christianvs Are you sure your debug output works? Can you set a breakpoint in the slot?
Beside the "mess" your code looks Ok.
If it still not works, you have to look if you somewhere block signals or if the event loop is not running etc.
-
@Christianvs What is ROS_INFO_STREAM?
Can you try qDebug() instead to verify the slot is called?The whole project is connected through ROS as it controls an AC motor. To be honest i'm not sure if qDebug works in my current setup.
None the less i got the timer to say it's active by running isActive() after i start the timer (what a stupid mistake!)
But that actually solves part of my problem as i now know the timer is actually active!
But it still does not execute the timerTest() function!
-
The whole project is connected through ROS as it controls an AC motor. To be honest i'm not sure if qDebug works in my current setup.
None the less i got the timer to say it's active by running isActive() after i start the timer (what a stupid mistake!)
But that actually solves part of my problem as i now know the timer is actually active!
But it still does not execute the timerTest() function!
@Christianvs said in Help with QTimer scope:
qDebug works in my current setup
std::cout should work.
And you can place a breakpoint inside the slot and start through debugger. -
Could the problem be that my weightChanger object somehow needs to be initialized in another way? By using new ?
Above is where i actually initialize the object and it is within this object the QTimer is stored
-
Could the problem be that my weightChanger object somehow needs to be initialized in another way? By using new ?
Above is where i actually initialize the object and it is within this object the QTimer is stored
@Christianvs said in Help with QTimer scope:
Could the problem be that my weightChanger object somehow needs to be initialized in another way? By using new ?
Yes! When this function ends, it will be destroyed (object goes out of scope).
-
@Christianvs said in Help with QTimer scope:
Could the problem be that my weightChanger object somehow needs to be initialized in another way? By using new ?
Yes! When this function ends, it will be destroyed (object goes out of scope).
@sierdzio @Christianvs You can see this type of error very often here :-)
-
Yay! Now that i create it on the heap using new the timers work without any problems!
Thanks for the help all of you! :)
-
Just make sure it is cleaned up properly afterwards (either assign
this
as parent or calldelete
on it when you don't need it anymore). -
@Christianvs if your issue is solved, please don't forget to mark your post as such. Thanks.
-
Just make sure it is cleaned up properly afterwards (either assign
this
as parent or calldelete
on it when you don't need it anymore).@sierdzio
Is there any downside to just calling this as parent?@Pablo-J-Rogina
I actually cannot find where to mark it as solved
EDIT: Done! :) -
@sierdzio
Is there any downside to just calling this as parent?@Pablo-J-Rogina
I actually cannot find where to mark it as solved
EDIT: Done! :)@Christianvs said in Help with QTimer scope:
Is there any downside to just calling this as parent?
No. The other solution is worse, because it is easy to forget about calling
delete
. If you use Qt's parent-child system, then Qt will automatically delete objects when their parent is deleted.There is a possibility that it is not the behaviour you want, but that is not something we can determine from the code above.
Other options for managing object life:
- QSharedPointer
- stl pointers