Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. adding a countdown timer in a QLabel
QtWS25 Last Chance

adding a countdown timer in a QLabel

Scheduled Pinned Locked Moved Solved Game Development
7 Posts 3 Posters 3.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mayyy
    wrote on last edited by
    #1

    hello all ,
    well , it's my first project using Qt and I want to add a coutdown timer for a game in QLabel but I can't find the problem there (use of undeclared identifier 'connect') . here is my main.cpp and thanks for your help :)

    void myTimerHandler(QLabel *time2){
    qint32 counter=0;
    QTimer *timer = new QTimer();
    counter++;
    time2->setText(QString("Time: %1 seconds").arg(counter));
    if(counter == 3600){
    time2->setText(QString("GAME OVER"));
    timer->stop();
    }
    }

    void accessories(QWidget *baseWidget)
    {
    QLabel *player2 = new QLabel(baseWidget);
    QLabel *name2 = new QLabel("Player 2", baseWidget);
    //QLabel *time2 = new QLabel("00:00:00", baseWidget);
    time2 = new QLabel("00:00:00", baseWidget);

    //qint32 counter = 0;
    //QTimer *timer = new QTimer();
    qint32 counter=0;
    QTimer *timer = new QTimer();
    connect(timer,SIGNAL(&QTimer::timeout()),SLOT(myTimerHandler(time2)));
    timer->start(1000);
    
    time2->setText(QString("Time: %1 seconds").arg(counter));
    counter++;
    
    if(counter == 3600){
        time2->setText(QString("GAME OVER"));
        timer->stop();
    }
    

    }
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    QWidget *myWidget = new QWidget();
    myWidget->setGeometry(0,0,1370,700);
    
    accessories(myWidget);
    

    myWidget->show();
    return a.exec();
    }

    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      It looks like you are doing that connect from a "floating" method hence your issue. The connect method belongs to the QObject class.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M mayyy

        hello all ,
        well , it's my first project using Qt and I want to add a coutdown timer for a game in QLabel but I can't find the problem there (use of undeclared identifier 'connect') . here is my main.cpp and thanks for your help :)

        void myTimerHandler(QLabel *time2){
        qint32 counter=0;
        QTimer *timer = new QTimer();
        counter++;
        time2->setText(QString("Time: %1 seconds").arg(counter));
        if(counter == 3600){
        time2->setText(QString("GAME OVER"));
        timer->stop();
        }
        }

        void accessories(QWidget *baseWidget)
        {
        QLabel *player2 = new QLabel(baseWidget);
        QLabel *name2 = new QLabel("Player 2", baseWidget);
        //QLabel *time2 = new QLabel("00:00:00", baseWidget);
        time2 = new QLabel("00:00:00", baseWidget);

        //qint32 counter = 0;
        //QTimer *timer = new QTimer();
        qint32 counter=0;
        QTimer *timer = new QTimer();
        connect(timer,SIGNAL(&QTimer::timeout()),SLOT(myTimerHandler(time2)));
        timer->start(1000);
        
        time2->setText(QString("Time: %1 seconds").arg(counter));
        counter++;
        
        if(counter == 3600){
            time2->setText(QString("GAME OVER"));
            timer->stop();
        }
        

        }
        int main(int argc, char *argv[])
        {
        QApplication a(argc, argv);

        QWidget *myWidget = new QWidget();
        myWidget->setGeometry(0,0,1370,700);
        
        accessories(myWidget);
        

        myWidget->show();
        return a.exec();
        }

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @mayyy
        You would have to use QObject::connect().

        connect() is available in any QObject-derived class. You are not in one. In fact you are not in any class. You are writing C-style global functions. You will need to get C++/object-oriented to get far in Qt.

        1 Reply Last reply
        1
        • M Offline
          M Offline
          mayyy
          wrote on last edited by
          #4

          thank you @SGaist and @JonB for your quick response , but even when I add QObject::connect() i get the same error .
          @JonB yes it's a global function but it works just this part ( of the countdown timer), should I implement a class for this countdown timer ? and how can i instantiate it then in a QLabel ?

          JonBJ 1 Reply Last reply
          0
          • M mayyy

            thank you @SGaist and @JonB for your quick response , but even when I add QObject::connect() i get the same error .
            @JonB yes it's a global function but it works just this part ( of the countdown timer), should I implement a class for this countdown timer ? and how can i instantiate it then in a QLabel ?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @mayyy
            Instead of just using QWidget *myWidget = new QWidget(), I would suggest creating your own subclass of QWidget (which itself inherits from QObject) and use that. Your functions can then be member functions, and probably the other widgets will be members too. You will be able to call connect(). Then things will begin to come together.

            If you are starting out in Qt, I strongly suggest you move away from SIGNAL/SLOT macros and adopt new style syntax.

            M 1 Reply Last reply
            0
            • JonBJ JonB

              @mayyy
              Instead of just using QWidget *myWidget = new QWidget(), I would suggest creating your own subclass of QWidget (which itself inherits from QObject) and use that. Your functions can then be member functions, and probably the other widgets will be members too. You will be able to call connect(). Then things will begin to come together.

              If you are starting out in Qt, I strongly suggest you move away from SIGNAL/SLOT macros and adopt new style syntax.

              M Offline
              M Offline
              mayyy
              wrote on last edited by mayyy
              #6

              @JonB ok I will try this . Yes I'm beginner in Qt and i will read the link carefully and thank you .

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Since you are a beginner, you should take the time to follow one or more of Qt's widgets tutorials in the documentation before going further. You'll get the ropes and avoid pitfalls doing so.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved