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. Remove all items from layout without destroying the items.
Forum Updated to NodeBB v4.3 + New Features

Remove all items from layout without destroying the items.

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 3 Posters 3.9k 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
    Szustarol
    wrote on last edited by
    #1

    Hi!
    I am creating an app which I want to work a bit like Microsoft Excel- you have several tabs and if You
    pick one the layout changes and loads things from another layout.
    I have been trying to make it this way:

    1. make one global layout pointer
    2. when layout is changed just change the pointer to point to another layout, for example
    globallayout 1;
    if changed == true:
    1.pointto(layout2);
    else:
    1.pointto(layout3);
    

    but this is not working, I have found out that QT does not allow such things because of layout stack or something like this
    so i just try to remove all items from the layout and then add items from the other one, but my app crashes after going back to the layout, and I guess it is because I remove items from the layout and delete them.
    So my question is:
    how do I Unattach items from layout without removing them?

    jsulmJ 1 Reply Last reply
    0
    • S Szustarol

      Hi!
      I am creating an app which I want to work a bit like Microsoft Excel- you have several tabs and if You
      pick one the layout changes and loads things from another layout.
      I have been trying to make it this way:

      1. make one global layout pointer
      2. when layout is changed just change the pointer to point to another layout, for example
      globallayout 1;
      if changed == true:
      1.pointto(layout2);
      else:
      1.pointto(layout3);
      

      but this is not working, I have found out that QT does not allow such things because of layout stack or something like this
      so i just try to remove all items from the layout and then add items from the other one, but my app crashes after going back to the layout, and I guess it is because I remove items from the layout and delete them.
      So my question is:
      how do I Unattach items from layout without removing them?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Szustarol Why don't you just use http://doc.qt.io/qt-5/qtabwidget.html ?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Szustarol
        wrote on last edited by
        #3

        too much information is to be displayed on the tab to fit it into tab widget

        jsulmJ 1 Reply Last reply
        0
        • S Szustarol

          too much information is to be displayed on the tab to fit it into tab widget

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #4

          @Szustarol "too much information is to be displayed on the tab to fit it into tab widget" - I don't understand. Why is it too much and why it is not too much with your approach? You can use QScrollArea if it does not fir in the tab.
          Are you aware how QTabWidget works? You use several tabs and show only one at given time. User can switch between tabs.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Szustarol
            wrote on last edited by
            #5

            I am aware how tab widget works.
            I took a screenshot so you see how my app will look:
            alt text

            the tab on the left will also update its content and the part on the right will be a list of entries.
            Also I plan on adding more information to the left-hand tab later on

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              said in Remove all items from layout without destroying the items.:

              how do I Unattach items from layout without removing them?

              There is a TakeAt function that gives ownership back
              http://doc.qt.io/qt-4.8/qlayout.html#takeAt

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Szustarol
                wrote on last edited by Szustarol
                #7

                Okay I am getting somewhere with this, but now the container is not being repainted and everything stays on the screen (overlaps), this is my redraw function:

                void clearLayout(QLayout * layout)
                {
                    if (layout)
                    {
                        while(layout->takeAt(0) != 0){}
                    }
                }
                
                void MainWindow::reload()
                {
                    for(auto && i : this->tabs)
                    {
                        this->ui->tabsList->addItem(i); //redraws the panel on the left
                    }
                    clearLayout(this->ui->verticalLayout_2); //clears the layout
                    if(selectedTab >= 0)
                    {
                        this->ui->verticalLayout_2->setAlignment(Qt::AlignTop); //if any tab is selected draws the coresponding panel
                        this->ui->verticalLayout_2->addLayout(this->tabs.at(selectedTab)->tabContent);
                    }
                    else
                    {
                        this->ui->actionUsu_aktywny_arkusz->setEnabled(false); //if no tab is selected disable the remove tab button in the menu
                    }
                    this->ui->verticalLayout_2->update();//update the redrawn layout
                    repaint();//repaint main window
                    update();//update main window
                }
                

                alt text
                here you can see as it overlaps
                any ideas?

                edit
                i have noticed i can do this:

                QLayoutItem * child;
                      while((child = layout->takeAt(0)) != 0){
                          child->widget()->hide();
                      }
                

                but then it crashes when i switch to another tab or click the same one again, segfault is fired at the "hide" line

                mrjjM 1 Reply Last reply
                0
                • S Szustarol

                  Okay I am getting somewhere with this, but now the container is not being repainted and everything stays on the screen (overlaps), this is my redraw function:

                  void clearLayout(QLayout * layout)
                  {
                      if (layout)
                      {
                          while(layout->takeAt(0) != 0){}
                      }
                  }
                  
                  void MainWindow::reload()
                  {
                      for(auto && i : this->tabs)
                      {
                          this->ui->tabsList->addItem(i); //redraws the panel on the left
                      }
                      clearLayout(this->ui->verticalLayout_2); //clears the layout
                      if(selectedTab >= 0)
                      {
                          this->ui->verticalLayout_2->setAlignment(Qt::AlignTop); //if any tab is selected draws the coresponding panel
                          this->ui->verticalLayout_2->addLayout(this->tabs.at(selectedTab)->tabContent);
                      }
                      else
                      {
                          this->ui->actionUsu_aktywny_arkusz->setEnabled(false); //if no tab is selected disable the remove tab button in the menu
                      }
                      this->ui->verticalLayout_2->update();//update the redrawn layout
                      repaint();//repaint main window
                      update();//update main window
                  }
                  

                  alt text
                  here you can see as it overlaps
                  any ideas?

                  edit
                  i have noticed i can do this:

                  QLayoutItem * child;
                        while((child = layout->takeAt(0)) != 0){
                            child->widget()->hide();
                        }
                  

                  but then it crashes when i switch to another tab or click the same one again, segfault is fired at the "hide" line

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

                  @Szustarol
                  When u take items back. they are not removed from screen. Just not owned
                  by layout anymore.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Szustarol
                    wrote on last edited by
                    #9

                    @mrjj
                    do you see any possible fix to my problem then?

                    mrjjM 1 Reply Last reply
                    0
                    • S Szustarol

                      @mrjj
                      do you see any possible fix to my problem then?

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

                      @Szustarol
                      Delete the QLayoutItem and widget when you take them out.
                      Then insert the new widgets.

                      1 Reply Last reply
                      1
                      • S Offline
                        S Offline
                        Szustarol
                        wrote on last edited by
                        #11

                        If i do this and then want to go back to the tab I've been deleting from the app crashes.
                        So this'd be error in my code?
                        because i guess when i delete those items I cant reshow them anymore, because they are deleted right?

                        mrjjM 1 Reply Last reply
                        0
                        • S Szustarol

                          If i do this and then want to go back to the tab I've been deleting from the app crashes.
                          So this'd be error in my code?
                          because i guess when i delete those items I cant reshow them anymore, because they are deleted right?

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

                          @Szustarol
                          if u delete them , then yes they are gone.
                          but if u want to reuse them u can keep in list and reload etc.

                          If you crash, make sure nothing u delete is selected or active.

                          1 Reply Last reply
                          1
                          • S Offline
                            S Offline
                            Szustarol
                            wrote on last edited by
                            #13

                            Okay sorry for keeping the thread dead for some time.
                            I have been trying your soultion, but copy constructor cannot be used so i cannot make a list of temporary objects i'd like to display.
                            Let's say you have a vector of QWidgets and you want to create another vector, with exactly the same QWidgets, how would you do it, because i have seriously no idea

                            mrjjM 1 Reply Last reply
                            0
                            • S Szustarol

                              Okay sorry for keeping the thread dead for some time.
                              I have been trying your soultion, but copy constructor cannot be used so i cannot make a list of temporary objects i'd like to display.
                              Let's say you have a vector of QWidgets and you want to create another vector, with exactly the same QWidgets, how would you do it, because i have seriously no idea

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

                              @Szustarol said in Remove all items from layout without destroying the items.:

                              make a list of temporary objects i'd like to display.

                              std::vector<QWidget *> mylist will be able to hold it those you take out.
                              (or Qlist)

                              • you want to create another vector, with exactly the same QWidgets,
                                That is not really possible.
                              1 Reply Last reply
                              1

                              • Login

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