首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在样式表中使用多个QStyledItemDelegate

在样式表中使用多个QStyledItemDelegate
EN

Stack Overflow用户
提问于 2009-12-31 04:36:48
回答 1查看 3.1K关注 0票数 12

我正在创建一个带样式的QTreeView,使用double-dispatch来解析数据项的特定委托,效果非常好。我从QStyledItemDelegate派生了委托的子类,以利用样式表,使设计人员能够在代码之外设计UI样式。

不幸的是,我一直无法解决CSS中的不同样式。如何选择和使用样式表中指定的项子控件样式?

我用来测试的CSS:

代码语言:javascript
复制
QTreeView::item:selected {
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #dddddd, stop: 1 #888888);
}
QTreeView::item:selected[role="title"] {
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #fde7ef, stop: 1 #f1cbda);
}
QTreeView::item:selected[role="entry"] {
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e7effd, stop: 1 #cbdaf1);
}

我的委托呈现类:

代码语言:javascript
复制
class VisitingDelegate(QtGui.QAbstractItemDelegate):
    def __init__(self, parent=None):
        super(VisitingDelegate,self).__init__(parent)
        roles = {}
        self.renderRoles = roles

        d = TitleDelegate(parent)
        d.setProperty("role", "title")
        roles['title'] = d

        d = EntryDelegate(parent)
        d.setProperty("role", "entry")
        roles['entry'] = d

    def delegateForIndex(self, mi):
        role = mi.model().data(mi, "renderRole")
        return self.renderRoles[role]

    def paint(self, painter, option, mi):
        dg = self.delegateForIndex(mi)
        return dg.paint(painter, option, mi)
    def sizeHint(self, option, mi):
        dg = self.delegateForIndex(mi)
        return dg.sizeHint(option, mi)

class TextDocumentDelegate(QtGui.QStyledItemDelegate):
    fmt = "<font color='%(color)s'>%(text)s</font)>"
    def paint(self, painter, option, mi):
        painter.save()

        opt = QtGui.QStyleOptionViewItemV4(option)
        self.initStyleOption(opt, mi)
        opt.text = ''

        style = opt.widget.style()
        style.drawControl(style.CE_ItemViewItem, opt, painter, opt.widget)

        textRect = style.subElementRect(style.SE_ItemViewItemText, opt, opt.widget);
        doc = self.asTextDoc(option, mi)
        painter.translate(textRect.topLeft())
        doc.drawContents(painter)

        painter.restore()

    def sizeHint(self, option, mi):
        doc = self.asTextDoc(option, mi)
        sz = doc.size()
        sz = QtCore.QSize(sz.width(), sz.height())
        return sz

    def asTextDoc(self, option, mi):
        info = {}
        info['text'] = mi.model().data(mi, Qt.DisplayRole)

        doc = QtGui.QTextDocument()
        doc.setDefaultFont(option.font)
        pal = option.palette
        if option.state & QtGui.QStyle.State_Selected:
            color = pal.color(pal.HighlightedText)
        else: color = pal.color(pal.Text)
        info['color'] = color.name()

        doc.setHtml(self.fmt % info)
        return doc

class EntryDelegate(TextDocumentDelegate):
    pass
class TitleDelegate(TextDocumentDelegate):
    fmt = "<h3><font color='%(color)s'>%(text)s</font)></h3>"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-03-25 14:02:24

不能以这种方式选择不同的样式。用于选择样式表规则的属性来自QWidget (本例中为QTreeView ),而不是委托。委托不是小部件,也没有表示单个项目的小部件。您的示例可以通过添加打印来说明这一点,其中样式是从小部件获得的:

代码语言:javascript
复制
style = opt.widget.style()
print opt.widget

它将显示该样式的小部件为QTreeView。因为两个代理的小部件是相同的,所以它不能有两个值的角色设置。

尽管样式表的编写使其看起来像是角色与项目相关联,但规则选择看起来像是这样编写的:

代码语言:javascript
复制
QTreeView[role="title"]::item:selected
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1982351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档