QMainWindow 中的布局错误? [英] Wrong layout in QMainWindow?

查看:64
本文介绍了QMainWindow 中的布局错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 PyQT5 中布局错误.我究竟做错了什么?是否有一些预定义的小字段大小或类似大小?我将主窗口创建为 QMainWindow,并在其中创建了一个小部件作为中央小部件.这是它的样子:

I am getting wrong layout in PyQT5. What am I doing wrong? Is there some predefined small field size or similar? I created main window as QMainWindow and inside it a widget as central widget. This is how it looks like:

class Main(QWidget):
    """The main widget with label and LineEdit"""
    def __init__(self, parent=None):
        super().__init__(parent)
        self.initUi()

    def initUi(self):
        """Initialize the UI of the main widget"""
        self.mySourceLabel = QLabel("Select your file:")
        self.mySourceLine = QLineEdit()
        self.mySourceLine.setPlaceholderText("File name here")

        # Set layout
        grid = QGridLayout()
        #grid.setSpacing(5)
        grid.addWidget(self.mySourceLabel, 0, 0)
        grid.addWidget(self.mySourceLine, 1, 0)
        self.setLayout(grid)

class MyApp(QMainWindow):
    """Main application class"""
    def __init__(self, parent=None):
        super().__init__(parent)
        self.initUi()

    def initUi(self):
        """Initialize UI of an application"""
        # main window size, title
        self.setGeometry(400, 300, 400, 300)
        self.setWindowTitle("Version upgrade ")

        # create instance of a class Main
        self.main = Main(self)

        # create central widget, create grid layout
        centralWidget = QWidget()
        centralLayout = QGridLayout()
        centralWidget.setLayout(centralLayout)

推荐答案

当您将父级传递给 QWidget 时,这将定位相对于其父级的位置并生成像您获得的一样的小部件,为了解决这个问题,布局使用时,QMainWindow 是一个特殊的 QWidget,因为它有预定义的元素,所以它已经有了布局:

When you pass the parent to a QWidget this will locate a position with respect to its parent and generate widgets like the ones you have obtained, to solve this, layouts are used, QMainWindow is a special QWidget since it has predefined elements, so it already has a layout:

在 QMainWindow 中,必须使用 setCentralWidget 函数将小部件添加到 centralwidget 中,在您的情况下:

In QMainWindow the widget must be added to the centralwidget with the setCentralWidget function, in your case:

class MyApp(QMainWindow):
    """Main application class"""
    def __init__(self, parent=None):
        super().__init__(parent)
        self.initUi()

    def initUi(self):
        [...]
        centralWidget = Main(self)
        self.setCentralWidget(centralWidget)

完整代码:

class Main(QWidget):
    """The main widget with label and LineEdit"""
    def __init__(self, parent=None):
        super().__init__(parent)
        self.initUi()

    def initUi(self):
        """Initialize the UI of the main widget"""
        self.mySourceLabel = QLabel("Select your file:")
        self.mySourceLine = QLineEdit()
        self.mySourceLine.setPlaceholderText("File name here")

        # Set layout
        grid = QGridLayout()
        #grid.setSpacing(5)
        grid.addWidget(self.mySourceLabel, 0, 0)
        grid.addWidget(self.mySourceLine, 1, 0)
        self.setLayout(grid)

class MyApp(QMainWindow):
    """Main application class"""
    def __init__(self, parent=None):
        super().__init__(parent)
        self.initUi()

    def initUi(self):
        """Initialize UI of an application"""
        # main window size, title
        self.setGeometry(400, 300, 400, 300)
        self.setWindowTitle("Version upgrade ")

        # create central widget, create grid layout
        centralWidget = Main(self)
        self.setCentralWidget(centralWidget)

截图:

这篇关于QMainWindow 中的布局错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