Got a snag with PySide2 here. So, I'm creating widgets using PySide2 through Qt Designer, and then I'm reassigning variables from the UI files, something like this:
self.refresh_btn = self.ui.findChild(QtWidgets.QPushButton, 'refresh_btn')
But when I try to use self.refresh_btn in another func,
The system log throws this at me:
#RuntimeError: Internal C++ object (PySide2.QtWidgets.QPushButton) already deleted.
Any clues on how to tackle this one? Appreciate any help!
import os
import sys
from PySide2 import QtCore, QtWidgets
from PySide2.QtCore import QFile
from PySide2.QtUiTools import QUiLoader
_CURRENT_DIR = os.path.dirname(__file__)
class TEST(QtWidgets.QDialog):
WIN_NAME = 'xxxx'
def __init__(self, parent=None):
super(self.__class__, self).__init__(parent=parent)
self._init_ui()
def _init_ui(self):
ui_file = QFile(_CURRENT_DIR + "/TEST.ui")
ui_file.open(QtCore.QIODevice.ReadOnly)
loader = QUiLoader()
self.ui = loader.load(ui_file)
ui_file.close()
self.layout = QtWidgets.QVBoxLayout(self)
self.layout.addWidget(self.ui)
self.setObjectName(self.WIN_NAME)
self.setWindowFlags(QtCore.Qt.Window)
self.setWindowTitle(self.WIN_NAME)
self.get_widgets_from_ui()
self.create_connections()
self.show()
def get_widgets_from_ui(self):
self.refresh_btn=self.ui.findChild(QtWidgets.QPushButton, 'refresh_btn')
self.clear_all_btn=self.ui.findChild(QtWidgets.QPushButton, 'clear_all_btn')
print(self.clear_all_btn)
def create_connections(self):
self.refresh_btn.clicked.connect(self.add_list_widget_to_sqe)
#i need use this fuc to dynamic add wiget to the main widget
def add_list_widget_to_sqe(self):
# print(self.layout)
print(self.clear_all_btn)# just test the clear_all_btn is living
pass
test=TEST()