使用Pyside2在QT创建者中使用窗口调整主窗口大小2

发布于 2025-01-27 10:54:53 字数 3193 浏览 2 评论 0原文

就像标题所说的那样,我正在QT Creator中设计一个布局,我希望主窗口小部件能够用窗口动态调整大小。我无法实现这一目标。

我已经找到了无尽的帖子,他们提供的唯一解决方案是给中央小部件一个布局(您首先需要添加孩子)并将尺寸策略设置为扩展。我已经尝试过。

接下来,我显示两个不同的最低示例。

示例1

首先,我创建了一个新的类型“应用程序(python qt for Python)”的项目>基础类Qwidget,“ python -window(UI文件)的QT”。

然后,我在设计器视图中添加两个文本编辑小部件。我右键单击根小部件,然后选择“布置”> “垂直布置”。最后,我将根小部件水平和垂直调整策略设置为“扩展”。这是最终的设计师窗口:

”由此产生的设计器窗口为例1,显示一个带有两个文本编辑小部件的root小部件,垂直放置,大小策略扩展”

这是widget.py.py 代码>文件:

# This Python file uses the following encoding: utf-8
import os
from pathlib import Path
import sys

from PySide2.QtWidgets import QApplication, QWidget
from PySide2.QtCore import QFile
from PySide2.QtUiTools import QUiLoader


class Widget(QWidget):
    def __init__(self):
        super(Widget, self).__init__()
        self.load_ui()

    def load_ui(self):
        loader = QUiLoader()
        path = os.fspath(Path(__file__).resolve().parent / "form.ui")
        ui_file = QFile(path)
        ui_file.open(QFile.ReadOnly)
        loader.load(ui_file, self)
        ui_file.close()


if __name__ == "__main__":
    app = QApplication([])
    widget = Widget()
    widget.show()
    sys.exit(app.exec_())

如果我运行widget.py文件,我会得到以下结果:

”示例1:窗口显示两个文本输入,但它们不与窗口调整大小”

如您所见,两个文本编辑字段可见,但在窗口中没有调整大小。

示例2

首先,我创建了一个类型“应用程序(python的QT)”的新项目> “ qt for python-窗口(UI文件)”,带有基类qmainwindow

然后,我在设计器视图中添加两个文本编辑小部件。我右键单击主窗口,然后选择“布线”> “垂直布置”。最后,我将中央小部件水平和垂直调整策略设置为“扩展”。这是最终的设计师窗口:

”示例2的生成设计师窗口,显示一个带有两个文本编辑小部件的中央小部件,垂直放置,尺寸策略扩展”

这是main> main> mainwindow 。

# This Python file uses the following encoding: utf-8
import os
from pathlib import Path
import sys

from PySide2.QtWidgets import QApplication, QMainWindow
from PySide2.QtCore import QFile
from PySide2.QtUiTools import QUiLoader


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.load_ui()

    def load_ui(self):
        loader = QUiLoader()
        path = os.fspath(Path(__file__).resolve().parent / "form.ui")
        ui_file = QFile(path)
        ui_file.open(QFile.ReadOnly)
        loader.load(ui_file, self)
        ui_file.close()


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

​=“ nofollow noreferrer”> “示例2:窗口为空”

在这种情况下,没有什么都没有出现窗口,它是完全空的。我还尝试将窗口的尺寸策略(而不是小部件)本身设置为扩展(并设置两者),结果相同。

我正在使用Python 3.10,Pyside 2,QT 5.15.3和QT Creator 7.0.0

Like the title says, I'm designing a layout in Qt Creator and I want the main widget to resize dynamically with the window. I am not able to achieve this.

I have already found endless posts where the only solution they give is giving the central widget a layout (for which you first need to add a child) and setting the size policy to Expanding. I have already tried this.

Next I show two different minimum examples.

Example 1

First, I create a new project of type "Application (Qt for Python)" > "Qt for Python - Window (UI file)", with base class QWidget.

I then add two Text Edit widgets in the Designer view. I right click the root widget and choose "Lay out" > "Lay out vertically". Finally, I set the root widget horizontal and vertical resize policy to "Expanding". This is the resulting Designer window:

The resulting Designer window for example 1, showing a root widget with two text edit widgets as children, laid out vertically, with size policy Expanding

This is the widget.py file:

# This Python file uses the following encoding: utf-8
import os
from pathlib import Path
import sys

from PySide2.QtWidgets import QApplication, QWidget
from PySide2.QtCore import QFile
from PySide2.QtUiTools import QUiLoader


class Widget(QWidget):
    def __init__(self):
        super(Widget, self).__init__()
        self.load_ui()

    def load_ui(self):
        loader = QUiLoader()
        path = os.fspath(Path(__file__).resolve().parent / "form.ui")
        ui_file = QFile(path)
        ui_file.open(QFile.ReadOnly)
        loader.load(ui_file, self)
        ui_file.close()


if __name__ == "__main__":
    app = QApplication([])
    widget = Widget()
    widget.show()
    sys.exit(app.exec_())

If I run the widget.py file, I get the following result:

Result for example 1: the window show the two text inputs but they do not resize with the window

As you can see, the two text edit fields are visible but they do not resize with the window.

Example 2

First, I create a new project of type "Application (Qt for Python)" > "Qt for Python - Window (UI file)", with base class QMainWindow.

I then add two Text Edit widgets in the Designer view. I right click the main window and choose "Lay out" > "Lay out vertically". Finally, I set the central widget horizontal and vertical resize policy to "Expanding". This is the resulting Designer window:

The resulting Designer window for example 2, showing a main window with a central widget with two text edit widgets as children, laid out vertically, with size policy Expanding

This is the mainwindow.py file:

# This Python file uses the following encoding: utf-8
import os
from pathlib import Path
import sys

from PySide2.QtWidgets import QApplication, QMainWindow
from PySide2.QtCore import QFile
from PySide2.QtUiTools import QUiLoader


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.load_ui()

    def load_ui(self):
        loader = QUiLoader()
        path = os.fspath(Path(__file__).resolve().parent / "form.ui")
        ui_file = QFile(path)
        ui_file.open(QFile.ReadOnly)
        loader.load(ui_file, self)
        ui_file.close()


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

If I run the mainwindow.py file, I get the following result:

Result for example 2: the window is empty

In this case nothing appears in the window, it is completely empty. I have also tried setting the size policy of the window (instead of the widget) itself to Expanding (and also setting both), with the same result.

I'm using Python 3.10, PySide 2, Qt 5.15.3 and Qt Creator 7.0.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文