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. Widget construction
Forum Updated to NodeBB v4.3 + New Features

Widget construction

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 3.8k 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.
  • S Offline
    S Offline
    s.frings
    wrote on last edited by
    #1

    Hello,

    To add widgets to a layout I could write:
    @
    QLabel label("Name:");
    QLineEdit lineEdit;
    QHBoxLayout layout;
    layout.addWidget(&label);
    layout.addWidget(&lineEdit);@

    Why does nit not work, when I write:
    @ QLabel label("Name:");
    QLineEdit lineEdit();
    QHBoxLayout layout();
    layout.addWidget(&label);
    layout.addWidget(&lineEdit);@

    I thought that I create a QLineEdit object and a QHBoxLayout object by calling their default constructor. But this is obviously not the case, because the compiler refuses to compile that code. If these two line are not constructor calls, what else happens here?

    1 Reply Last reply
    0
    • ZlatomirZ Offline
      ZlatomirZ Offline
      Zlatomir
      wrote on last edited by
      #2

      For the default constructor use: QHBoxLayout layout;

      Because this: QHBoxLayout layout(); gets interpreted like a function declaration (a function named layout witch doesn't take any parameters and returns a QHBoxLayout)

      https://forum.qt.io/category/41/romanian

      1 Reply Last reply
      0
      • I Offline
        I Offline
        iunknwn
        wrote on last edited by
        #3

        QLineEdit lineEdit(); or QHBoxLayout layout() could be parsed as an object definition with an empty initializer or a function declaration. The language standard specifies that the ambiguity is always resolved in favor of the function declaration. An empty parentheses initializer is allowed in other contexts e.g. in a new expression or constructing a value-initialized temporary.

        @QLineEdit* lineEdit = new QLineEdit();@

        In Qt, it is a good practice to create all widgets (all classes that inherits QWidget) on heap, that way they all stay in parent-child relationship and parents cleans up on deletion.

        So I would rather do this.
        @
        QWidget* widget = new QWidget(); //parent
        QLabel* label = new QLabel("Name:");
        QLineEdit* lineEdit = new QLineEdit();
        QHBoxLayout* layout = new QHBoxLayout();
        layout->addWidget(label);
        layout->addWidget(lineEdit);

        widget->setLayout(layout); //This re-parents layout, label and lineEdit as children of widget.
        @

        Vista x64 with Qt 4.8.2 and VS2010

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lyuts
          wrote on last edited by
          #4

          Zlatomir is write, in your code:

          @QLineEdit lineEdit();@

          this means the declaration of a function. Everybody should be careful about this. Scott Meyers mentioned about this in his books. That's why I prefer writing

          @QLineEdit lineEdit; // on stack@

          but

          @QLineEdit *lineEdit = new QLineEdit(); // on heap@

          Just to expand iunknwn's explanation:

          @QLabel label("Name:");
          QLineEdit lineEdit;
          QHBoxLayout layout;
          layout.addWidget(&label);
          layout.addWidget(&lineEdit);@

          This won't work. Look at it this way. As soon as the execution leaves this block, label, lineEdit and layout will be destroyed, because they are local variable (i.e. on stack). So You have to declare them on heap. (Or maybe, make them members of your class. It depends on your design).

          I'm a rebel in the S.D.G.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            s.frings
            wrote on last edited by
            #5

            Thanks for your help. That answers my question.

            I also understand that I normally should create Widgets on the heap. In case of that small test application, the code above works because it is located in the main() function.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              s.frings
              wrote on last edited by
              #6

              Meanwhile bought a helpful book. I understand now that the Mainwindow calls "delete" for all child elements, which is a second cause why child elements must be created on the heap.

              My program possibly crashed whenever I closed the main windows but I did not notice it because I did not execute it from a shell window.

              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