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. appending new widgets to dialog derived from base dialog
Forum Updated to NodeBB v4.3 + New Features

appending new widgets to dialog derived from base dialog

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 378 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.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by
    #1

    i have a base class for dialog, that adds widgets to layout:

    class BaseDialog
    {
    public:
      BaseDialog(QWidgets *parent)
      {
        auto pLabel = new QLabel("base");
        auto pLayout = new QVBoxLayout(this);
        pLayout->addWidget(pLabel);
      }
    };
    

    then i have a derived dialog class, which needs to add extra widgets to the dialog:

    class DerviedDialog : public BaseDialog
    {
    public:
      DerviedDialog(QWidgets *parent)
      {
        auto pOtherLabel = new QLabel("derived");
        auto pLayout = new QVBoxLayout(this);
        pLayout->addLayout(layout()); // <-- not possible!
        pLayout->addWidget(pLabel);
      }
    };
    

    here i am getting the error:

    QWidget::setLayout: Attempting to set QLayout "" on MainWindow "", which already has a layout
    

    i know from the docs:

    If there already is a layout manager installed on this widget, QWidget won't let you install another. You must first delete the existing layout manager (returned by layout()) before you can call setLayout() with the new layout.
    

    but i can't really delete the old layout, because i need to widgets added by base class too.

    how should i fix this?

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

      Hi,

      Why not just add your widget to the current layout of the dialog ?

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

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

        First - you're missing the call to the base constructor in your derived class, which means you're not passing the parent parameter down the inheritance chain. Also it should be QWidget not QWidgets.

        As for the question - don't crate another layout. Just use the one already created by the base class by calling layout() to get it.

        1 Reply Last reply
        2
        • SGaistS SGaist

          Hi,

          Why not just add your widget to the current layout of the dialog ?

          U Offline
          U Offline
          user4592357
          wrote on last edited by
          #4

          @SGaist thanks. when i add the widget directly in layout()->addWidget(w); the new widget sits on top of other widgets (coming from base class). what i am doing wrong?

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

            What if you cast the layout to its original QVBoxLayout type ?

            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
            0

            • Login

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