如何跨小部件状态实现 QListWidget 项目的一致突出显示? [英] How do I achieve consistent highlighting of QListWidget items across widget states?

查看:36
本文介绍了如何跨小部件状态实现 QListWidget 项目的一致突出显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PyQT 4.8.3 创建一个对话框,其中包含两个允许多选的 QListWidgets.

I am using PyQT 4.8.3 to create a dialog with two QListWidgets both allowing multiple selection.

  1. 我发现如果启用了这些 QListWidgets,只有当 QListWidget 有焦点时,所选项目才会以蓝色突出显示,没有焦点时突出显示为浅灰色.

  1. I find that if these QListWidgets are enabled, the selected items are highlighted in blue only when the QListWidget has focus, without focus the highlight is light-grey.

我还发现,如果 QListWidgets 被禁用,即使缺乏焦点,所选项目也会以蓝色突出显示.

I also find that if the QListWidgets are disabled, the selected items are highlighted in blue despite lack of focus.

当用户从一个列表转到另一个列表时,他们会发现这非常令人困惑.

As the users go from one list to the other they will find this very confusing.

作为一名开发人员,我发现浅灰色/未聚焦、蓝色/禁用的行为是不可取的.如果您有任何修改它们的建议,我将不胜感激.

As a developer I find the light-grey/unfocused, blue/disabled behaviours undesirable. I would appreciate any advice on modifying them.

我已经浏览了 QListWidget、QListView 和 QAbstractView 没有找到任何适用的内容,我还查看了 样式表 没有运气的文档.

I've looked through the docs for QListWidget, QListView and QAbstractView without finding anything applicable, I have also looked through the stylesheet documentation without having any luck.

推荐答案

我会在这里使用样式表.在此示例中,此 QListWidget 中的选定项将以蓝色突出显示,当 QListWidget 禁用或没有焦点时,它们将变为灰色:

I would use stylesheets here. In this example, the selected items in this QListWidget will he highlighted in blue, and when the QListWidget is disabled or without focus they will turn gray:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PyQt4 import QtCore, QtGui

class myWindow(QtGui.QWidget):

    def __init__(self, parent=None):
        super(myWindow, self).__init__(parent)

        self.setStyleSheet( """ QListWidget:item:selected:active {
                                     background: blue;
                                }
                                QListWidget:item:selected:!active {
                                     background: gray;
                                }
                                QListWidget:item:selected:disabled {
                                     background: gray;
                                }
                                QListWidget:item:selected:!disabled {
                                     background: blue;
                                }
                                """
                                )

        self.listWidget = QtGui.QListWidget(self)
        self.listWidget.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)

        self.button = QtGui.QPushButton(self)
        self.button.setText("Disable the list!")
        self.button.clicked.connect(self.on_button_clicked)

        self.layout = QtGui.QVBoxLayout(self)
        self.layout.addWidget(self.button)
        self.layout.addWidget(self.listWidget)

        for itemNumber in range(5):
            item = QtGui.QListWidgetItem(self.listWidget)
            item.setText("Item {0}".format(itemNumber))
            self.listWidget.addItem(item)


    @QtCore.pyqtSlot()
    def on_button_clicked(self):
        enable = False if self.listWidget.isEnabled() else True

        self.listWidget.setEnabled(enable)

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('myWindow')

    main = myWindow()
    main.show()

    sys.exit(app.exec_())

这篇关于如何跨小部件状态实现 QListWidget 项目的一致突出显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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