-1

I've set a placeholder in a QLabel to "user".

After ui->setupUi(this);, QLabel gets changed via ui->label->setText("superuser");. No changes are made afterwards.

As soon as QMainWindow is initialized and shown via mw.show(); in main.cpp file , QLabel is set to the placeholder "user" again.

I tried:

  • qDebug()<<ui->label->text(); before and after ui->label->setText("superuser"); and placed one right at the end of constructor of QMainWindow. Results:
    user
    superuser
    superuser
    
  • Checked mainwindow.ui in nano, all is right.
  • Checked connections: there are no connections that are linked to the function that changes QLabel.
  • Checked if mw->show(); is triggered multiple times in main.cpp, one call.
  • No external thread changes the QLabel outside from QMainWindow.

Here's a minimal reproducible example:

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

using namespace std;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->init();
    qDebug() << "afterInit" << ui->label->text();
}

void MainWindow::init()
{
    this->set_options("SuperUser");
}

void MainWindow::set_options(QString s)
{
    debugInt++;
    qDebug() << debugInt;
    qDebug() << ui->label->text();
    ui->label->setText(s);
    qDebug() << ui->label->text();
}

void MainWindow::on_actionLogout_triggered()
{
    this->set_options("User");
}

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtWidgets/QMainWindow>
#include <QDebug>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public slots:
    void set_options(QString s);

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

private:
    void init();
    Ui::MainWindow *ui;

    int debugInt=0;

private slots:
    void on_actionLogout_triggered();
};
#endif //MAINWINDOW_H

main.cpp:

#include "mainwindow.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    a.setQuitOnLastWindowClosed(true);

    MainWindow w;

    w.show();

    return a.exec();
}

ui->label is set to "Current: user" in mainwindow.ui. That's the standard set text that I call placeholder.

mainwindow.ui:

<property name="text">
           <string>Current: user</string>

My QDebug output when I start the program and then press logout:

1
"Current: user"
"Current: SuperUser"
"afterInit "Current: SuperUser"
2
"Current: user"
"Current: User"

As you can see, by default ui->label is set to "Current: user". In the init process it gets overwritten to "Current: SuperUser". But when I start the program, it is shown as "Current: user". As soon as I click the actionLogout (menubarbutton in mainwindow_ui), ui->label is set to "Current: User" and gets shown right.

Note: I created a new program with the given code. Works as it should, ui->label isn't changed in any other function in my code.

red power
  • 1
  • 2
  • 2
    Could you provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? – Abderrahmene Rayene Mihoub Jun 08 '23 at 21:06
  • 1
    Are you being confused by a label and e.g. a `QLineEdit` next to each other? What does it mean, to have *set a **placeholder** in a **QLabel** to [...]*? – Atmo Jun 09 '23 at 00:03
  • Does your last edit say that the code you provided does not reproduce the issue? Because it doesn't, I get "Current: SuperUser" at first, and "Current: User" when I click `actionLogout`. Here's the output: (`1\n"Current: user"\n"SuperUser"\nafterInit "SuperUser"\n2\n"SuperUser"\n"User`) – Abderrahmene Rayene Mihoub Jun 12 '23 at 12:08
  • It didn´t reproduce the issue. there´s something wrong in my original project, but i cant figure out what it is. The given code is the only code related to ui->label in my original project – red power Jun 12 '23 at 13:37
  • We cannot help you without a minimal **reproducible** example, this is a debugging issue, read the link I provided, and this one [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems), to see where does your label text gets changed, that would at least help your make a minimal **reproducible** example. But for now, I don't think your question is answerable. Good luck. – Abderrahmene Rayene Mihoub Jun 12 '23 at 13:47

1 Answers1

-1

So I´ve found the error. I have overwritten MainWindow::changeEvent(QEvent* event) method:

mainwindow.cpp

void MainWindow::changeEvent(QEvent* event)
{
  if(0 == event) return;

  switch(event->type())
  {
  case QEvent::LanguageChange: ui->retranslateUi(this); break;

  case QEvent::LocaleChange: break;

  default: break;
  }

  QMainWindow::changeEvent(event);

  }

Fix:

I add ui->label->setText( tr("Current: ") + currentUserString ); before QMainWindow::changeEvent(event) because the QLabel gets changed when MainWindow::changeEvent(QEvent* event) is executed to the set string in .qm/.ts translation file. Thats why mainwindow showed me "Current: user" (from translation file) instead of "Current: SuperUser" (changed in init function)

new mainwindow.cpp

void MainWindow::changeEvent(QEvent* event)
{
  if(0 == event) return;

  switch(event->type())
  {
  case QEvent::LanguageChange: ui->retranslateUi(this); break;

  case QEvent::LocaleChange: break;

  default: break;
  }

  if(uiIsSetUp) //bool that get set true after `ui->setupUi(this);`
    ui->label->setText( tr("Current: ") + currentUserString );

  QMainWindow::changeEvent(event);

  }
red power
  • 1
  • 2