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

QLabel setText() only updates when adjusting window size



  • Hello all,

    I am still new to Qt, but as I am starting to get the hang of things, I ran into a weird issue. I tried to write a simple program with two buttons and a counter, where the label adds 1 or subtracts 1 when you click the corresponding button. The thing is, after clicking the button, the label only changes if I adjust the size of the window while the app is open. I tried using label->update() but it did not work. Any help or a better way to make this simple app would be much appreciated.

    Running on Mac OS X 10.15.4
    Qt Creator 4.12.0, Based on Qt 5.14.2 (Clang 10.0 (Apple))

    Source Code (mainWindow.cpp)
    #include "mainwindow.h"
    #include <QVBoxLayout>
    #include <QPushButton>
    #include <QHBoxLayout>
    #include <QTextEdit>
    #include <QSplitter>
    #include <QTreeView>
    #include <QListView>
    #include <QFrame>
    #include <QLabel>
    #include <QString>
    #include <QSignalMapper>

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    {
    QString labeltext = "Counter: "+QString::number(MainWindow::counter);
    QVBoxLayout *rightVertBox = new QVBoxLayout();
    QHBoxLayout *mainbox = new QHBoxLayout();
    addButton = new QPushButton("Add 1", this);
    subtractButton = new QPushButton("Subtract 1", this);
    label = new QLabel(labeltext,this);
    QWidget *centralWidget = new QWidget();
    QWidget *rightWrapper = new QWidget();

    mainbox->addWidget(label);
        rightVertBox->addWidget(addButton);
        rightVertBox->addWidget(subtractButton);
            rightWrapper->setLayout(rightVertBox);
    mainbox->addWidget(rightWrapper);
    
    connect( addButton, SIGNAL ( clicked() ), this, SLOT ( buttonAdd() ) );
    connect( subtractButton, SIGNAL ( clicked() ), this, SLOT ( buttonSubtract() ) );
    
    centralWidget->setLayout(mainbox);
    setCentralWidget(centralWidget);
    

    }

    MainWindow::~MainWindow()
    {
    }

    void MainWindow::buttonAdd()
    {
    MainWindow::counter++;
    QString labeltext = "Counter: "+QString::number(MainWindow::counter);
    label->setText(labeltext);
    label->update();
    MainWindow::update();
    }

    void MainWindow::buttonSubtract()
    {
    MainWindow::counter--;
    QString labeltext = "Counter: "+QString::number(MainWindow::counter);
    label->setText(labeltext);
    label->update();
    }

    Source Code (mainwindow.h)
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QPushButton>
    #include <QLabel>

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    private slots:
    void buttonAdd();
    void buttonSubtract();
    private:
    int counter = 0;
    QLabel *label;
    QPushButton *addButton;
    QPushButton *subtractButton;
    };
    #endif // MAINWINDOW_H


  • Moderators

    Your code is OK. I've run it on my side and it works great. This must be some platform or GPU issue. Try on a different operating system, or with different Qt version. Make sure you have all the updates installed.

    Some other comments:

    connect( addButton, SIGNAL ( clicked() ), this, SLOT ( buttonAdd() ) );
    connect( subtractButton, SIGNAL ( clicked() ), this, SLOT ( buttonSubtract() ) );
    

    I strongly recommend using the new connection syntax, like this:

    connect( addButton, &QPushButton::clicked, this, &MainWindow::buttonAdd);
    

    It is checked at compile time and generally more powerful than the old syntax.

    QLabel *label;
    QPushButton *addButton;
    QPushButton *subtractButton;
    

    Here I recommend initializing the pointers in headers files, like:

    QLabel *label = nullptr;
    QPushButton *addButton = nullptr;
    QPushButton *subtractButton = nullptr;
    

    This way you won't get garbage values there even if you forget to initialize in constructor. Or you can use QPointer.


  • Moderators

    Your code is OK. I've run it on my side and it works great. This must be some platform or GPU issue. Try on a different operating system, or with different Qt version. Make sure you have all the updates installed.

    Some other comments:

    connect( addButton, SIGNAL ( clicked() ), this, SLOT ( buttonAdd() ) );
    connect( subtractButton, SIGNAL ( clicked() ), this, SLOT ( buttonSubtract() ) );
    

    I strongly recommend using the new connection syntax, like this:

    connect( addButton, &QPushButton::clicked, this, &MainWindow::buttonAdd);
    

    It is checked at compile time and generally more powerful than the old syntax.

    QLabel *label;
    QPushButton *addButton;
    QPushButton *subtractButton;
    

    Here I recommend initializing the pointers in headers files, like:

    QLabel *label = nullptr;
    QPushButton *addButton = nullptr;
    QPushButton *subtractButton = nullptr;
    

    This way you won't get garbage values there even if you forget to initialize in constructor. Or you can use QPointer.



  • @sierdzio Thanks for the help! After an insane amount of forum reading late last night, I saw that someone used QWidget::repaint() to fix something similar and that worked for me. But thank you very much for the tips on the new syntax!


Log in to reply