我正在寻找一种方法来挤出QTableWidget
细胞中所有可能的空间。我粘贴了一些代码来演示我在下面尝试过的事情,并且我上传了在 OSX 10.8.5、Qt 4.8.4、PySide 1.2.1 上生成的屏幕截图。我想要的是能够设置为valueIWantToBeZero = 0
使单元格恰好是单个字符的大小,并且我可以将尽可能多的列打包到可用空间中。如果我在此示例中设置,则文本会从单元格的右侧消失。每个单元格的左侧似乎有大约 5 个像素的填充,我无法摆脱。顶部似乎也有 1 个像素,底部有 2 个像素,但我不太关心垂直间距。valueIWantToBeZero = 0
如何使单元格恰好是一个字符的宽度?
感谢您的任何建议!:-)
#! /usr/bin/env python
import sys
from PySide import QtCore, QtGui
class TableWidget(QtGui.QTableWidget):
def __init__(self, parent):
super(TableWidget, self).__init__(parent)
font = QtGui.QFont("Courier",12,QtGui.QFont.Normal)
font.setKerning(False)
font.setLetterSpacing(QtGui.QFont.PercentageSpacing,0.0)
font.setHintingPreference(QtGui.QFont.PreferNoHinting)
font.setWordSpacing(0.0)
self.setFont(font)
self.setStyleSheet("QHeaderView::section:horizontal {margin: 0px; border: 0; padding 0px}")
self.setStyleSheet("QTableView::item {padding: 0px; margin: 0px; border: 0; background-color: orange; }")
valueIWantToBeZero = 5
hheader = self.horizontalHeader()
hheader.setFont(font)
hheader.setDefaultSectionSize(hheader.fontMetrics().width("A")+valueIWantToBeZero)
vheader = self.verticalHeader()
vheader.setDefaultSectionSize(vheader.fontMetrics().height())
dim = 10
self.setColumnCount(dim)
self.setRowCount(dim)
for r in range(dim):
for c in range(dim):
item = QtGui.QTableWidgetItem(str(r))
self.setItem(r,c,item)
def main():
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.setGeometry(100,100,200,200)
tw = TableWidget(window)
twLayout = QtGui.QVBoxLayout()
twLayout.addWidget(tw)
window.setLayout(twLayout)
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()