Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problem with updating a widget



  • 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.



  • 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.



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



  • @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.



  • 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.



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


  • Lifetime Qt Champion

    Hi and welcome to devnet,

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



  • @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.


Log in to reply