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. Subclass of QDialog isn't displayed correctly
Forum Updated to NodeBB v4.3 + New Features

Subclass of QDialog isn't displayed correctly

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

    Hey, I make a subclass of QDialog with a simple QFormLayout with 2 Labels, and it looks like this:
    0_1557769009551_3f0b7549-038c-4319-a0ce-94f1c5580d55-image.png

    It appears that the two labels have been put on top of each other...

    Header:

    #ifndef DIALOGNEWBRANCH_H
    #define DIALOGNEWBRANCH_H
    
    #include <QDialog>
    
    class DialogNewBranch : public QDialog
    {
        Q_OBJECT
    
    public:
        DialogNewBranch(QWidget* parent);
    
    };
    
    #endif // DIALOGNEWBRANCH_H
    
    

    Class:

    #include "dialognewbranch.h"
    #include <QFormLayout>
    #include <QLabel>
    
    DialogNewBranch::DialogNewBranch(QWidget* parent) : QDialog(parent)
    {
        QFormLayout form(this);
        form.addRow(new QLabel("Name:"));
        form.addRow(new QLabel("Position:"));
    }
    
    

    Code referencing the class:

    DialogNewBranch* dialog = new DialogNewBranch(this);
    
    if (dialog->exec() == QDialog::Accepted)
    {
        // TODO
    }
    

    What am I doing wrong?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      Hi and welcome to devnet,

      Your layout is on the stack hence it's destroyed at then of the constructor.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by Kent-Dorfman
        #3

        @astavie said in Subclass of QDialog isn't displayed correctly:

        DialogNewBranch::DialogNewBranch(QWidget* parent) : QDialog(parent)
        {
        QFormLayout form(this);
        form.addRow(new QLabel("Name:"));
        form.addRow(new QLabel("Position:"));
        }

        Generally when you do this, you use this->setLayout(&form) so that the layout is saved and "assigned" to the widget. As SGaist mentioned, your locally allocated layout goes away at the end of the constructor unless you keep a reference to it as I've done above...maybe reference isn't the right term, as it probably is member copied internally.

        Kent-DorfmanK 1 Reply Last reply
        2
        • Kent-DorfmanK Kent-Dorfman

          @astavie said in Subclass of QDialog isn't displayed correctly:

          DialogNewBranch::DialogNewBranch(QWidget* parent) : QDialog(parent)
          {
          QFormLayout form(this);
          form.addRow(new QLabel("Name:"));
          form.addRow(new QLabel("Position:"));
          }

          Generally when you do this, you use this->setLayout(&form) so that the layout is saved and "assigned" to the widget. As SGaist mentioned, your locally allocated layout goes away at the end of the constructor unless you keep a reference to it as I've done above...maybe reference isn't the right term, as it probably is member copied internally.

          Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by Kent-Dorfman
          #4

          @Kent-Dorfman

          Actually, I got to thinking about stack allocated widgets and layouts and decided to check the sourcecode. In QWidget::setLayout() there does NOT appear to be any object copy of the layout. It just saves the address of the already created layout, and possibly "reparents" the widgets in the layout...So, stack created layouts are probably a very bad idea...

          Also, the sourcecode comments indicate that if the layout constructor references the target widget that will own the layout, then setLayout is not explicitly required.

          JonBJ 1 Reply Last reply
          0
          • Kent-DorfmanK Kent-Dorfman

            @Kent-Dorfman

            Actually, I got to thinking about stack allocated widgets and layouts and decided to check the sourcecode. In QWidget::setLayout() there does NOT appear to be any object copy of the layout. It just saves the address of the already created layout, and possibly "reparents" the widgets in the layout...So, stack created layouts are probably a very bad idea...

            Also, the sourcecode comments indicate that if the layout constructor references the target widget that will own the layout, then setLayout is not explicitly required.

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @Kent-Dorfman

            In QWidget::setLayout() there does NOT appear to be any object copy of the layout.

            Just as a by-the-by: since all QLayouts inherit from QObject you know that there will be no copy, QObjects can't be copied....

            Kent-DorfmanK 1 Reply Last reply
            2
            • JonBJ JonB

              @Kent-Dorfman

              In QWidget::setLayout() there does NOT appear to be any object copy of the layout.

              Just as a by-the-by: since all QLayouts inherit from QObject you know that there will be no copy, QObjects can't be copied....

              Kent-DorfmanK Offline
              Kent-DorfmanK Offline
              Kent-Dorfman
              wrote on last edited by
              #6

              @JonB said in Subclass of QDialog isn't displayed correctly:

              ust as a by-the-by: since all QLayouts inherit from QObject you know that there will be no copy, QObjects can't be copied....

              That's actually what got me thinking about the flaw in my logic.

              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