如何在pyqt4的QTableWidget中增加header标签的行高和行项的字体大小
how to increase the row height of the header labels and font size of the row items in QTableWidget in pyqt4
这里我想增加 header 标签的行高和单元格项目的字体大小。在我的代码中,我使用了 self.table.setRowHeight()
方法,但它不起作用。所以请告诉我他们有什么方法可以增加 header 标签的行高和单元格项目的字体大小。
下面是我的代码:
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()
如果你想增加水平的高度header你必须使用setFixedHeight()
,例如:
self.table.horizontalHeader().setFixedHeight(60)
如果你想增加单元格的字体你必须设置QTableWidget
的setFont()
方法:
fnt = self.table.font()
fnt.setPointSize(40)
self.table.setFont(fnt)
这里我想增加 header 标签的行高和单元格项目的字体大小。在我的代码中,我使用了 self.table.setRowHeight()
方法,但它不起作用。所以请告诉我他们有什么方法可以增加 header 标签的行高和单元格项目的字体大小。
下面是我的代码:
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()
如果你想增加水平的高度header你必须使用setFixedHeight()
,例如:
self.table.horizontalHeader().setFixedHeight(60)
如果你想增加单元格的字体你必须设置QTableWidget
的setFont()
方法:
fnt = self.table.font()
fnt.setPointSize(40)
self.table.setFont(fnt)