代码之家  ›  专栏  ›  技术社区  ›  Evandro Coan

如何在不创建填充按钮的情况下填充QHBoxLayout以对齐多个QGroupBox'e?

  •  0
  • Evandro Coan  · 技术社区  · 6 年前

    我得到了这个布局:

    enter image description here

    但我希望第一个文本框与第二个文本框对齐,如下所示:

    enter image description here

    但不必创建一个无用的按钮来填充空间。

    这是我想到的最简单的示例代码:

    import sys
    
    from PyQt5 import QtGui
    from PyQt5 import QtWidgets
    
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    
    def main():
        app = QtWidgets.QApplication(sys.argv)
        programWindow = ProgramWindow()
    
        programWindow.show()
        sys.exit(app.exec_())
    
    
    class ProgramWindow(QtWidgets.QMainWindow):
    
        def __init__(self):
            QtWidgets.QMainWindow.__init__(self)
            self.setup_main_window()
    
            self.first_input_text()
            self.second_input_text()
    
            self.set_window_layout()
    
        def setup_main_window(self):
            self.resize( 800, 600 )
            self.centralwidget = QWidget()
            self.setCentralWidget( self.centralwidget )
    
        def first_input_text(self):
            self.textEditWidget1 = QPlainTextEdit( self )
            self.startSimulationButton1 = QPushButton( "Start Simulation" )
            self.startSimulationButtonDumb = QPushButton( "Start Simulation fillingg" )
    
            verticalInnerLayout = QVBoxLayout()
            verticalInnerLayout.addWidget( self.startSimulationButton1 )
            verticalInnerLayout.addWidget( self.startSimulationButtonDumb )
    
            horizontalInnerLayout = QHBoxLayout()
            horizontalInnerLayout.addLayout( verticalInnerLayout )
            horizontalInnerLayout.addWidget( self.textEditWidget1 )
    
            self.groupBox1 = QGroupBox( "First Group" )
            self.groupBox1.setLayout( horizontalInnerLayout )
    
        def second_input_text(self):
            self.textEditWidget2 = QPlainTextEdit( self )
            self.startSimulationButton2 = QPushButton( "Start Simulation bigger" )
    
            verticalInnerLayout = QVBoxLayout()
            verticalInnerLayout.addWidget( self.startSimulationButton2 )
    
            horizontalInnerLayout = QHBoxLayout()
            horizontalInnerLayout.addLayout( verticalInnerLayout )
            horizontalInnerLayout.addWidget( self.textEditWidget2 )
    
            self.groupBox2 = QGroupBox( "Second Group" )
            self.groupBox2.setLayout( horizontalInnerLayout )
    
        def set_window_layout(self):
            main_vertical_layout = QVBoxLayout( self.centralwidget )
            main_vertical_layout.addWidget( self.groupBox1 )
            main_vertical_layout.addWidget( self.groupBox2 )
    
    
    if __name__ == "__main__":
        main()
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   S. Nick    6 年前

    QWidget。设置最小宽度(minw) 此属性以像素为单位保存小部件的最小宽度。

    试试看:

    import sys
    
    from PyQt5 import QtGui
    from PyQt5 import QtWidgets
    
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    
    def main():
        app = QtWidgets.QApplication(sys.argv)
        programWindow = ProgramWindow()
    
        programWindow.show()
        sys.exit(app.exec_())
    
    
    class ProgramWindow(QtWidgets.QMainWindow):
    
        def __init__(self):
            QtWidgets.QMainWindow.__init__(self)
            self.setup_main_window()
    
            self.first_input_text()
            self.second_input_text()
    
            self.set_window_layout()
    
        def setup_main_window(self):
            self.resize( 800, 600 )
            self.centralwidget = QWidget()
            self.setCentralWidget( self.centralwidget )
    
        def first_input_text(self):
            self.textEditWidget1 = QPlainTextEdit( self )
            self.startSimulationButton1 = QPushButton( "Start Simulation" )
            self.startSimulationButton1.setMinimumWidth(150) #+
            #self.startSimulationButtonDumb = QPushButton( "Start Simulation fillingg" )
    
            verticalInnerLayout = QVBoxLayout()
            verticalInnerLayout.addWidget( self.startSimulationButton1 )
            #verticalInnerLayout.addWidget( self.startSimulationButtonDumb )
    
            horizontalInnerLayout = QHBoxLayout()
            horizontalInnerLayout.addLayout( verticalInnerLayout )
            horizontalInnerLayout.addWidget( self.textEditWidget1 )
    
            self.groupBox1 = QGroupBox( "First Group" )
            self.groupBox1.setLayout( horizontalInnerLayout )
    
        def second_input_text(self):
            self.textEditWidget2 = QPlainTextEdit( self )
            self.startSimulationButton2 = QPushButton( "Start Simulation bigger" )
            self.startSimulationButton2.setMinimumWidth(150) # +
    
            verticalInnerLayout = QVBoxLayout()
            verticalInnerLayout.addWidget( self.startSimulationButton2 )
    
            horizontalInnerLayout = QHBoxLayout()
            horizontalInnerLayout.addLayout( verticalInnerLayout )
            horizontalInnerLayout.addWidget( self.textEditWidget2 )
    
            self.groupBox2 = QGroupBox( "Second Group" )
            self.groupBox2.setLayout( horizontalInnerLayout )
    
        def set_window_layout(self):
            main_vertical_layout = QVBoxLayout( self.centralwidget )
            main_vertical_layout.addWidget( self.groupBox1 )
            main_vertical_layout.addWidget( self.groupBox2 )
    
    
    if __name__ == "__main__":
        main()
    

    enter image description here

        2
  •  0
  •   Evandro Coan    6 年前

    我做到了这一点:

    enter image description here

    使用此代码:

    import sys
    
    from PyQt5 import QtGui
    from PyQt5 import QtWidgets
    
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    
    
    def main():
        app = QtWidgets.QApplication(sys.argv)
        programWindow = ProgramWindow()
    
        programWindow.show()
        sys.exit(app.exec_())
    
    
    class ProgramWindow(QtWidgets.QMainWindow):
    
        def __init__(self):
            QtWidgets.QMainWindow.__init__(self)
            self.setup_main_window()
            self.first_input_text()
            self.second_input_text()
            self.set_window_layout()
    
        def setup_main_window(self):
            self.largestFirstCollumn = 0
            self.resize( 800, 600 )
    
            self.centralwidget = QWidget()
            self.setCentralWidget( self.centralwidget )
    
        def first_input_text(self):
            self.textEditWidget1 = QPlainTextEdit( self )
            self.startSimulationButton1 = QPushButton( "Start Simulation" )
    
            self.firstVerticalInnerLayout = QVBoxLayout()
            self.firstVerticalInnerLayout.addWidget( self.startSimulationButton1 )
    
            self.firstHorizontalInnerLayout = QHBoxLayout()
            self.firstHorizontalInnerLayout.addLayout( self.firstVerticalInnerLayout )
            self.firstHorizontalInnerLayout.addWidget( self.textEditWidget1 )
    
            self.groupBox1 = QGroupBox( "First Group" )
            self.groupBox1.setLayout( self.firstHorizontalInnerLayout )
            self.get_minimum_width( self.firstVerticalInnerLayout )
    
        def second_input_text(self):
            self.textEditWidget2 = QPlainTextEdit( self )
            self.startSimulationButton2 = QPushButton( "Start Simulation bigger" )
    
            self.secondVerticalInnerLayout = QVBoxLayout()
            self.secondVerticalInnerLayout.addWidget( self.startSimulationButton2 )
    
            self.secondHorizontalInnerLayout = QHBoxLayout()
            self.secondHorizontalInnerLayout.addLayout( self.secondVerticalInnerLayout )
            self.secondHorizontalInnerLayout.addWidget( self.textEditWidget2 )
    
            self.groupBox2 = QGroupBox( "Second Group" )
            self.groupBox2.setLayout( self.secondHorizontalInnerLayout )
            self.get_minimum_width( self.secondVerticalInnerLayout )
    
        def get_minimum_width(self, target_layout):
            # https://stackoverflow.com/questions/4963220/how-to-preview-sizes-of-widgets-in-layout-before-a-show
            # How to preview sizes of widgets in layout before a show()?
            target_layout.update()
            target_layout.activate()
    
            geometry = target_layout.geometry()
            print("%sx%s" % ( geometry.width(), geometry.height() ) )
    
            if geometry.width() > self.largestFirstCollumn:
                self.largestFirstCollumn = geometry.width()
    
        def set_minimum_width(self, left_layout, right_layout):
            right_layout.update()
            right_layout.activate()
    
            geometry = right_layout.geometry()
            print( "%sx%s - %s, %s" % ( geometry.width(), geometry.height(), self.largestFirstCollumn, geometry.width() ) )
            left_layout.setSpacing( 10 + self.largestFirstCollumn - geometry.width() )
    
        def set_window_layout(self):
            main_vertical_layout = QVBoxLayout( self.centralwidget )
            main_vertical_layout.addWidget( self.groupBox1 )
            main_vertical_layout.addWidget( self.groupBox2 )
    
            self.set_minimum_width( self.firstHorizontalInnerLayout, self.firstVerticalInnerLayout)
            self.set_minimum_width( self.secondHorizontalInnerLayout, self.secondVerticalInnerLayout)
    
    
    if __name__ == "__main__":
        main()
    

    相关:

    1. Align every widget of a QHboxLayout to the top in Pyqt
    2. How to have a fixed-size layout that also keeps the window from resizing?
    3. How to align the layouts QHBoxLayout and QVBoxLayout on pyqt4?
    4. QWidget::setLayout: Attempting to set QLayout "" on ProgramWindow "", which already has a layout