带有自定义小部件的 qlistwidgetitem 在 UI 上看起来没有被选中

发布于 2024-11-14 22:20:47 字数 1967 浏览 4 评论 0原文

我有一个处于图标模式的 Qlistwidget,并且我正在使用 setItemWidget 在自定义小部件中显示我的元素,到目前为止这是有效的。

几乎就像这样:

https://stackoverflow.com/questions/ 3639468/what-qt-widgets-to-use-for-read-only-scrollable-collapsible-icon-list

我唯一的问题是当我选择项目时,它们看起来没有被选中(它们周围没有框架)。它们被选择是因为我收到了正确的信号,但您在用户界面上看不到选择。 关于如何让它们看起来被选中有什么想法吗?

** 编辑以添加示例代码 **

(这是对上一个链接中找到的代码的修改)

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:

https://stackoverflow.com/questions/3639468/what-qt-widgets-to-use-for-read-only-scrollable-collapsible-icon-list

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 技术交流群。

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

发布评论

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

评论(1

∞琼窗梦回ˉ 2024-11-21 22:20:47

是的。 。
它与viewMode有关。
当我将 list1 的 viewMode 设置为 ListMode 时,所选项目看起来已选中(突出显示),

list1.setViewMode(QtGui.QListView.ListMode)

但仍试图找出它无法与 iconMode 一起使用的原因代码>. 。 。

Yes. .
it is related to the viewMode.
When I set the viewMode for the list1 as ListMode, selected items look selected(highlighted)

list1.setViewMode(QtGui.QListView.ListMode)

still trying to figure out why it is not working with the iconMode. . .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文