Im struggling a bit with replacing widgets(Qpushbutton) with a custom button that has a enter event for qIcon and keeping the nested layout structure from a external ui.
The replaced button shows up in the ui, but it doesnt retain the exact same position as the existing button.
from PySide2.QtWidgets import QPushButton, QDialog, QVBoxLayout, QApplicationfrom PySide2.QtGui import QPixmap, QIconfrom PySide2.QtCore import QSizefrom PySide2.QtUiTools import QUiLoaderclass HoverPushButton(QPushButton): def __init__(self, normal_pixmap, hover_pixmap, button_size): super(HoverPushButton, self).__init__() self.icon_normal = QIcon(normal_pixmap) self.icon_over = QIcon(hover_pixmap) self.setIcon(self.icon_normal) self.setIconSize(QSize(button_size, button_size)) def enterEvent(self, event): self.setIcon(self.icon_over) return super(HoverPushButton, self).enterEvent(event) def leaveEvent(self, event): self.setIcon(self.icon_normal) return super(HoverPushButton, self).leaveEvent(event)class MyDialog(QDialog): def __init__(self): super(MyDialog, self).__init__() self.setup_ui() def setup_ui(self): # Load your UI from .ui file loader = QUiLoader() ui_directory = 'D:/Python/UI/' ui_file = ui_directory+'imagedialog_simple.ui' self.ui = loader.load(ui_file) self.resize(900, 800) # Set QVBoxLayout for the QDialog layout = QVBoxLayout(self) layout.setContentsMargins(0, 30, 0, 0) layout.addWidget(self.ui) # Load sprite sheet self.sprite_sheet = QPixmap(ui_directory+'sprite_canvas.png') self.sprite_sheet_hover = QPixmap(ui_directory+'sprite_canvas_hover.png') icon_size = 85 button_size = 35 # Call the function to replace buttons self.brush_button = self.ui.findChild(QPushButton, 'pushButton_penBrush') self.brush_button_replaced = self.replace_custom_button(self.brush_button, 1, 1, icon_size, button_size) print(self.brush_button_replaced.pos()) print(self.brush_button_replaced.parentWidget()) def replace_custom_button(self, existing_button, row, column, icon_size, button_size): # Crop images from sprite sheet normal_pixmap = self.sprite_sheet.copy(column * icon_size, row * icon_size, icon_size, icon_size) hover_pixmap = self.sprite_sheet_hover.copy(column * icon_size, row * icon_size, icon_size, icon_size) # Create an instance of HoverPushButton with the specified button_size new_button = HoverPushButton(normal_pixmap, hover_pixmap, button_size) # Clone minimum size new_button.setMinimumSize(existing_button.minimumSize()) # Clone maximum size new_button.setMaximumSize(existing_button.maximumSize()) new_button.setText(existing_button.text()) # Replace the existing button with the new custom button self.layout().replaceWidget(existing_button, new_button) new_button.move(existing_button.pos()) new_button.setParent(existing_button.parentWidget()) existing_button.deleteLater() return new_buttonif __name__ == '__main__': #~ app = QApplication([]) dialog = MyDialog() dialog.show() #~ app.exec_()
Whats the proper way of replacing a existing widget, maintaining the nested structure (parent widget) so it get the correct layout placement and position?
I tried to use move and it works if the layout is broken (x,y) but when using vertical, horizontal layout etc then the button doesnt align with the existing buttonScreenshot of the placement compared to qtdesigner.