为什么对话不显示我是否设定?

发布于 2025-02-06 09:40:37 字数 879 浏览 1 评论 0原文

我创建一个对话框,如果我将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 技术交流群。

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

发布评论

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

评论(1

勿忘初心 2025-02-13 09:40:38

从QT文档中:

对话框始终是顶级小部件,但是如果它具有父级,则其默认位置的中心位于父母的顶级小部件的顶部(如果不是顶级窗口本身)。它还将共享父母的任务栏条目。

使用Qwidget :: setParent()函数的过载来更改QDialog窗口小部件的所有权。此功能允许您明确设置重新修改小部件的窗口标志;使用超载函数将清除窗口标志指定小部件的窗口系统属性(特别是它将重置QT :: Dialog标志)。

setParent方法需要两个参数,第二个是窗口标志,因此遗漏了第二个参数,使您拥有没有窗口标志的qdialog。为了再次显示对话框,只需重新分配窗口标志即可。

From the Qt documentation:

A dialog is always a top-level widget, but if it has a parent, its default location is centered on top of the parent's top-level widget (if it is not top-level itself). It will also share the parent's taskbar entry.

Use the overload of the QWidget::setParent() function to change the ownership of a QDialog widget. This function allows you to explicitly set the window flags of the reparented widget; using the overloaded function will clear the window flags specifying the window-system properties for the widget (in particular it will reset the Qt::Dialog flag).

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.

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