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.
Qt 6.11 is out! See what's new in the release blog

Remove all items from layout without destroying the items.

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 3 Posters 6.3k 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
    #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