带有自定义小部件的 qlistwidgetitem 在 UI 上看起来没有被选中
我有一个处于图标模式的 Qlistwidget,并且我正在使用 setItemWidget 在自定义小部件中显示我的元素,到目前为止这是有效的。
几乎就像这样:
我唯一的问题是当我选择项目时,它们看起来没有被选中(它们周围没有框架)。它们被选择是因为我收到了正确的信号,但您在用户界面上看不到选择。 关于如何让它们看起来被选中有什么想法吗?
** 编辑以添加示例代码 **
(这是对上一个链接中找到的代码的修改)
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)
#Build the list widgets
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.show() #kick off the app in standard PyQt4 fashion
sys.exit(app.exec_())
谢谢
/J
I have a Qlistwidget in icon mode and I'm using setItemWidget to display my elements in my custom widgets, so far this is working.
Pretty much is like this one:
The only problem I have is that when I select the items, they don't look selected (no frame around them). They are being selected as I'm getting the right signals but you can't see the selection on the UI.
Any ideas on how to make them appear selected?
** Edit to add sample code **
(it is a modification on the code found on the previous link)
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)
#Build the list widgets
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.show() #kick off the app in standard PyQt4 fashion
sys.exit(app.exec_())
Thanks
/J
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。 。
它与
viewMode
有关。当我将 list1 的
viewMode
设置为ListMode
时,所选项目看起来已选中(突出显示),但仍试图找出它无法与
iconMode
一起使用的原因代码>. 。 。Yes. .
it is related to the
viewMode
.When I set the
viewMode
for the list1 asListMode
, selected items look selected(highlighted)still trying to figure out why it is not working with the
iconMode
. . .