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 with updating a widget
QtWS25 Last Chance

Problem with updating a widget

Scheduled Pinned Locked Moved Solved General and Desktop
update
7 Posts 3 Posters 2.7k 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.
  • A Offline
    A Offline
    adrianngai1130
    wrote on 8 Mar 2017, 18:18 last edited by
    #1

    Hello everyone

    I have a QLabel that is connected to a QTimer, which emits a signal every second. The signal causes a function to run, updating the text within the QLabel. However, the QLabel does not update.

    I define the QTimer and connect it here:

        QTimer* timer             =   new QTimer(this);
        timer                    ->   setSingleShot(false);
        timer                    ->   setInterval(1000/fps);
        timer                    ->   start();
        connect(timer, SIGNAL(timeout()), this, SLOT(updateScreen()));
    

    The QLabel is defined here in the constructor of my class:

        time_t localTimer              =   time(NULL);
        struct tm *timeStruct    =   localtime(&localTimer);
    
        char currentTime[100];
        strftime(currentTime,100,"%I : %M : %S",timeStruct);
    
        QLabel* localTimeBar     =   new QLabel(this);
        localTimeBar            ->   setText(currentTime);
        localTimeBar            ->   setFixedSize(titleUnit,infoBarHeight);
        localTimeBar            ->   move(titleUnit * 2,0);
        localTimeBar            ->   setStyleSheet("border-right-width : 0px");
    

    This updates the QLabel:

    void interface::updateScreen(){
        time_t localTimer              =   time(NULL);
        struct tm *timeStruct    =   localtime(&localTimer);
    
        char currentTime[100];
        strftime(currentTime,100,"%I : %M : %S",timeStruct);
    
        localTimeBar -> setText(currentTime);
        localTimeBar -> repaint(); //I've tried update() as well but to no avail
    
        qApp -> processEvents();
    }
    

    No errors are produced when the program is run. However, the QLabel does not update. What am I doing wrong? Thanks in advance.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      adrianngai1130
      wrote on 8 Mar 2017, 18:52 last edited by
      #4

      To people who may stumble upon this thread looking for an answer:

      The problem was defining the QLabel in the constructor. Defining it as public in the class and only referring to it during the update seemed to fix the problem.

      1 Reply Last reply
      0
      • O Offline
        O Offline
        Olivier Ronat
        wrote on 8 Mar 2017, 18:36 last edited by
        #2

        Hello
        I think you must use localTimeBar->setText(currentTime().toString());
        or use a QTime variable of your own and the toString() method

        1 Reply Last reply
        0
        • A Offline
          A Offline
          adrianngai1130
          wrote on 8 Mar 2017, 18:44 last edited by
          #3

          @Olivier-Ronat I don't believe that it's to do with the type of the input given into setText(). I've tried simply replacing currentTime with "test string" and still the QLabel did not update.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            adrianngai1130
            wrote on 8 Mar 2017, 18:52 last edited by
            #4

            To people who may stumble upon this thread looking for an answer:

            The problem was defining the QLabel in the constructor. Defining it as public in the class and only referring to it during the update seemed to fix the problem.

            1 Reply Last reply
            0
            • O Offline
              O Offline
              Olivier Ronat
              wrote on 8 Mar 2017, 18:53 last edited by
              #5

              Try to add QString(currentTime) to have a QString data. I've got issues with the automatic casting of *char and QString

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 8 Mar 2017, 22:05 last edited by
                #6

                Hi and welcome to devnet,

                Maybe a silly question but what's the value of fps ?

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

                A 1 Reply Last reply 9 Mar 2017, 09:23
                0
                • S SGaist
                  8 Mar 2017, 22:05

                  Hi and welcome to devnet,

                  Maybe a silly question but what's the value of fps ?

                  A Offline
                  A Offline
                  adrianngai1130
                  wrote on 9 Mar 2017, 09:23 last edited by
                  #7

                  @SGaist said in Problem with updating a widget:

                  oh fps was just to divide 1000 milliseconds by the ideal amount of times I wanted the program to refresh per second (fps), giving the interval at which the program should have been updated.

                  1 Reply Last reply
                  0

                  2/7

                  8 Mar 2017, 18:36

                  topic:navigator.unread, 5
                  • Login

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