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 local variable not showing up
Forum Update on Monday, May 27th 2025

QLabel local variable not showing up

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 3.2k 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.
  • V Offline
    V Offline
    Vinay_Kumar
    wrote on last edited by
    #1

    Hi,

    I am new to QT and was experimenting few stuffs to get going. I created a UI layout and added menu Tool bar.
    Upon clicking one of the menu option i invoke _triggered function. Inside that i am creating local variable of QLabel and displaying some message., However this does not show up!. If i creating a member in my class and invoke label.show(), it works. Here is the code.

    void MainWindow::on_actionRefresh_triggered()
    {

    QLabel label ("Refreshing");
    label.show();
    

    }

    Is there anything that i am doing wrong?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      GrahamL
      wrote on last edited by
      #2

      Hi
      Welcome to Qt!
      Your local variable (label) is going out of scope.
      You could create it as a pointer -

      @
      QLabel* label = new QLabel("Refreshing");
      @

      Also when including code in your posts you should use code blocks

      Hope this helps

      1 Reply Last reply
      0
      • musimbateM Offline
        musimbateM Offline
        musimbate
        wrote on last edited by
        #3

        Hi and welcome to devnet:

        change your mainwindow to this:
        @

        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H

        #include <QMainWindow>
        #include <QLabel>

        namespace Ui {
        class MainWindow;
        }

        class MainWindow : public QMainWindow
        {
        Q_OBJECT

        public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();

        private:
        Ui::MainWindow *ui;
        QLabel *mLabel;
        };

        #endif // MAINWINDOW_H

        @

        instantiate the label in the constructor like this(mainwindow.cpp)

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

        MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
        {
        ui->setupUi(this);
        mLabel=new QLabel(this);
        mLabel->setText("Refreshing");
        mLabel->hide();
        }

        MainWindow::~MainWindow()
        {
        delete ui;
        }

        @

        and in your slot you can do what you did showing the label.Also it is good practice to wrap your code inside @s for readability.

        Why join the navy if you can be a pirate?-Steve Jobs

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          Hmm,
          But your code does work fine. What it does is:

          • Create a QLabel (probably a floating window since it doesn't have a parent)
          • Set the text of the QLabel
          • Show the QLabel on screen
          • Delete the QLabel (function closed!!!!!, scope lost)
            The Show function is only to get the widget on top and not minimized etc, but you delete it immediately when the function is completed, so it gets removed from screen.
            Just some thoughts:
            Add a Qlabel to your statusBar (comes with MainWindow-form) and place the information there or generate a QMessagebox::information() in your triggered slot. that is a modal dialog that needs to be accepted/cleared before being removed from screen.

          Greetz, Jeroen

          1 Reply Last reply
          0

          • Login

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