Wrong layout in PyQT5?

2019-02-20 22:42发布

问题:

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)

回答1:

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:

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)

complete code:

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)

Screenshot: