pyqt4:如何显示非模式对话框?
对于我的一生,我无法弄清楚这一点......按下按钮时,我有代码:
@QtCore.pyqtSlot():
def buttonPressed(self):
d = QtGui.QDialog()
d.show()
所发生的只是一个没有任何内容的窗口短暂弹出,然后消失。反复点击按钮没有帮助。
使用Python 2.6,最新的PyQt4。
for the life of me I can't figure this out... on a button press I have the code:
@QtCore.pyqtSlot():
def buttonPressed(self):
d = QtGui.QDialog()
d.show()
all that happens is a window briefly pops up without any contents and then disappears. Hitting the button repeatedly doesn't help.
Using Python 2.6, latest PyQt4.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我没记错的话,似乎其他人也有类似问题。似乎发生的情况是,您定义了一个局部变量 d 并将其初始化为 QDialog,然后显示它。问题是,一旦
buttonPressed
处理程序执行完毕,对d
的引用就会超出范围,因此它会被垃圾收集器销毁。尝试执行类似self.d = QtGui.QDialog()
的操作以将其保持在范围内。If I am not mistaken, it seems that someone else had a similar issue. What seems to be happening is that you define a local variable
d
and initialize it as aQDialog
, then show it. The problem is that once thebuttonPressed
handler is finished executing, the reference tod
goes out of scope, and so it is destroyed by the garbage collector. Try doing something likeself.d = QtGui.QDialog()
to keep it in scope.当您创建对话框时,您应该将父级传递给对话框,如下所示:
这将保留对 QDialog 对象的引用,并将其保留在范围内。如果您将适当的 QMainWindow 等作为父级传递,它还允许对话框的正确行为。
You should pass a parent to the dialog when you created it like this:
This will keep the reference to the QDialog object, keeping it in scope. It also allows proper behavior of the Dialog if you are passing the appropriate QMainWindow, etc. as the parent.