pyqt4:如何显示非模式对话框?

发布于 2024-11-04 11:49:15 字数 226 浏览 4 评论 0原文

对于我的一生,我无法弄清楚这一点......按下按钮时,我有代码:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哽咽笑 2024-11-11 11:49:15

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 a QDialog, then show it. The problem is that once the buttonPressed handler is finished executing, the reference to d goes out of scope, and so it is destroyed by the garbage collector. Try doing something like self.d = QtGui.QDialog() to keep it in scope.

绿光 2024-11-11 11:49:15

当您创建对话框时,您应该将父级传递给对话框,如下所示:

@QtCore.pyqtSlot():
def buttonPressed(self):
    d = QtGui.QDialog(self)
    d.show()

这将保留对 QDialog 对象的引用,并将其保留在范围内。如果您将适当的 QMainWindow 等作为父级传递,它还允许对话框的正确行为。

You should pass a parent to the dialog when you created it like this:

@QtCore.pyqtSlot():
def buttonPressed(self):
    d = QtGui.QDialog(self)
    d.show()

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文