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:- make one global layout pointer
- 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? -
@Szustarol Why don't you just use http://doc.qt.io/qt-5/qtabwidget.html ?
-
@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:
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 -
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 }
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
-
@Szustarol
When u take items back. they are not removed from screen. Just not owned
by layout anymore. -
@Szustarol
Delete the QLayoutItem and widget when you take them out.
Then insert the new widgets. -
@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 -
@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.
- you want to create another vector, with exactly the same QWidgets,