Qt - QTreeView WordWrap 不工作

发布于 2024-10-06 00:40:21 字数 1411 浏览 4 评论 0原文

我一直在一些论坛上阅读有关 WordWrap 不适用于 QTreeView 的内容(如文本显示在屏幕外),但我找不到任何解决此问题的“黑客”。 bookTreeView 封装在另一个小部件中,这可能是问题所在......或者只是不支持?

    bookTreeView->setModel(standardModel);
    bookTreeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    bookTreeView->setWordWrap(true);
    bookTreeView->sizeHint();
    bookTreeView->setTextElideMode(Qt::ElideNone);
    bookTreeView->setExpandsOnDoubleClick(true);
    bookTreeView->setUniformRowHeights(true);
    bookTreeView->setHeaderHidden(true);
    bookTreeView->setStyleSheet("QTreeView { font-size: 27px; show-decoration-selected: 0; } QTreeView::branch:has-siblings:!adjoins-item { border-image: none; } QTreeView::branch:has-siblings:adjoins-item { border-image: none; } QTreeView::branch:!has-children:!has-siblings:adjoins-item { border-image: none;} QTreeView::branch:has-children:!has-siblings:closed, QTreeView::branch:closed:has-children:has-siblings { border-image: none; image: url(':images/images/right_arrow.png'); } QTreeView::branch:open:has-children:!has-siblings, QTreeView::branch:open:has-children:has-siblings  { border-image: none; image: url(':images/images/down_arrow.png'); } ");

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(someWidget);
    layout->addWidget(bookTreeView);

    QWidget *page = new QWidget;
    page->setLayout(layout);

    return page;

I have been reading in some of the forums about WordWrap not working for QTreeView (as in the text displays off screen), but I couldn't find any "hack" that fixed this. The bookTreeView is encapsulated inside another widget, this may be the problem...or it's just not supported?

    bookTreeView->setModel(standardModel);
    bookTreeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    bookTreeView->setWordWrap(true);
    bookTreeView->sizeHint();
    bookTreeView->setTextElideMode(Qt::ElideNone);
    bookTreeView->setExpandsOnDoubleClick(true);
    bookTreeView->setUniformRowHeights(true);
    bookTreeView->setHeaderHidden(true);
    bookTreeView->setStyleSheet("QTreeView { font-size: 27px; show-decoration-selected: 0; } QTreeView::branch:has-siblings:!adjoins-item { border-image: none; } QTreeView::branch:has-siblings:adjoins-item { border-image: none; } QTreeView::branch:!has-children:!has-siblings:adjoins-item { border-image: none;} QTreeView::branch:has-children:!has-siblings:closed, QTreeView::branch:closed:has-children:has-siblings { border-image: none; image: url(':images/images/right_arrow.png'); } QTreeView::branch:open:has-children:!has-siblings, QTreeView::branch:open:has-children:has-siblings  { border-image: none; image: url(':images/images/down_arrow.png'); } ");

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(someWidget);
    layout->addWidget(bookTreeView);

    QWidget *page = new QWidget;
    page->setLayout(layout);

    return page;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光瘦了 2024-10-13 00:40:21

我唯一知道的 hack - 它是 Tree Item 中的 QLable。

Only hack I know - it's QLable inside Tree Item.

无尽的现实 2024-10-13 00:40:21

正如评论者所建议的,您可以使用自定义委托来执行此操作。不幸的是, QTreeView.setWordWrap(True) 似乎没有效果(至少在 Qt 4.8 中)。为了回答另一个问题,我们使用 QTreeView 的自定义 QStyledItemDelegate 实现了自动换行功能:

在 QTreeView (Qt/PySide/PyQt) 中实现自动换行委托?

这只是一种方法,我确信还有其他好方法,并注意这个例子非常基本,没有编辑器或类似的东西......

import sys
from PySide import QtGui, QtCore

class SimpleTree(QtGui.QTreeView):
    def __init__(self, parent = None):    
        QtGui.QTreeView.__init__(self, parent)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.setGeometry(500,200, 400, 300)  
        self.setUniformRowHeights(False) #optimize: but for word wrap, we don't want this!
        self.model = QtGui.QStandardItemModel()
        self.model.setHorizontalHeaderLabels(['Task', 'Description'])
        self.setModel(self.model)
        self.rootItem = self.model.invisibleRootItem()
        item0 = [QtGui.QStandardItem('Sneeze'), QtGui.QStandardItem('You have been blocked up')]
        item00 = [QtGui.QStandardItem('Tickle nose, this is a very long entry. Row should resize.'), QtGui.QStandardItem('Key first step')]
        item1 = [QtGui.QStandardItem('<b>Get a job</b>'), QtGui.QStandardItem('Do not blow it')]
        item2  = [QtGui.QStandardItem("Now let's see how this one works. It is medium."), QtGui.QStandardItem('Do not blow it')]       
        self.rootItem.appendRow(item0)
        item0[0].appendRow(item00) 
        self.rootItem.appendRow(item1)
        self.rootItem.appendRow(item2)
        self.setColumnWidth(0,150)
        self.expandAll()
        self.setItemDelegate(ItemWordWrap(self))

