qtreewidget不需要的空格(其他不可移动的行)

时间:2015-11-22 15:06:36

标签: python qt scroll pyqt qtreewidget

我正在创建一个自定义qtreewidget类,它自动调整其窗口以完全适合当前可见的元素(我不想滚动)。为此,我运行一个count函数来查找打开的qtreewidgetitems及其子节点的数量,并从那里设置一个固定的高度。但是,当我展开子窗口小部件(单击其中一个项目上的展开箭头)时,整个视图突然需要滚动,因为底部有额外的空白区域,尽管我的计数功能可以精确计算所需的高度。我该如何摆脱它?

下面是一个可以直接运行的工作类。

import sys
from PyQt4 import QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *


class TreeWidget(QTreeWidget):
    def __init__(self, parent=None):
        super(TreeWidget, self).__init__()
        self.installEventFilter(self)
        self.setStyleSheet('''
            background: None;
            border: None;
            outline: None;
            outline-width: 0px;
            selection-background-color: blue;
            ''')
        header = QtGui.QTreeWidgetItem(["Tree", "First"])
        self.setAutoScroll(False)
        self.setHeaderItem(header)
        self.header().close()
        # self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

    def fill_item(self, item, value):
        item.setExpanded(False)
        if type(value) is dict:
            for key, val in sorted(value.iteritems()):
                child = QTreeWidgetItem()
                child.setText(0, unicode(key))
                # child.doubleClicked.connect(lambda: self.doubleClicked1(child))
                item.addChild(child)
                self.fill_item(child, val)
        elif type(value) is list:
            last = None
            for val in value:
                child = QTreeWidgetItem()
                if type(val) is dict:
                    item.addChild(child)
                    child.setText(0, '[dict]')
                    # child.doubleClicked.connect(lambda: self.doubleClicked1(child))
                    self.fill_item(child, val)
                    last = child
                elif type(val) is list:
                    self.fill_item(last, val)
                else:
                    item.addChild(child)
                    child.setText(0, unicode(val))
                    child.setText(1, 'test')
                    # child.doubleClicked.connect(lambda: self.doubleClicked1(child))
                    child.setExpanded(False)
                    last = child
        else:
            child = QTreeWidgetItem()
            child.setText(0, unicode(val))
            child.setText(1, 'test')
            # child.doubleClicked.connect(lambda: self.doubleClicked1(child))
            item.addChild(child)

    def fill_widget(self, value):
        self.clear()
        self.fill_item(self.invisibleRootItem(), value)

    def resizeEvent(self, event):
        self.resize()

    def resize(self):
        width = 50
        self.header().resizeSection(1, width)
        self.header().resizeSection(0, self.width()-width)
        height = self.visibleCount()
        print height/15
        self.setFixedHeight(height+0)

    def eventFilter(self, source, event):
        if source is self:
            if event.type() == 1:
                self.resize()
            elif event.type() == QEvent.Leave:
                self.clearSelection()
        return QtGui.QTreeWidget.eventFilter(self, source, event)

    def visibleCount(self, parent=0):
        height = 0
        if parent == 0:
            topHeight = 0
            for a in xrange(self.topLevelItemCount()):
                item = self.topLevelItem(a)
                topHeight += self.visualItemRect(item).height()
                if item.isExpanded():
                    height += self.visibleCount(item)
            height += topHeight
        else:
            childHeight = 0
            for a in xrange(parent.childCount()):
                item = parent.child(a)
                childHeight += self.visualItemRect(item).height()
                if item.isExpanded():
                    height += self.visibleCount(item)
            height += childHeight
        return height

    def editClicked(self, parent=0):
        # print 'edit 2'
        if parent == 0:
            for a in xrange(self.topLevelItemCount()):
                item = self.topLevelItem(a)
                print type(item)
                item.setExpanded(True)
                self.editClicked(item)
        else:
            for a in xrange(parent.childCount()):
                item = parent.child(a)
                print type(item)
                item.setText(1, '+')
                item.setExpanded(True)
                self.editClicked(item)

    def doubleClicked1(self, widget):
        print widget


def main():
    app = QtGui.QApplication(sys.argv)
    ex = TreeWidget()
    data = [
        'Make sure ZMQ remote button gets honored',
        'Fill in plot',
        'Remove cycle',
        'Remove current control or make it working',
        'Handle possible startup errors with dialogs',
        'Get improved current read-out (requires hardware changes)',
        ['1','2','3'],
        'Email quench notification'
        ]
    ex.fill_widget(data)
    ex.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main() 

1 个答案:

答案 0 :(得分:1)

您可以完全删除事件过滤器。在展开或折叠项目时,您需要调整窗口小部件的大小。这两个动作都会触发信号,因此您只需将这些信号连接到resize

即可
class TreeWidget(QTreeWidget):
    def __init__(self, parent=None):
        super(TreeWidget, self).__init__(parent)
        self.expanded.connect(self.resize)
        self.collapsed.connect(self.resize)