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. [SOLVED] QLabel->isVisible always returns false
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QLabel->isVisible always returns false

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 14.0k 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.
  • B Offline
    B Offline
    beowulf
    wrote on last edited by
    #1

    @MyLabel = new QLabel(this);
    MyLabel ->setVisible(true);
    qDebug() << MyLabel ->isVisible();@

    bq.
    Output: false

    Am I doing something wrong?

    -- 0x00

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on last edited by
      #2

      If you are using the above statement inside the constructor it will always result false. Calling setVisible(true) will set the widget to be visible if all its parents widget up to the window are visible.

      Once the mainwindow/parent is visible only then the label visible property is changed to true.

      Check "this":http://qt-project.org/doc/qt-5.0/qtwidgets/qwidget.html#visible-prop for more information.

      1 Reply Last reply
      0
      • W Offline
        W Offline
        willi4ms
        wrote on last edited by
        #3

        hello l3e0wulf, I executed the following code:

        Inside the constructor class:

        MyLabel = new QLabel(this);
        MyLabel->setVisible(true);
        qDebug() << MyLabel->isVisible();

        in the main function:

        {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        qDebug() << w.MyLabel->isVisible();

        return a.exec();
        }

        Result:

        false
        true

        1 Reply Last reply
        0
        • B Offline
          B Offline
          beowulf
          wrote on last edited by
          #4

          bq. If you are using the above statement inside the constructor it will always result false. Calling setVisible(true) will set the widget to be visible if all its parents widget up to the window are visible.
          Once the mainwindow/parent is visible only then the label visible property is changed to true.
          Check this [qt-project.org] for more information.

          I can't understand how fix it.

          @MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
          {

          MyLabel = new QLabel(this);
          MyLabel ->setVisible(true);
          qDebug() << MyLabel ->isVisible();

          }@

          -- 0x00

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Sam
            wrote on last edited by
            #5

            Its simple the parent of the MyLabel is MyWidget, and at the start MyWidget is not visible. So inside the main you write

            @MyWidget w;
            w.show(); @

            When MyWidget is initialised (first line) it call its constructor, and inside the constructor you have initialised MyLabel and set its property setVisible to "true", and the next line is qDebug() , now according to the documentation :

            bq. "Calling setVisible(true) will set the widget to be visible if all its parents widget up to the window are visible"

            So MyWidget is not yet visible unless w.show() is called. Hence when the parent becomes visible then the property of the child setVisible(true) is reflected.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              beowulf
              wrote on last edited by
              #6

              Still don't working.

              @MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
              {

              ui->setupUi(this);
              
              MyWidget *MWidget = new MyWidget(this);
              
              MyWidget->setGeometry(QRect(0, 0, 336, 425));
              
              MyWidget->show();
              

              }

              MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
              {

              MyLabel = new QLabel(this);
              MyLabel ->setVisible(true);
              qDebug() << MyLabel ->isVisible();

              }@

              -- 0x00

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Sam
                wrote on last edited by
                #7

                This wont work like this , See when you initialise

                @MyWidget *MWidget = new MyWidget(this);@

                it calls MyWidget constructor i.e

                @MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
                {
                MyLabel = new QLabel(this);

                MyLabel ->setVisible(true);

                qDebug() << MyLabel ->isVisible();

                }@

                and till this point MyWidget.show() is not called or MyWidget is not visible so output will be false (inside the constructor). Just for test you can call show() inside the constructor and check the two behaviour.

                @MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
                {

                show();

                MyLabel = new QLabel(this);

                MyLabel ->setVisible(true);

                qDebug() << MyLabel ->isVisible();

                }@

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  beowulf
                  wrote on last edited by
                  #8

                  Still does not work, only works if I put show () inside the constructor MainWindow (); example:

                  @MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
                  {

                  ui->setupUi(this);
                  
                  show();
                  
                  MyWidget *MWidget = new MyWidget(this);
                  

                  }

                  MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
                  {

                  show();

                  MyLabel = new QLabel(this);

                  MyLabel ->setVisible(true);

                  qDebug() << MyLabel ->isVisible();

                  }@

                  The funny thing is that in the main.cpp the MainWidget already with show().

                  -- 0x00

                  1 Reply Last reply
                  0
                  • W Offline
                    W Offline
                    willi4ms
                    wrote on last edited by
                    #9

                    Hello put the

                    @qDebug() << MyLabel ->isVisible();@

                    in another method not inside the constructor because when you call inside the constructor the widget/window and parents are not still visible.

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      beowulf
                      wrote on last edited by
                      #10

                      Solved! Thanks Sam and willi4ms!

                      -- 0x00

                      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