class ItemWordWrap(QtGui.QStyledItemDelegate):
    def __init__(self, parent=None):
        QtGui.QStyledItemDelegate.__init__(self, parent)
        self.parent = parent
        
    def paint(self, painter, option, index):
        text = index.model().data(index) 
        document = QtGui.QTextDocument() # #print "dir(document)", dir(document)
        document.setHtml(text)       
        document.setTextWidth(option.rect.width())  #keeps text from spilling over into adjacent rect
        index.model().setData(index, option.rect.width(), QtCore.Qt.UserRole+1)
        painter.save() 
        painter.translate(option.rect.x(), option.rect.y()) 
        document.drawContents(painter)  #draw the document with the painter
        painter.restore()
        
    def sizeHint(self, option, index):
        #Size should depend on number of lines wrapped
        text = index.model().data(index)
        document = QtGui.QTextDocument()
        document.setHtml(text) 
        width = index.model().data(index, QtCore.Qt.UserRole+1)
        if not width:
            width = 20
        document.setTextWidth(width) 
        return QtCore.QSize(document.idealWidth() + 10,  document.size().height())       

def main():
    app = QtGui.QApplication(sys.argv)
    myTree = SimpleTree()
    myTree.show()
    sys.exit(app.exec_())
    
if __name__ == "__main__":
    main()

As suggested by a commenter, you can do this using a custom delegate. Unfortunately, QTreeView.setWordWrap(True) seems to have no effect (at least in Qt 4.8). In response to another question, we implemented word wrap functionality using a custom QStyledItemDelegate for a QTreeView:

Implementing a delegate for wordwrap in a QTreeView (Qt/PySide/PyQt)?

This is just one way to do it, there are other good ways I'm sure, and note this example is very basic, no editors or anything like that...

import sys
from PySide import QtGui, QtCore

class SimpleTree(QtGui.QTreeView):
    def __init__(self, parent = None):    
        QtGui.QTreeView.__init__(self, parent)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.setGeometry(500,200, 400, 300)  
        self.setUniformRowHeights(False) #optimize: but for word wrap, we don't want this!
        self.model = QtGui.QStandardItemModel()
        self.model.setHorizontalHeaderLabels(['Task', 'Description'])
        self.setModel(self.model)
        self.rootItem = self.model.invisibleRootItem()
        item0 = [QtGui.QStandardItem('Sneeze'), QtGui.QStandardItem('You have been blocked up')]
        item00 = [QtGui.QStandardItem('Tickle nose, this is a very long entry. Row should resize.'), QtGui.QStandardItem('Key first step')]
        item1 = [QtGui.QStandardItem('<b>Get a job</b>'), QtGui.QStandardItem('Do not blow it')]
        item2  = [QtGui.QStandardItem("Now let's see how this one works. It is medium."), QtGui.QStandardItem('Do not blow it')]       
        self.rootItem.appendRow(item0)
        item0[0].appendRow(item00) 
        self.rootItem.appendRow(item1)
        self.rootItem.appendRow(item2)
        self.setColumnWidth(0,150)
        self.expandAll()
        self.setItemDelegate(ItemWordWrap(self))

class ItemWordWrap(QtGui.QStyledItemDelegate):
    def __init__(self, parent=None):
        QtGui.QStyledItemDelegate.__init__(self, parent)
        self.parent = parent
        
    def paint(self, painter, option, index):
        text = index.model().data(index) 
        document = QtGui.QTextDocument() # #print "dir(document)", dir(document)
        document.setHtml(text)       
        document.setTextWidth(option.rect.width())  #keeps text from spilling over into adjacent rect
        index.model().setData(index, option.rect.width(), QtCore.Qt.UserRole+1)
        painter.save() 
        painter.translate(option.rect.x(), option.rect.y()) 
        document.drawContents(painter)  #draw the document with the painter
        painter.restore()
        
    def sizeHint(self, option, index):
        #Size should depend on number of lines wrapped
        text = index.model().data(index)
        document = QtGui.QTextDocument()
        document.setHtml(text) 
        width = index.model().data(index, QtCore.Qt.UserRole+1)
        if not width:
            width = 20
        document.setTextWidth(width) 
        return QtCore.QSize(document.idealWidth() + 10,  document.size().height())       

def main():
    app = QtGui.QApplication(sys.argv)
    myTree = SimpleTree()
    myTree.show()
    sys.exit(app.exec_())
    
if __name__ == "__main__":
    main()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文