用于只读、可滚动、可折叠、图标列表的 Qt 小部件 [英] What Qt widget(s) to use for read-only, scrollable, collapsible, icon list

查看:95
本文介绍了用于只读、可滚动、可折叠、图标列表的 Qt 小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Qt 比较陌生,并不完全熟悉开箱即用的小部件.我有一个有点(但不是很)复杂的小部件要创建,并且不想重新发明任何轮子.什么是最好的 QWidget 用作子类和/或 QWidgets 用于组成我的小部件的起点.这是我正在寻找的最终结果(对粗制图表示歉意):

要点:

  • 所有图标都将占据相同的大小,比如 128 x 128.忽略类别分组,它们都应该在一个漂亮的网格中对齐.
  • 小部件应该展开以填充它可以占据的所有水平和垂直区域.水平扩展/缩小可能会增加/减少每行显示的图标数量.
  • 图标是分组的,这些组应该是可折叠的.
  • 如果小部件的高度超过其空间,则应出现垂直滚动条.

解决方案

您实际上正在寻找 QListView/QListWidget 的一些更深奥的选项.

在顶层,添加 QTreeWidgetQTreeView 将为您提供您正在寻找的层次结构,以及管理滚动区域.

(扩展的)QTreeXItem 的每个 Listed Item 将是一个 QListView/QListWidget,设置 setViewMode(QListView::IconMode)) 在他们身上.

请注意,要获得上面想要的精确外观,您可能必须使用 QListView 并使用自定义委托,自己处理绘图(除非您能找到一个完全符合您要求的主题).但是,我已经使用 Q*Widget 类编写了一个简短的 PyQt 解决方案,因为它们更短,并且仍将演示如何获得正确的布局.如果您使用 C++,同样的 Qt 函数调用也适用,但显然您可能需要更多或更少的簿记.

导入系统从 PyQt4 导入 QtGui、QtCoreclass displayItem(QtGui.QWidget): #一个要显示的简单小部件,只需在 100x100 小部件中居中一个数字def __init__(self,num):QtGui.QWidget.__init__(self)self.size=100self.resize(self.size,self.size)self.setMinimumSize(self.size,self.size)self.text = numdefpaintEvent(self,event):p = QtGui.QPainter(self)p.drawText(self.size//2,self.size//2,str(self.text))app = QtGui.QApplication(sys.argv)小部件 = QtGui.QTreeWidget()widget.setWindowTitle('简单树')#构建列表小部件treeItem1 = QtGui.QTreeWidgetItem(widget)treeItem1.setText(0,"TreeWidget Parent") #设置 [+] 框的标题"list1 = QtGui.QListWidget() #这将包含您的图标列表list1.setMovement(QtGui.QListView.Static) #否则图标是可拖动的list1.setResizeMode(QtGui.QListView.Adjust) #每次调整大小时重做布局list1.setViewMode(QtGui.QListView.IconMode) #从左到右布局,不是从上到下listItem = QtGui.QListWidgetItem(list1)listItem.setSizeHint(QtCore.QSize(100,100)) #否则widget项会重叠(烦人的bug)list1.setItemWidget(listItem,displayItem(1))listItem = QtGui.QListWidgetItem(list1) #再增加几项listItem.setSizeHint(QtCore.QSize(100,100))list1.setItemWidget(listItem,displayItem(2))listItem = QtGui.QListWidgetItem(list1)listItem.setSizeHint(QtCore.QSize(100,100))list1.setItemWidget(listItem,displayItem(3))list1.setAutoFillBackground(True) #对于将成为 QTreeWidgetItem 小部件的小部件是必需的treeSubItem1 = QtGui.QTreeWidgetItem(treeItem1) #制作一个子项来保存我们的列表widget.setItemWidget(treeSubItem1,0,list1) #将此列表分配为树项treeItem2 = QtGui.QTreeWidgetItem(widget) #制作一个假的第二个父节点treeItem2.setText(0,"TreeWidget Parent II")widget.show() #以标准 PyQt4 方式启动应用程序sys.exit(app.exec_())

