Wrong layout in QMainWindow?

ⅰ亾dé卋堺 提交于 2019-12-17 20:44:10

问题


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:



来源:https://stackoverflow.com/questions/44918165/wrong-layout-in-qmainwindow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!