使用Pyside2在QT创建者中使用窗口调整主窗口大小2
就像标题所说的那样,我正在QT Creator中设计一个布局,我希望主窗口小部件能够用窗口动态调整大小。我无法实现这一目标。
我已经找到了无尽的帖子,他们提供的唯一解决方案是给中央小部件一个布局(您首先需要添加孩子)并将尺寸策略设置为扩展。我已经尝试过。
接下来,我显示两个不同的最低示例。
示例1
首先,我创建了一个新的类型“应用程序(python qt for Python)”的项目>基础类Qwidget
,“ python -window(UI文件)的QT”。
然后,我在设计器视图中添加两个文本编辑小部件。我右键单击根小部件,然后选择“布置”> “垂直布置”。最后,我将根小部件水平和垂直调整策略设置为“扩展”。这是最终的设计师窗口:
这是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
文件,我会得到以下结果:
如您所见,两个文本编辑字段可见,但在窗口中没有调整大小。
示例2
首先,我创建了一个类型“应用程序(python的QT)”的新项目> “ qt for python-窗口(UI文件)”,带有基类qmainwindow
。
然后,我在设计器视图中添加两个文本编辑小部件。我右键单击主窗口,然后选择“布线”> “垂直布置”。最后,我将中央小部件水平和垂直调整策略设置为“扩展”。这是最终的设计师窗口:
这是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”>
在这种情况下,没有什么都没有出现窗口,它是完全空的。我还尝试将窗口的尺寸策略(而不是小部件)本身设置为扩展(并设置两者),结果相同。
我正在使用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:
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:
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:
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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论