how to increase the row height of the header labels and font size of the row items in QTableWidget in pyqt4

南楼画角 提交于 2020-01-05 23:48:13

问题


Here i want to increase the row height of the headerlabel and font size of the cell items. In my code I am using self.table.setRowHeight() method, but its not working. So please tell me is their any method to increase the row height of the header labels and font size of cell items.

given bellow is my code:

import sys
from PyQt4 import QtGui, QtCore


ROUNDED_STYLE_SHEET1 = """QPushButton {
     background-color: green;
     color: white;
     border-style: outset;
     border-width: 4px;
     border-radius: 15px;
     border-color: none;
     font: bold 12px;
     min-width: 10em;
     padding: 10px;
 }
"""

OVAL = """QPushButton {

      position: relative;
      width: 50px;
      height: 30px;
      margin: 20px 0;
      background: blue;
      border-radius: 48% / 25%;
      color: white;
      font: bold 10px;
}
"""

class Example(QtGui.QWidget):

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

        self.initUI()

    def initUI(self):
      self.label = QtGui.QLabel("Invoice Serial Number-39",self)
      self.label.setStyleSheet("font: bold 20pt AGENTORANGE")
      self.label.move(10,20)


      self.line_edit = QtGui.QLineEdit(self)
      self.line_edit.move(900,15)
      self.line_edit.resize(300,30)
      self.btn1 = QtGui.QPushButton("connect",self)
      self.btn1.setStyleSheet(ROUNDED_STYLE_SHEET1)
      self.btn1.move(1200,10)
      self.btn2 =QtGui.QPushButton("back", self)
      self.btn2.move(70,200)
      self.btn3 = QtGui.QPushButton("Reset Form", self)
      self.btn3.move(140,200)
      self.btn3.setStyleSheet("background-color:brown")
      self.btn4 = QtGui.QPushButton("pay", self) 
      self.btn4.setStyleSheet(OVAL)
      self.btn4.move(1200,170)

      self. table = QtGui.QTableWidget(self)
      self.table.move(10,70)
      self.table.resize(1350,100)
      self.table_item = QtGui.QTableWidgetItem()
      self.table.setRowCount(1)
      self.table.setColumnCount(6)
      self.tbutton = QtGui.QToolButton()
      self.tbutton.setToolTip("delete")
      self.tbutton.setIcon(QtGui.QIcon("trash1.png"))
      self.tbutton.setCheckable(True) 
      self.qspinbox = QtGui.QSpinBox()
      self.qspinbox.setMinimum(1)
      self.qspinbox.setMaximum(50)
      self.qspinbox.setRange(1,50)
      self.qspinbox.setMinimum(1)

      self.table.setHorizontalHeaderLabels(("S.no, Item Description,Qty,Rate(Rs:),Subtotal,"",").split(','))
      self.table.setVerticalHeaderLabels(("1").split(','))

      self.table.setItem(0,0,QtGui.QTableWidgetItem("1"))
      self.table.setItem(0,1, QtGui.QTableWidgetItem("Acne-aid Wash Facial Cleansing"))
      self.table.setItem(0,2,QtGui.QTableWidgetItem("1"))
      self.table.setItem(0,3,QtGui.QTableWidgetItem("191.72"))
      self.table.setItem(0,4,QtGui.QTableWidgetItem("191.72"))
      self.table.setItem(0,5,QtGui.QTableWidgetItem(""))
      self.table.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
      self.table.setCellWidget(0,5,self.tbutton)
      self.table.setCellWidget(0,2,self.qspinbox)
      self.table.horizontalHeader().setStyleSheet("QHeaderView { font-size:  18pt};")
      self.table.horizontalHeader().setStyleSheet("::section {background-color : lightGray;font-size:10pt;}")
      self.table.setRowHeight(0,100)
      self.table.horizontalHeader().setStretchLastSection(True)
      self.table.setStyleSheet("QTableWidget{ width:100%; height: 100px; }")




      self.table.setColumnWidth(0,100)
      self.table.resizeRowsToContents()

      self.table.setColumnWidth(1,250)
      self.table.setColumnWidth(2,300)
      self.table.setColumnWidth(3,250)
      self.table.setColumnWidth(4,250)
      self.table.setColumnWidth(5,200)





      self.btn4.clicked.connect(self.on_pushButton_clicked)
      # self.dialog = Example1()


      self.setWindowTitle("business management")
      self.setGeometry(200,300,900,600)
      self.showMaximized()

    def on_pushButton_clicked(self):
        self.dialog.showMaximized() 


def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
    ex.setStyleSheet("background-color:white") 


if __name__ == '__main__':
    main()

回答1:


If you want to increase the height of the horizontal header you must use setFixedHeight(), for example:

self.table.horizontalHeader().setFixedHeight(60)

If you want to increase the font of the cells you must set the setFont() method of the QTableWidget:

fnt = self.table.font()
fnt.setPointSize(40)
self.table.setFont(fnt)


来源:https://stackoverflow.com/questions/52089202/how-to-increase-the-row-height-of-the-header-labels-and-font-size-of-the-row-ite

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