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. QLabel setText() only updates when adjusting window size
Qt 6.11 is out! See what's new in the release blog

QLabel setText() only updates when adjusting window size

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 631 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.
  • S Offline
    S Offline
    shawnlong
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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.

      (Z(:^

      1 Reply Last reply
      5
      • S Offline
        S Offline
        shawnlong
        wrote on last edited by
        #3

        @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!

        1 Reply Last reply
        1

        • Login

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