Segfault in python using PyQt [closed]

谁说胖子不能爱 提交于 2020-07-03 13:28:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!