Remove all items from layout without destroying the items.



  • 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?


  • Moderators



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


  • Moderators

    @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.



  • 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


  • Qt Champions 2016

    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



  • 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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

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



  • 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?


  • Qt Champions 2016

    @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.



  • 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


  • Qt Champions 2016

    @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.

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.