我想要一个有2个'标题'行的QTableWidget.基本上我想让表格的前2行不垂直滚动.例如:
Header 1 | Header 2
__________________
Header 3 | Header 4
__________________
Data | Data
__________________
Data | Data
__________________
Data | Data
__________________
Data | Data
__________________
Run Code Online (Sandbox Code Playgroud)
当用户向下滚动时,我想阻止4个标题(前两行)中的任何一个不滚动离开屏幕.
我在Qt中没有看到添加额外的标题行或阻止单行的滚动.也许有一个棘手的方法来实现这一点,通过有2个实际表和其中一个表有一行是一个标题?
我找到了一种方法来做到这一点,虽然有点模糊.我确实找到了一个很好的例子,说明如何做类似的事情,但是使用列而不是标题行.
http://doc.qt.nokia.com/4.7-snapshot/itemviews-frozencolumn.html
我创建了两个表,一个用于标题行,另一个用于数据.然后,我隐藏了数据表的水平标题,将所有内容的边距/间距设置为0.这样可以将表格压得足够紧密,看起来像一张桌子.
确保隐藏每个表的水平滚动条,然后添加连接两个隐藏滚动条的新滚动条.因此,当用户使用独立滚动条滚动时,它会触发"真实"隐藏滚动条上的事件.这样,用户只能使用一个滚动条进行交互.
我还必须捕获来自QHeaderView类的所有信号,并确保同时将信号请求的更改应用于两个表.
最重要的部分是确保垂直标题项的宽度在两个表中都是相同的长度.垂直标题项的宽度在名为resizeEvent的事件上设置,http: //www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qwidget.html#resizeEvent .所以我必须在我的类中重写此方法,以将两个表上的标题设置为相同的宽度.
码:
def resize(self):
"""
Called when we know the data table has been setup by Qt so we are
guaranteed that the headers now have a width, etc.
There is no other way to guarantee that your elements have been sized,
etc. by Qt other than this event.
"""
# Make the width of the vertical headers on the header table the same
# size as the initialized width of the data table (data table widths
# are setup automatically to fit the content)
width = self._data_table.verticalHeader().width()
self._header_table.verticalHeader().setFixedWidth(width)
Run Code Online (Sandbox Code Playgroud)
码:
def selectAll(self):
"""Select all data in both tables"""
for table in [self._header_table, self._data_table]:
for row in xrange(table.rowCount()):
for col in xrange(table.columnCount()):
item = table.item(row, col)
table.setItemSelected(item, True)
Run Code Online (Sandbox Code Playgroud)