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 ?
On Google Pixel smartphones, this is done by tapping and holding on a blank space on your Android smartphone's Home Screen (on the wallpaper, not on the icons or on other widgets). This brings up a menu, where you need to choose Widgets.
Though there's a lot of movement around QML widgets are still supported and AFAIK there are no official statements or plans to retire them in foreseeable future.
If you do not have the widget pointer, do the following: QLayoutItem * item = layout->itemAt(0); QWidget * widget = item->widget(); if (widget != NULL) { layout->removeWidget(widget); //if you want to delete the widget, do: widget->setParent(NULL); delete widget; } (formatting does not work, but you get the idea...)
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With