Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a Widget with another using Qt?

Tags:

python

qt

pyqt

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 :

Exemple of the bug
(source: free.fr)
.

Do you know how I can remove the old widget and next display the new one ?

like image 485
Natim Avatar asked Jan 07 '11 11:01

Natim


People also ask

How do I replace a widget?

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.

Is Qt widgets deprecated?

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.

How do I remove Qt widgets?

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


2 Answers

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

like image 130
B4adle7 Avatar answered Sep 22 '22 09:09

B4adle7


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.

like image 27
Lukáš Lalinský Avatar answered Sep 24 '22 09:09

Lukáš Lalinský