简体   繁体   中英

In PyQt5 right widget is shorter than the rest

I am doing a small application with GUI in Python using PyQt5 but for some reason my right widget appears to be shorter than the rest

As you can see QGridLayout is a little bit shorter. I tried other layouts but it's all the same. This really bothers me. Anybody know the reason?

Here is my code. I post the simplified version for convenience but nonetheless it illustrates my problem.

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()

        self._x = 300
        self._y = 300
        self._width = 1000
        self._height = 600

        self.init_ui()

    def init_ui(self):
        self.init_main_layout()

        self.setGeometry(self._x, self._y, self._width, self._height)
        self.show()

    def init_main_layout(self):
        # central widget
        self.main_splitter = QSplitter(Qt.Horizontal)

        # canvas and tabwidget in the middle
        figure = plt.figure()
        canvas = FigureCanvas(figure)
        plt_toolbar = NavigationToolbar(canvas, self)

        mid_splitter = QSplitter(Qt.Vertical)
        layout = QVBoxLayout()
        layout.addWidget(plt_toolbar)
        layout.addWidget(canvas)

        pl = QWidget()
        pl.setLayout(layout)
        mid_splitter.addWidget(pl)

        tabs = QTabWidget()
        tab1 = QWidget()

        formGroupBox = QGroupBox("")
        layout = QFormLayout()
        formGroupBox.setLayout(layout)
        hor_layout = QHBoxLayout()
        hor_layout.addWidget(formGroupBox)

        tab1.setLayout(hor_layout)
        mid_splitter.addWidget(tabs)

        # the right one
        right_part = QGridLayout()
        right_part.addWidget(QLabel('Text'), 1, 0)
        right_part.addWidget(QComboBox(), 1, 1, 1, 5)
        right_part.addWidget(QPushButton(), 1, 6)

        tree = QFileSystemModel()
        tree_widget = QTreeView()
        tree_widget.setModel(tree)

        right_part.addWidget(tree_widget, 2, 0, -1, -1)
        # TODO right part is shorter than the rest
        right_part_widget = QWidget()
        right_part_widget.setLayout(right_part)

        self.main_splitter.addWidget(QListWidget())
        self.main_splitter.addWidget(mid_splitter)
        self.main_splitter.addWidget(right_part_widget)
        self.main_splitter.setSizes([self._width * 0.2, self._width * 0.4, self._width * 0.4])
        self.setCentralWidget(self.main_splitter)
        self.statusBar().showMessage('Test')


def main(args):
    app = QtWidgets.QApplication(args)
    main_window = MainWindow()

    sys.exit(app.exec_())


if __name__ == '__main__':
    import sys

    main(sys.argv)

right_part.setContentsMargins(0,0,0,0)按照Heike的建议为我工作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM