问题
I have an QHBoxLayout with a QTreeWidget on the left, a separator on the middle and a widget on the right.
When I click on the QTreeWidget, I want to change the widget on the right to modify the QTreeWidgetItem
I tried to do this with this code :
def new_rendez_vous(self):
self.ui.horizontalLayout_4.removeWidget(self.ui.editionFormWidget)
del self.ui.editionFormWidget
self.ui.editionFormWidget = RendezVousManagerDialog(self.parent)
self.ui.editionFormWidget.show()
self.ui.horizontalLayout_4.addWidget(self.ui.editionFormWidget)
self.connect(self.ui.editionFormWidget, QtCore.SIGNAL('saved'), self.scheduleTreeWidget.updateData)
def edit(self, category, rendez_vous):
self.ui.horizontalLayout_4.removeWidget(self.ui.editionFormWidget)
del self.ui.editionFormWidget
self.ui.editionFormWidget = RendezVousManagerDialog(self.parent, category, rendez_vous)
self.ui.editionFormWidget.show()
self.ui.horizontalLayout_4.addWidget(self.ui.editionFormWidget)
self.connect(self.ui.editionFormWidget, QtCore.SIGNAL('saved'), self.scheduleTreeWidget.updateData)
def edit_category(self, category):
self.ui.horizontalLayout_4.removeWidget(self.ui.editionFormWidget)
del self.ui.editionFormWidget
self.ui.editionFormWidget = CategoryManagerDialog(self.parent, category)
self.ui.editionFormWidget.show()
self.ui.horizontalLayout_4.addWidget(self.ui.editionFormWidget)
self.connect(self.ui.editionFormWidget, QtCore.SIGNAL('saved'), self.scheduleTreeWidget.updateData)
But it doesn't work and all the widgets are stacked up on each other :
(source: free.fr)
.
Do you know how I can remove the old widget and next display the new one ?
回答1:
The most common solution is to use QStackedWidget and put all possible widgets into the stack. When selecting an item, just call setCurrentWidget to display the one you want.
回答2:
I have the same question as Natim.
The QStackedWidget is a solution for a preset layout. It acts like the flippy thing in an old diner for a music box. (X-amount of albums in the jukebox, flip through the installed albums).
However this does not solve the question.
For instance I have code I am prototyping with a UI layout, however I want to replace some of the widgets that are acting as place-holders with the proper widgets that are coded during the primary script execution, or is dynamically created.
I am sure there is a simple procedure or caveat as to how to properly remove/replace a widget.
The code I have has a basic textEdit widget in a grid layout. I want to code a custom version of this widget for drag and drops and then swap it out with the default textEdit.
Like Natim, the code seems logically sound, however the widgets are hap-hazardly piled in the layout like dumping a purse.
Hopefully can figure out the trick to this and repost the caveat.
SOLUTION:
Voilà!! Found something that definitely does the trick. CLOSE your widget
# Remove, Create, Replace
self.ui.gridLayout.removeWidget(self.ui.dragDataEdit)
self.ui.dragDataEdit.close()
self.ui.dragDataEdit = myDumpBox(self.ui.centralwidget)
self.ui.gridLayout.addWidget(self.ui.dragDataEdit, 0, 0, 1, 1)
self.ui.gridLayout.update()
I removed the widget from the layout, then closed the widget. At this time the variable I am using is open to create my custom/modified widget, and then re-insert it into the layout
Yes some more elegance is needed to deal with more complicated layouts, but the basic need of destroying a widget in order to replace it is in the .close() method
Cheers.. hope this helps. B
回答3:
Actually, as for Qt5 at least, there is a more compact alternative to B4adle7's solution utilizing QLayout's replaceWidget:
containing_layout = placeholder.parent().layout()
containing_layout.replaceWidget(placeholder, new_widget)
来源:https://stackoverflow.com/questions/4625102/how-to-replace-a-widget-with-another-using-qt