为什么对话不显示我是否设定?
我创建一个对话框,如果我将MainWindow设置为父母,则不会显示。 MainWindows是父母?
from qtpy.QtWidgets import *
from qtpy.QtGui import *
from qtpy.QtCore import *
def center(w: QWidget):
top = QApplication.activeWindow()
rect = QRect(top.mapToGlobal(QPoint(0, 0)), top.size())
r = QStyle.alignedRect(Qt.LeftToRight, Qt.AlignCenter, w.size(), rect)
w.move(r.topLeft())
class Button(QPushButton):
def mousePressEvent(self, e: QMouseEvent) -> None:
super().mousePressEvent(e)
dlg = QDialog()
dlg.resize(100, 100)
dlg.setParent(QApplication.activeWindow())
# center(dlg)
dlg.exec()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setCentralWidget(Button())
app = QApplication([])
main = MainWindow()
main.show()
app.exec()
I create a dialog, and If I set mainwindow as it's parent, it not show.If I comment dlg.setParent(QApplication.activeWindow())
,It work well, It's that dialog couldn't set mainwindows as it's parent?
from qtpy.QtWidgets import *
from qtpy.QtGui import *
from qtpy.QtCore import *
def center(w: QWidget):
top = QApplication.activeWindow()
rect = QRect(top.mapToGlobal(QPoint(0, 0)), top.size())
r = QStyle.alignedRect(Qt.LeftToRight, Qt.AlignCenter, w.size(), rect)
w.move(r.topLeft())
class Button(QPushButton):
def mousePressEvent(self, e: QMouseEvent) -> None:
super().mousePressEvent(e)
dlg = QDialog()
dlg.resize(100, 100)
dlg.setParent(QApplication.activeWindow())
# center(dlg)
dlg.exec()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setCentralWidget(Button())
app = QApplication([])
main = MainWindow()
main.show()
app.exec()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从QT文档中:
setParent
方法需要两个参数,第二个是窗口标志,因此遗漏了第二个参数,使您拥有没有窗口标志的qdialog。为了再次显示对话框,只需重新分配窗口标志即可。From the Qt documentation:
The
setParent
method it takes two arguments, the second being the window flags, so leaving out the second argument leaves you with a qdialog with no window flag. In order do show the dialog again, just reassign the window flags.