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. Update QLabel in slots
Forum Updated to NodeBB v4.3 + New Features

Update QLabel in slots

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 853 Views
  • 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.
  • A Offline
    A Offline
    akshaybabloo
    wrote on last edited by akshaybabloo
    #1

    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:

    1. Add repaint() after setText()
    2. Add clear() before setText()
    3. Add clear() before setText() and the add repaint()
    4. Add update() after setText()
    5. 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.

    jsulmJ 1 Reply Last reply
    0
    • Cobra91151C Offline
      Cobra91151C Offline
      Cobra91151
      wrote on last edited by Cobra91151
      #6

      I have checked your repository on GitHub and fixed the issue.

      Your code:
      ui->statusbar->addPermanentWidget(new StatusBarIndicator, 1); // here you create instance of StatusBarIndicator, also new StatusBarIndicator creates 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:
      Label fixed

      1 Reply Last reply
      1
      • A akshaybabloo

        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:

        1. Add repaint() after setText()
        2. Add clear() before setText()
        3. Add clear() before setText() and the add repaint()
        4. Add update() after setText()
        5. 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.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

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

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        A 1 Reply Last reply
        1
        • jsulmJ jsulm

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

          A Offline
          A Offline
          akshaybabloo
          wrote on last edited by
          #3

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

          JonBJ 1 Reply Last reply
          0
          • A akshaybabloo

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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #4

            @akshaybabloo
            As @jsulm wrote, all should already be working.
            At a guess: inside class StatusBarIndicator, what does ui (and ui->serverStatusLabel) refer to? Is it possible it's not the label you think it is? Put some other text into the label first, then qInfo() << ui->serverStatusLabel->text(); before setting it anew in the slot? Is the signal connected to the correct instance? Try with something else?

            A 1 Reply Last reply
            0
            • JonBJ JonB

              @akshaybabloo
              As @jsulm wrote, all should already be working.
              At a guess: inside class StatusBarIndicator, what does ui (and ui->serverStatusLabel) refer to? Is it possible it's not the label you think it is? Put some other text into the label first, then qInfo() << ui->serverStatusLabel->text(); before setting it anew in the slot? Is the signal connected to the correct instance? Try with something else?

              A Offline
              A Offline
              akshaybabloo
              wrote on last edited by
              #5

              @JonB @jsulm I have updated my question with the link to the repository

              1 Reply Last reply
              0
              • Cobra91151C Offline
                Cobra91151C Offline
                Cobra91151
                wrote on last edited by Cobra91151
                #6

                I have checked your repository on GitHub and fixed the issue.

                Your code:
                ui->statusbar->addPermanentWidget(new StatusBarIndicator, 1); // here you create instance of StatusBarIndicator, also new StatusBarIndicator creates 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:
                Label fixed

                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