I am confused on how to use classes at Qt



  • Lets say for example we have the "MainWindow" class and one created by us lets call it "live"

    In the mainwindow.h we have created a qtimer as public:
    @QTimer *timer;@

    and through one void fuction of the "live" class we want to see if the qtimer is active.
    We can't use
    @if(timer->isActive())@

    because it will simply say
    @error: 'timer' was not declared in this scope@

    What am i supposed to do?



  • Where are you defining the live variable, in MainWindow class ?
    It would help if you show us some code.
    One possible solution would be to create another QTimer pointer in live class and assign 'timer' to it.



  • Can you post full code in here. I think your function is not member of your class.



  • You need to keep in mind that usually you're aiming for loose coupling between classes. So here I think the right point to start is asking: Why does live need to know that a timer that belongs to MainWindow is still active? Maybe it doesn't need to know and just needs to be notified by MainWindow, that the timer has run out (e.g. via a signal/slot connection). Or maybe the timer is misplaced in MainWindow and should actually be inside live.
    Sometimes, it is true that a thing just has to be shared between two separate classes. In this case, both classes should have a pointer to the thing. This possibly is the most common source of crashes. When the programmer isn't fully aware which classes hold pointers to the thing at any time. In those cases it might happen, that class A uses its pointer to delete the thing, while class B still holds (and wants to use) the thing – crash. To prevent that, you need a system to manage ownership and propagate pointer validity. This system is for example given by so called safe pointers (see QSharedPointer, QWeakPointer, QPointer...).


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.