I'm relatively new to Qt, and am not entirely familiar with the out-of-the-box widgets. I have a somewhat (but not very) complex widget to create, and don't want to reinvent any wheels. What is the best QWidget to use as a starting point to subclass and/or QWidgets to use to compose my widget. Here is the end-result I am looking for (apologies for the crude drawing):

Key points:

  • All icons will take up the same size, say 128 x 128. Ignoring the category groupings, they should all align in a nice grid.
  • The widget should expand to fill all the horizontal and vertical area it can take. Expanding / shrinking horizontally may increase / decrease the number of icons shown in each row.
  • Icons are grouped, and those groups should be collapsible.
  • If the height of the widget exceeds its space, a vertical scrollbar should appear.

解决方案

You're actually looking for some of the more esoteric options for a QListView/QListWidget.

At the top level, add QTreeWidget or QTreeView will give you the hierarchy you're looking for, as well as managing the scrolling area.

Each Listed Item of the (expanded) QTreeXItem will be a QListView/QListWidget, setting setViewMode(QListView::IconMode) on them.

EDIT: Note that to get the precise look you wanted above, you'll probably have to use QListView and use a custom delegate, handling the drawing yourself (unless you can find a theme that will do exactly what you want). However, I've coded up a short PyQt solution below using the Q*Widget classes because they're shorter, and will still demonstrate how to get the right layout. If you're using C++, the same Qt function calls apply, but obviously you might have more or less bookkeeping.

import sys
from PyQt4 import QtGui, QtCore

class displayItem(QtGui.QWidget):  #A simple widget to display, just centers a digit in a 100x100 widget
    def __init__(self,num):
        QtGui.QWidget.__init__(self)
        self.size=100
        self.resize(self.size,self.size)
        self.setMinimumSize(self.size,self.size)
        self.text = num
    def paintEvent(self,event):
        p = QtGui.QPainter(self)
        p.drawText(self.size//2,self.size//2,str(self.text))

app = QtGui.QApplication(sys.argv)
widget = QtGui.QTreeWidget()
widget.setWindowTitle('simple tree')

#Build the list widgets
treeItem1 = QtGui.QTreeWidgetItem(widget)
treeItem1.setText(0,"TreeWidget Parent")   #Sets the "header" for your [+] box

list1 = QtGui.QListWidget()                #This will contain your icon list
list1.setMovement(QtGui.QListView.Static)  #otherwise the icons are draggable
list1.setResizeMode(QtGui.QListView.Adjust) #Redo layout every time we resize
list1.setViewMode(QtGui.QListView.IconMode) #Layout left-to-right, not top-to-bottom

listItem = QtGui.QListWidgetItem(list1)
listItem.setSizeHint(QtCore.QSize(100,100)) #Or else the widget items will overlap (irritating bug)
list1.setItemWidget(listItem,displayItem(1))

listItem = QtGui.QListWidgetItem(list1)     #Add a few more items
listItem.setSizeHint(QtCore.QSize(100,100))
list1.setItemWidget(listItem,displayItem(2))

listItem = QtGui.QListWidgetItem(list1)
listItem.setSizeHint(QtCore.QSize(100,100))
list1.setItemWidget(listItem,displayItem(3))

list1.setAutoFillBackground(True)                #Required for a widget that will be a QTreeWidgetItem widget
treeSubItem1 = QtGui.QTreeWidgetItem(treeItem1)  #Make a subitem to hold our list
widget.setItemWidget(treeSubItem1,0,list1)       #Assign this list as a tree item

treeItem2 = QtGui.QTreeWidgetItem(widget)        #Make a fake second parent
treeItem2.setText(0,"TreeWidget Parent II")

widget.show()           #kick off the app in standard PyQt4 fashion
sys.exit(app.exec_())

这篇关于用于只读、可滚动、可折叠、图标列表的 Qt 小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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