简体   繁体   English

调整 QDialog 的大小作为 MainWindow 调整大小

[英]Resize QDialog as MainWindow resizes

There is a QWidget with a button, which brings up a QDialog on click.有一个带有按钮的 QWidget,点击后会弹出一个 QDialog。 setParent of the dialog is set to QWidget.对话框的 setParent 设置为 QWidget。 Every time the button is clicked, the dialog comes up with a specific size.每次单击按钮时,对话框都会出现特定大小。 I don't know why.我不知道为什么。 If a size is set for the dialog, it comes up with that size, of course.如果为对话框设置了大小,它当然会提供该大小。 But how to make the dialog fill the whole window and take up the size of the window, whatever the window size is?但是如何让对话框填满整个 window 并占据 window 的大小,无论 window 的大小是多少? For instance, if the window size is 700, 600, then dialog should inherit that size and if the window is resized or maximized, the dialog should change its size with the window as it is being resized.例如,如果 window 的大小为 700、600,则对话框应继承该大小,如果 window 调整大小或最大化,则对话框应更改其大小,同时调整 Z05B8C74CBD96FBF2DE4C1A352702FBF4 的大小Here's the code:这是代码:

import sys
from PySide2.QtWidgets import *
from PySide2 import *


class Dialog(QDialog):
    def __init__(self):
    super(Dialog, self).__init__()
    self.move(0, 0)
    #self.resize(200, 100)
    self.setStyleSheet("background: teal;")
    self.setParent(MainWindow)


 def show_dialog():
    d = Dialog()
    d.exec_()


class MainWindow(QWidget):
    def __init__(self):
    super(MainWindow, self).__init__()
    self.resize(700, 600)

    btn = QPushButton()
    btn.setText("Click")

    layout = QVBoxLayout()
    layout.addWidget(btn)
    self.setLayout(layout)

    btn.clicked.connect(show_dialog)


if __name__ == '__main__':
   app = QApplication(sys.argv)
   MainWindow = MainWindow()
   MainWindow.show()
   sys.exit(app.exec_())

The solution is to set the size with resize() method:解决方案是使用 resize() 方法设置大小:

self.setParent(MainWindow)
self.resize(MainWindow.size())

Although I don't like the way to access the elements since it can generate errors, instead I have rewritten your solution as follows:虽然我不喜欢访问元素的方式,因为它会产生错误,但我已经重写了您的解决方案,如下所示:

import sys
from functools import partial

from PySide2.QtWidgets import QApplication, QDialog, QPushButton, QVBoxLayout, QWidget


class Dialog(QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)
        self.setStyleSheet("background: teal;")


def show_dialog(w):
    d = Dialog(w)
    d.resize(w.size())
    d.exec_()


class MainWindow(QWidget):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.resize(700, 600)
        btn = QPushButton()
        btn.setText("Click")
        layout = QVBoxLayout(self)
        layout.addWidget(btn)
        btn.clicked.connect(partial(show_dialog, self))


if __name__ == "__main__":
    app = QApplication(sys.argv)
    MainWindow = MainWindow()
    MainWindow.show()
    sys.exit(app.exec_())

Update:更新:

Since the OP clarified his question, then the solution remains the same: obtain the size of one element and set it in the other, but in this case, the size of the first must be monitored using an event filter.由于 OP 澄清了他的问题,因此解决方案保持不变:获取一个元素的大小并将其设置在另一个元素中,但在这种情况下,必须使用事件过滤器监视第一个元素的大小。

import sys
from functools import partial

from PySide2 import QtCore, QtWidgets


class Resizer(QtCore.QObject):
    sizeChanged = QtCore.Signal(QtCore.QSize)

    def __init__(self, widget):
        super(Resizer, self).__init__(widget)
        self._widget = widget
        self.widget.installEventFilter(self)

    @property
    def widget(self):
        return self._widget

    def eventFilter(self, obj, event):
        if self.widget is obj and event.type() == QtCore.QEvent.Resize:
            self.sizeChanged.emit(event.size())
        return super(Resizer, self).eventFilter(obj, event)


class Dialog(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)
        self.setStyleSheet("background: teal;")


def show_dialog(w):
    d = Dialog(w)
    d.resize(w.size())
    resizer = Resizer(w)
    resizer.sizeChanged.connect(d.resize)
    d.exec_()


class MainWindow(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.resize(700, 600)
        btn = QtWidgets.QPushButton()
        btn.setText("Click")
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(btn)
        btn.clicked.connect(partial(show_dialog, self))


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = MainWindow()
    MainWindow.show()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM