Qt - QTreeView WordWrap 不工作
我一直在一些论坛上阅读有关 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我唯一知道的 hack - 它是 Tree Item 中的 QLable。
Only hack I know - it's QLable inside Tree Item.
正如评论者所建议的,您可以使用自定义委托来执行此操作。不幸的是, QTreeView.setWordWrap(True) 似乎没有效果(至少在 Qt 4.8 中)。为了回答另一个问题,我们使用 QTreeView 的自定义 QStyledItemDelegate 实现了自动换行功能:
在 QTreeView (Qt/PySide/PyQt) 中实现自动换行委托?
这只是一种方法,我确信还有其他好方法,并注意这个例子非常基本,没有编辑器或类似的东西......
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...