PDA

View Full Version : Segfault trying to access gui (single thread)



alexandernst
11th April 2010, 15:15
I have a problem trying to hide/show my widget.
This is an example of what I have:

main.py


class Main(QMainWindow):
def __init__(self):
QMainWindow.__init__(self, parent=None)
self.gui = Gui()

def onMyCustomEvent(self):
self.gui.showOrHide() #<---- SEGFAULT

app = QtGui.QApplication(sys.argv)
window = Main()
sys.exit(app.exec_())


gui.py


class Gui(QWidget):
def __init__(self):
QWidget.__init__(self, parent=None)
#set size of widget
#set title
#.....
#.....

def showOrHide(self):
if self.isVisible():
self.setVisible(False)
else:
self.setVisible(True)



So, this is the code, and when I try to show or hide the gui, I get a SEGFAULT or a message saying "QCoreApplication::sendPostedEvents: Cannot send posted events for objects in another thread". The SEGFAULT and the message appear randomly.
I have searched and the message means that I must use processEvent, postEvent and so on because I'm trying to access a gui from another thread, but I'm _not_! I don't use threads in my app, so I'm a little confused.
Why could be happening?

Thanks

alexandernst
12th April 2010, 01:12
I have been playing with this all day and I found that if I do "print self.width()" in the "showOrHide" method, it will print the width of the widget and it _won't_ segfault. In fact, I can access every method from my qwidget that will return a value, and I can print it. But I can't set any value to my widget because it will segfault.
Any ideas? Any clues of why could this be happening?

alexandernst
12th April 2010, 18:34
Found a solution. In "onMyCustomEvent()" instead of doing "self.gui.showOrHide()" I do "self.emit(QtCore.SIGNAL("showOrHideEvent()"))" and in main I do "self.connect(self, QtCore.SIGNAL("showOrHideEvent()"), self.console.showOrHide)" so I can catch it.
Thanks to everyone for reading this topic.