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. Problem related to timer?
Qt 6.11 is out! See what's new in the release blog

Problem related to timer?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.2k 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.
  • P Offline
    P Offline
    pratik041
    wrote on last edited by
    #1

    In the code given below the mousepress slot is not called at all. what may be the problem behind it?
    @
    #ifndef BUTTON_H
    #define BUTTON_H

    #include <QtGui>

    class button:public QPushButton
    {
    Q_OBJECT

    public:
    button(QWidget *parent = 0);
    void animate_button();

    public slots:
    void mousepress();

    };

    #endif // BUTTON_H

    #include <QtGui>
    #include "ofi_vc_gui_button.h"

    button::button(QWidget *parent): QPushButton(parent)
    {
    qDebug ()<<"button constructor";
    QPalette pal;
    pal.setColor(QPalette::ButtonText,QColor(0,255,255));
    setPalette (pal);
    setText ("hello");
    }

    void button::animate_button (bool t)
    {
    QTimer timer;
    connect (&timer, SIGNAL(timeout()), this, SLOT(mousepress()));
    timer.start (1000);
    qDebug ()<<"timer called";
    }

    void button::mousepress ()
    {
    qDebug ()<<"mouse pressed";
    }

    #include <QtGui/QApplication>
    #include "ofi_vc_gui_button.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QMainWindow window;
    button *b1 = new button(&window);
    b1->animate_button ();

    #if defined(Q_WS_S60)
    window.showMaximized();
    #else
    window.show();
    #endif

    return a.exec&#40;&#41;;
    

    }

    @

    Pratik Agrawal

    1 Reply Last reply
    0
    • U Offline
      U Offline
      uranusjr
      wrote on last edited by
      #2

      In your code, timer is a local variable in button::animate_button (bool t), and will be destroyed after the function ended.

      You should declare it on the heap instead:

      @
      void button::animate_button (bool t)
      {
      QTimer *timer = new QTimer(this);
      connect(timer, SIGNAL(timeout()),
      this, SLOT(mousepress()));
      timer->start (1000);
      qDebug ()<<"timer called";
      }
      @

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pratik041
        wrote on last edited by
        #3

        thank you for your useful post i missed that

        Pratik Agrawal

        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