问题
I am new in PyQt in python and I have been working on a GUI lately. One of the parts of the GUI is a button which is supposed to get rid of all lines currently in the GUI. Because I am using threads, the way I implemented this feature is to have a GUI attribute, self.clear, which is set to true when the button is pressed. Then, inside the main thread at the end I am checking if self.clear is true and if so I clear the lines. However, that results in segfaults. Through print statements I believe that I have found the error to be something about the self.clear variable. Also, I notice that when the button is pressed the self.clear variable in the main thread is still false and was not updated. Any ideas what could be causing the segfault? Thank you!
UPDATE: Here is the code for putting the button in the GUI
self.clearBtn = QPushButton('Clear History')
self.clearBtn.clicked.connect(self.clearHistory)
self.vbox.addWidget(self.clearBtn)
Clear history does the following:
def clearHistory(self):
self.clear = True
Then in the main handler function, in the end I have the following code:
if (self.clear == True):
# self.clearHistory()
for shape in self.shapes.values():
if (shape.type == "line"):
for line in shape.object:
om.removeFromObjectModel(line)
shape.object = deque()
shape.duration = None
self.clear = False
But the function usually does not enter the conditional, however, there are cases when it does.
回答1:
Segfault usually occurs when you're passing Widgets or any UI elements from the main UI Thread to a child QtThread, make sure you're only using your widgets in the main thread of the UI, and if you need to change the UI based on some signal from a QtThread then create a slot in the main class and update the UI from there, check out QtCore.pyqtSignal
来源:https://stackoverflow.com/questions/62638625/segfault-in-python-using-pyqt