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. Label doesn't show up
Forum Updated to NodeBB v4.3 + New Features

Label doesn't show up

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.3k 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.
  • Y Offline
    Y Offline
    yaronct
    wrote on last edited by
    #1

    Hi.

    I have this simple piece of code:

    @#include <QApplication>
    #include <QLabel>

    int main (int argc, char *argv[]) {
    QApplication app(argc, argv);
    QWidget w;
    w.setGeometry (0, 0, 800, 450);
    QLabel a("AAA", &w);
    a.setGeometry (10, 10, 200, 100);
    w.showNormal ();
    QLabel b("BBB", &w);
    b.setGeometry (10, 410, 200, 100);
    w.update ();
    return app.exec ();
    }@

    When I run it, I see "AAA" but not "BBB". Why is that? It must be possible to add a child widget to 'w' after it's been shown!

    I'm using Qt 5.4.0 on Ubuntu 14.10.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      One problem is that you placed the B label below the bottom border of the widget. A rectangle of height 100 placed at y 410 will try to render text somewhere around y 460, which is lower than the bottom border (450).

      The other problem is described in the docs of "QWidget":http://doc.qt.io/qt-5/qwidget.html#QWidget

      bq. If you add a child widget to an already visible widget you must explicitly show the child to make it visible.

      So all you need to do is this:
      @
      QLabel b("BBB", &w);
      b.setGeometry (10, 410, 200, 100);
      b.show();
      @
      You don't need the call to w.update().

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alex_malyu
        wrote on last edited by
        #3

        Comments were removed even though I still believe that code which rely on the object order destruction in the same scope is evil.

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @alex_malyu This code is fine (for a code snippet). Order of destruction of scope variables is guaranteed by the standard and is the reverse order they were declared in. So
          @
          //this is perfectly fine:
          QWidget parent1;
          QWidget child1(&parent1); //will destruct first and detach from parent

          //this is an error:
          QWidget child2;
          QWidget parent2;
          child2->setParent(&parent2); //a double delete will occur on scope exit
          @

          The setGeometry call is setting the position in parent coordinates, so yes, they are related. The widget is just not shown initially if the parent is already visible. This is done to allow creating hidden widgets without a flicker. If they were shown initially you would have to call hide() right away but they might still be shown for a moment causing a distraction. They are shown automatically for not yet shown parents because there's no such problem there.

          Layouts should be used most of the time, but some simple non-resizable widgets can do without them fine (less typing).

          1 Reply Last reply
          0
          • Y Offline
            Y Offline
            yaronct
            wrote on last edited by
            #5

            Ok, Thanx a lot for your help!!

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              [quote author="alex_malyu" date="1423617598"]Comments were removed even though I still believe that code which rely on the object order destruction in the same scope is evil.[/quote]
              Agreed. But for small examples or snippets to illustrate an issue it's ok.

              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