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. Set QMainWindow layout
Forum Updated to NodeBB v4.3 + New Features

Set QMainWindow layout

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

    I am trying to make a widget, which contains in its vertical layout few other windows, so:

    void add_window(QMainWIndow* ptr)
    {
    ui->vertical_laout->addWidget(ptr);
    }
    

    Very simple, everything works fine, untill i want to remove that window from that layout, and open it as it was before, this doesn't happen. From what the removeWidget says

    "void QLayout::removeWidget(QWidget *widget)
    Removes the widget widget from the layout. After this call, it is the caller's responsibility to give the widget a reasonable geometry or to put the widget back into a layout or to explicitly hide it if necessary."

    But how to use the default layout? I tried both these functions but none works, the first one crash and the second one doesn't do anything.

    void Window::default_layout()
    {
        ui->centralWidget->layout()->addWidget(this);
    }
    
    void Window::default_layout()
    {
        setCentralWidget(ui->centralwidget);
    }
    

    As you probably can guess, i tried mdi area, but in fact the same problem occurred, the window can't be reopened normally after it's been added to another mdi area with the exception there was more code to do what i wanted.

    JonBJ 1 Reply Last reply
    0
    • L Loc888

      I am trying to make a widget, which contains in its vertical layout few other windows, so:

      void add_window(QMainWIndow* ptr)
      {
      ui->vertical_laout->addWidget(ptr);
      }
      

      Very simple, everything works fine, untill i want to remove that window from that layout, and open it as it was before, this doesn't happen. From what the removeWidget says

      "void QLayout::removeWidget(QWidget *widget)
      Removes the widget widget from the layout. After this call, it is the caller's responsibility to give the widget a reasonable geometry or to put the widget back into a layout or to explicitly hide it if necessary."

      But how to use the default layout? I tried both these functions but none works, the first one crash and the second one doesn't do anything.

      void Window::default_layout()
      {
          ui->centralWidget->layout()->addWidget(this);
      }
      
      void Window::default_layout()
      {
          setCentralWidget(ui->centralwidget);
      }
      

      As you probably can guess, i tried mdi area, but in fact the same problem occurred, the window can't be reopened normally after it's been added to another mdi area with the exception there was more code to do what i wanted.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Loc888
      A QMainWindow expects you put a widget in as its central widget, e.g.

      mainWidget = new QWidget;
      mainWindow->setCentralWidget(mainWidget);
      

      Then that central widget can have a layout and other widgets can be placed on that, just like any other widget, e.g.

      layout = new QVerticalLayout;
      combobox = new QComboBox;
      layout->addWidget(combobox);
      mainWidget->setLayout(layout);
      
      L 1 Reply Last reply
      1
      • JonBJ JonB

        @Loc888
        A QMainWindow expects you put a widget in as its central widget, e.g.

        mainWidget = new QWidget;
        mainWindow->setCentralWidget(mainWidget);
        

        Then that central widget can have a layout and other widgets can be placed on that, just like any other widget, e.g.

        layout = new QVerticalLayout;
        combobox = new QComboBox;
        layout->addWidget(combobox);
        mainWidget->setLayout(layout);
        
        L Offline
        L Offline
        Loc888
        wrote on last edited by
        #3

        @JonB This doesn't work, also it's not what i was asking.

        JonBJ 1 Reply Last reply
        0
        • L Loc888

          @JonB This doesn't work, also it's not what i was asking.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Loc888 said in Set QMainWindow layout:

          This doesn't work

          :)

          Best of luck.

          L 1 Reply Last reply
          0
          • JonBJ JonB

            @Loc888 said in Set QMainWindow layout:

            This doesn't work

            :)

            Best of luck.

            L Offline
            L Offline
            Loc888
            wrote on last edited by
            #5

            @JonB Luck also doesn't help.

            mrjjM 1 Reply Last reply
            0
            • L Loc888

              @JonB Luck also doesn't help.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Loc888

              Hi
              What do you mean by reopen normally ?
              After the QMainWindow has been inside layout, when you take it out it will have other
              size and you might need to call setGeometry to set it to something bigger.
              It will not go back to the size it had before being stuffed into the a layout.
              Also when you insert a Window into layout , some windows flag are set / removed and often
              you will need to call show() on it to have it show as a window again.

              1 Reply Last reply
              2

              • Login

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