Update QLabel in slots
-
I have a label on status bar which should update a text depending on text received in a custom slot. I can add a text to this label from the constructor.
The code for the slot is:
void StatusBarIndicator::receiveServerStatusLabel(const QString &text) { qInfo() << text; ui->serverStatusLabel->setText(text); }This slot can receive the text but it's not updating the label. Looking at other threads I have tried the following:
- Add
repaint()aftersetText() - Add
clear()beforesetText() - Add
clear()beforesetText()and the addrepaint() - Add
update()aftersetText() - All the combinations above and then add
QCoreApplication::processEvents()
None of the above points worked. Is there any way to update the text?
Update
Just made my code public - https://github.com/akshaybabloo/DataLogger. I am using CMake.
Double click on any of the list item, this should open a window. Click on - start server - button. That button should update the label.
- Add
-
I have checked your repository on GitHub and fixed the issue.
Your code:
ui->statusbar->addPermanentWidget(new StatusBarIndicator, 1);// here you create instance ofStatusBarIndicator, alsonew StatusBarIndicatorcreates a memory leak, do not use it like that anywhere.But
auto *indicator = new StatusBarIndicator;// here your create new instance again
connect(this, &Logger::emitServerStatusLabel, indicator, &StatusBarIndicator::receiveServerStatusLabel);My code:
auto *indicator = new StatusBarIndicator(this); ui->statusbar->addPermanentWidget(indicator, 1); connect(this, &Logger::emitServerStatusLabel, indicator, &StatusBarIndicator::receiveServerStatusLabel);Screenshot:

-
I have a label on status bar which should update a text depending on text received in a custom slot. I can add a text to this label from the constructor.
The code for the slot is:
void StatusBarIndicator::receiveServerStatusLabel(const QString &text) { qInfo() << text; ui->serverStatusLabel->setText(text); }This slot can receive the text but it's not updating the label. Looking at other threads I have tried the following:
- Add
repaint()aftersetText() - Add
clear()beforesetText() - Add
clear()beforesetText()and the addrepaint() - Add
update()aftersetText() - All the combinations above and then add
QCoreApplication::processEvents()
None of the above points worked. Is there any way to update the text?
Update
Just made my code public - https://github.com/akshaybabloo/DataLogger. I am using CMake.
Double click on any of the list item, this should open a window. Click on - start server - button. That button should update the label.
@akshaybabloo said in Update QLabel in slots:
None of the above points worked
None of that should be necessary.
Did you make sure the slot is actually called?
If it is called - what does text contain? - Add
-
@akshaybabloo said in Update QLabel in slots:
None of the above points worked
None of that should be necessary.
Did you make sure the slot is actually called?
If it is called - what does text contain?@jsulm for now the code just emits a string:
void Logger::on_serverButton_toggled(bool checked) { if (checked){ emit emitServerStatusLabel("server running on 127.0.0.1:1000"); } else { emit emitServerStatusLabel(""); } }And yes, the slot does receive the text.
-
@jsulm for now the code just emits a string:
void Logger::on_serverButton_toggled(bool checked) { if (checked){ emit emitServerStatusLabel("server running on 127.0.0.1:1000"); } else { emit emitServerStatusLabel(""); } }And yes, the slot does receive the text.
@akshaybabloo
As @jsulm wrote, all should already be working.
At a guess: inside classStatusBarIndicator, what doesui(andui->serverStatusLabel) refer to? Is it possible it's not the label you think it is? Put some other text into the label first, thenqInfo() << ui->serverStatusLabel->text();before setting it anew in the slot? Is the signal connected to the correct instance? Try with something else? -
@akshaybabloo
As @jsulm wrote, all should already be working.
At a guess: inside classStatusBarIndicator, what doesui(andui->serverStatusLabel) refer to? Is it possible it's not the label you think it is? Put some other text into the label first, thenqInfo() << ui->serverStatusLabel->text();before setting it anew in the slot? Is the signal connected to the correct instance? Try with something else? -
I have checked your repository on GitHub and fixed the issue.
Your code:
ui->statusbar->addPermanentWidget(new StatusBarIndicator, 1);// here you create instance ofStatusBarIndicator, alsonew StatusBarIndicatorcreates a memory leak, do not use it like that anywhere.But
auto *indicator = new StatusBarIndicator;// here your create new instance again
connect(this, &Logger::emitServerStatusLabel, indicator, &StatusBarIndicator::receiveServerStatusLabel);My code:
auto *indicator = new StatusBarIndicator(this); ui->statusbar->addPermanentWidget(indicator, 1); connect(this, &Logger::emitServerStatusLabel, indicator, &StatusBarIndicator::receiveServerStatusLabel);Screenshot:
