Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]QTimer works incorrect..
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]QTimer works incorrect..

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.3k Views 1 Watching
  • 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.
  • L Offline
    L Offline
    lwx_me
    wrote on last edited by
    #1

    my test code is:
    @#include "playwidget.h"
    #include "ui_playwidget.h"
    #include <QTimer>

    PlayWidget::PlayWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PlayWidget)
    {
    ui->setupUi(this);
    _timer = new QTimer(this);
    connect(_timer, SIGNAL(timeout()), this, SLOT(showInfo()));
    _timer->start(1000);
    }

    PlayWidget::~PlayWidget()
    {
    delete ui;
    }

    void PlayWidget::showInfo()
    {
    static int counts = 0;

    ui->label->setText(QString("count = %1").arg(++counts));
    

    }@

    @void MainWindow::on_pushButton_clicked()
    {
    PlayWidget *pw = new PlayWidget();
    pw->show();
    }
    @

    The question is when there is only one PlayWidget it works fine,but when I create more(gt 1), they work abnormally.
    I found that the counts increment equal to the PlayWidget I create.
    Every PlayWidget and QTimer are new instance.
    Why is this happening?
    Thanks

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on last edited by
      #2

      You made "counts" a static variable! Inside a function the keyword "static" before a variable means that this variable will retain its value between different function calls. It's essentially a global variable that is only visible/accessible inside the function. Most important, when you declare a "static" variable inside a class method, then all instances of that class will share the very same variable!

      I am pretty sure that making "counts" a (non-static!) member variable of the PlayWidget class will fix it ;-)

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lwx_me
        wrote on last edited by
        #3

        @MuldeR MuldeR
        Thanks, yes it's "static" make it wrong but I consider only static memeber variables share the variable,here the "counts " was a local variable.
        I test the code in C and it works fine.

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

          Nope! A variable declared "static" inside a function is not local anymore. As said before, it is essentially a global variable! Only difference to a global variable declared outside a function is that the "static" variable can only be accessed inside the function where it was declared. Otherwise it acts like a global variable. So if you declare a "static" variable inside some method of a class, as you did in your showInfo() method, then all instances of that class will share the very same variable - just like a static member variable (class variable). That is: With your code all instances of PlayWidget share the same "counts" value! That's basic C++ and not related to QTimer at all...

          (Only exception to the above-mentioned is related to Template functions, but that's another issue I think ^^)

          My OpenSource software at: http://muldersoft.com/

          Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

          Go visit the coop: http://youtu.be/Jay...

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lwx_me
            wrote on last edited by
            #5

            thanks,I know it now.

            Regards,
            lwx_me

            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