删除滚动条以显示完整表格 [英] Remove scrollbar to show full table

查看:52
本文介绍了删除滚动条以显示完整表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a scrollview to which I dynamically add QTableWidgets. However, the QTables themselves also have scrollbars and therefore don't show the full table. Is there a way to disable the scroll bar so that the table always gets shown in full?

我添加了

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

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

As suggested. The scroll bar does disappear, but it still only shows the partial tables (Ican scroll with hovering iverthe table and using the mouse wheel, still). The code for the Widget is below

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)

推荐答案

如果你只是想删除滚动条,你必须使用:

If you just want to delete the Scrollbar you must use:

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

如果你想显示扩展的QTableWidget,在setData()方法的末尾添加:

If you want to show the expanded QTableWidget, add this to the end of the setData() method:

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)

注意:函数 getQTableWidgetSize 是以下帖子的 C++ 代码到 python 的转换:如何确定一个QTableWidget的正确大小?

Note: The function getQTableWidgetSize is a conversion of the code in C ++ to python of the following post: How to determine the correct size of a QTableWidget?

这篇关于删除滚动条以显示完整表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