pyqt QTablewidget删除滚动条以显示完整的表格

时间:2017-01-09 07:26:25

标签: python pyqt pyside qtablewidget

我有一个滚动视图,我动态添加QTableWidgets。但是,QTables本身也有滚动条,因此不显示完整的表格。有没有办法禁用滚动条,以便表格始终显示完整?

enter image description here

编辑:我添加了

    self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
    self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

建议。滚动条确实消失了,但它仍然只显示部分表格(我可以滚动悬停iverthe表并使用鼠标滚轮,仍然)。 Widget的代码在

之下
from PySide.QtGui import *
from PySide.QtCore import *

class MdTable(QTableWidget):
    def __init__(self, data, depth, *args):

        QTableWidget.__init__(self, *args)
        self.hheaders = ["c1", "c2", "c3", "c4"]
        self.depth = depth
        self.bids = data
        self.setData()

    def setData(self):

        self.setRowCount(self.depth)
        self.setColumnCount(5)

        for i in xrange(self.depth):
            if len(self.data) > i:
                d1= QTableWidgetItem(str(self.data[i][0]))
                d2= QTableWidgetItem(str(self.data[i][1]))
                self.setItem(i, 1, d1)
                self.setItem(i, 2, d2)

        self.setHorizontalHeaderLabels(self.hheaders)
        self.verticalHeader().setVisible(False)
        self.resizeRowsToContents()
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

1 个答案:

答案 0 :(得分:2)

如果您只想删除滚动条,则必须使用:

{QtableWidget}.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
{QtableWidget}.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)

如果您想展示展开的QTableWidget,请将其添加到setData()方法的末尾:

self.setMaximumSize(self.getQTableWidgetSize())
self.setMinimumSize(self.getQTableWidgetSize())

并像这样定义getQTableWidgetSize(self)

def getQTableWidgetSize(self):
    w = self.verticalHeader().width() + 4  # +4 seems to be needed
    for i in range(self.columnCount()):
        w += self.columnWidth(i)  # seems to include gridline (on my machine)
    h = self.horizontalHeader().height() + 4
    for i in range(self.rowCount()):
        h += self.rowHeight(i)
    return QtCore.QSize(w, h)

enter image description here

注意: 函数getQTableWidgetSize是将以下帖子中的C ++代码转换为python:How to determine the correct size of a QTableWidget?