如何在QT Designer上扩展主要UI元素?

问题描述 投票:0回答:1

我正在通过 QT Designer 测试 python 应用程序的 GUI,到目前为止,它看起来像我想要的样子:左侧和右侧的一列中组织的四个按钮,一个带有五个的地图其上的单选按钮。 Here's a look of the main window.

但是,当我预览窗口并拉伸/最大化它时,UI 元素没有展开。相反,它们都保留在窗口的左上角。有办法解决这个问题吗?

建议我通过在一个水平布局中插入两个垂直布局来使用布局,尽管由于某种原因,我无法将单选按钮粘贴在地图图像内。更不用说元素都被隔开了。不确定是否可以通过更改 python 中的代码来替代此操作。这是 objects from the Main Window 的外观,以防万一我用错了它们。

python pyqt pyqt5 qt-designer
1个回答
0
投票

以下是如何使用 Python 代码实现类似布局的基本示例:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QHBoxLayout, QWidget, QPushButton, QRadioButton, QLabel

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Resizable UI Example")
        
        main_widget = QWidget()
        self.setCentralWidget(main_widget)
        
        main_layout = QHBoxLayout()
        main_widget.setLayout(main_layout)
        
        # Left side: push buttons
        left_layout = QVBoxLayout()
        main_layout.addLayout(left_layout)
        
        for i in range(4):
            button = QPushButton(f"Button {i+1}")
            left_layout.addWidget(button)
        
        # Right side: map and radio buttons
        right_layout = QVBoxLayout()
        main_layout.addLayout(right_layout)
        
        map_label = QLabel("Map Widget")
        right_layout.addWidget(map_label)
        
        for i in range(5):
            radio_button = QRadioButton(f"Option {i+1}")
            right_layout.addWidget(radio_button)

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

在这段代码中,我们创建了一个水平布局的主窗口。在布局内部,我们为窗口的左侧和右侧添加两个垂直布局。在左侧布局中,我们添加按钮,在右侧布局中,我们添加标签(代表地图)和单选按钮。当窗口大小调整时,此布局将自动调整和扩展小部件。

© www.soinside.com 2019 - 2024. All rights reserved.