如何使用其他按钮增加QFrame.HLine行分隔符的宽度和距离?

时间:2018-06-12 20:10:10

标签: python qt button layout pyqt5

我得到的这个非常小QFrame.HLine我倾向于在帖子Separator in box layout上做:

enter image description here

但我无法弄清楚如何增加它的大小,即使它们之间的间距变大。这是我想要做的Photoshop图像:

enter image description here

我尝试设置属性setLineWidthsetMidLineWidth,但无论我设置它们的值有多大,线条都不会变大。

这是示例代码:

#! /usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import PyQt5

from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt

from PyQt5.QtWidgets import QFrame
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QGridLayout

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.create_input_text()
        self.set_window_layout()

    def setup_main_window(self):
        self.resize( 400, 300  )
        self.centralwidget = QWidget()
        self.setCentralWidget( self.centralwidget )

    def create_input_text(self):
        self.separatorLine = QFrame()
        self.separatorLine.setFrameShape( QFrame.HLine )
        self.separatorLine.setFrameShadow( QFrame.Raised )

        # self.separatorLine.setLineWidth( 150 )
        # self.separatorLine.setMidLineWidth( 150 )

        rect = self.separatorLine.frameRect()
        print( "frameShape: %s" % rect )
        print( "width: %s" % self.separatorLine.width() )
        print( "height: %s" % self.separatorLine.height() )

        self.redoButton = QPushButton( "Redo Operations" )
        self.calculate  = QPushButton( "Compute and Follow" )
        self.open       = QPushButton( "Open File" )
        self.save       = QPushButton( "Save File" )

        self.verticalGridLayout = QGridLayout()
        self.verticalGridLayout.addWidget( self.redoButton    , 1 , 0)
        self.verticalGridLayout.addWidget( self.calculate     , 2 , 0)
        self.verticalGridLayout.addWidget( self.separatorLine , 3 , 0)
        self.verticalGridLayout.addWidget( self.open          , 4 , 0)
        self.verticalGridLayout.addWidget( self.save          , 5 , 0)
        self.verticalGridLayout.setSpacing( 0 )
        self.verticalGridLayout.setAlignment(Qt.AlignTop)

        self.innerLayout = QHBoxLayout()
        self.innerLayout.addLayout( self.verticalGridLayout )

    def set_window_layout(self):
        main_vertical_layout = QVBoxLayout( self.centralwidget )
        main_vertical_layout.addLayout( self.innerLayout )

if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:1)

  

QGridLayout.setRowMinimumHeight(row,minSize)

     

将行行的最小高度设置为minSize像素。

import sys
import PyQt5

from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt

from PyQt5.QtWidgets import QFrame
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QGridLayout

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.create_input_text()
        self.set_window_layout()

    def setup_main_window(self):
        self.resize( 400, 300  )
        self.centralwidget = QWidget()
        self.setCentralWidget( self.centralwidget )

    def create_input_text(self):
        self.separatorLine = QFrame()
        self.separatorLine.setFrameShape( QFrame.HLine )
        self.separatorLine.setFrameShadow( QFrame.Raised )

        # self.separatorLine.setLineWidth( 150 )
        #self.separatorLine.setMidLineWidth( 5 )
        # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHeightForWidth(self.separatorLine.sizePolicy().hasHeightForWidth())
        self.separatorLine.setSizePolicy(sizePolicy)
        self.separatorLine.setStyleSheet("font: 9pt;")
        self.separatorLine.setLineWidth(0)
        self.separatorLine.setMidLineWidth(10)
        # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++


        rect = self.separatorLine.frameRect()
        print( "frameShape: %s" % rect )
        print( "width: %s" % self.separatorLine.width() )
        print( "height: %s" % self.separatorLine.height() )

        self.redoButton = QPushButton( "Redo Operations" )
        self.calculate  = QPushButton( "Compute and Follow" )
        self.open       = QPushButton( "Open File" )
        self.save       = QPushButton( "Save File" )

        self.verticalGridLayout = QGridLayout()
        self.verticalGridLayout.addWidget( self.redoButton    , 1 , 0)
        self.verticalGridLayout.addWidget( self.calculate     , 2 , 0)
        self.verticalGridLayout.addWidget( self.separatorLine , 3 , 0)
        self.verticalGridLayout.addWidget( self.open          , 4 , 0)
        self.verticalGridLayout.addWidget( self.save          , 5 , 0)
        self.verticalGridLayout.setSpacing( 0 )
        self.verticalGridLayout.setRowMinimumHeight(3, 40)               # +++
        self.verticalGridLayout.setAlignment(Qt.AlignTop)

        self.innerLayout = QHBoxLayout()
        self.innerLayout.addLayout( self.verticalGridLayout )

    def set_window_layout(self):
        main_vertical_layout = QVBoxLayout( self.centralwidget )
        main_vertical_layout.addLayout( self.innerLayout )

if __name__ == "__main__":
    main()

enter image description here