如何为当前项目制作 QCombobox 绘画项目委托? (Qt 4)

发布于 2024-07-13 23:30:46 字数 198 浏览 9 评论 0原文

QCombobox 设置项目委托不为当前项目绘制。

我正在尝试创建一个显示不同线类型(实线、点线、短划线等)的组合框。 目前我正在为其内容设置项目委托 以便绘制/绘制线型而不是显示名称。 所有线条类型都正在正确绘制,但是一旦我从 组合框,组合框的当前索引仅显示行名称而不绘制它。 我怎样才能让它在当前的线型上绘制选定的线型 组合框索引?

QCombobox set Item delegate not painting for current Item..

I am trying to create a combo box showing different line types (Solid, Dotted, Dash etc). Currently i am setting item delegate for its content
so as to draw/paint line type instead of displaying names. All line types are drawing currectly but as soon as i am selecting any line type from the
combobox, the current index of combo box is displaying just the line name and not painting it. How can i make it paint the selected line type on the current
combo box index?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

夏天碎花小短裙 2024-07-20 23:30:46

委托在组合弹出窗口中绘制项目:

class LineStyleDelegate(QtGui.QItemDelegate):

    def __init__(self, object, parent = None):
        QtGui.QItemDelegate.__init__(self, parent)

    def paint(self, painter, option, index):
        data = index.model().data(index, QtCore.Qt.UserRole)
        if data.isValid() and data.toPyObject() is not None:
            data = data.toPyObject()
            painter.save()

            rect = option.rect
            rect.adjust(+5, 0, -5, 0)

            pen = QtGui.QPen()
            pen.setColor(QtCore.Qt.black)
            pen.setWidth(3)
            pen.setStyle(data)
            painter.setPen(pen)

            middle = (rect.bottom() + rect.top()) / 2

            painter.drawLine(rect.left(), middle, rect.right(), middle)
            painter.restore()

        else:
            QtGui.QItemDelegate.paint(self, painter, option, index)

        painter.drawLine(rect.left(), middle, rect.right(), middle)
        painter.restore()

    else:
        QtGui.QItemDelegate.paint(self, painter, option, index)

paintEvent 在组合中绘制当前项目。 当然,您可以手动绘制它,但是有一种简单的方法可以绘制组合框控件本身(如果您想要当前的箭头按钮或其他东西):

def paintEvent(self, e):
    data = self.itemData(self.currentIndex(), QtCore.Qt.UserRole)
    if data.isValid() and data.toPyObject() is not None:
        data = data.toPyObject()
        p = QtGui.QStylePainter(self)
        p.setPen(self.palette().color(QtGui.QPalette.Text))

        opt = QtGui.QStyleOptionComboBox()
        self.initStyleOption(opt)
        p.drawComplexControl(QtGui.QStyle.CC_ComboBox, opt)

        painter = QtGui.QPainter(self)
        painter.save()

        rect = p.style().subElementRect(QtGui.QStyle.SE_ComboBoxFocusRect, opt, self)
        rect.adjust(+5, 0, -5, 0)

        pen = QtGui.QPen()
        pen.setColor(QtCore.Qt.black)
        pen.setWidth(3)
        pen.setStyle(data)
        painter.setPen(pen)

        middle = (rect.bottom() + rect.top()) / 2

        painter.drawLine(rect.left(), middle, rect.right(), middle)
        painter.restore()

    else:
        QtGui.QComboBox.paintEvent(self, e)

Delegate to paint items in combo popup:

class LineStyleDelegate(QtGui.QItemDelegate):

    def __init__(self, object, parent = None):
        QtGui.QItemDelegate.__init__(self, parent)

    def paint(self, painter, option, index):
        data = index.model().data(index, QtCore.Qt.UserRole)
        if data.isValid() and data.toPyObject() is not None:
            data = data.toPyObject()
            painter.save()

            rect = option.rect
            rect.adjust(+5, 0, -5, 0)

            pen = QtGui.QPen()
            pen.setColor(QtCore.Qt.black)
            pen.setWidth(3)
            pen.setStyle(data)
            painter.setPen(pen)

            middle = (rect.bottom() + rect.top()) / 2

            painter.drawLine(rect.left(), middle, rect.right(), middle)
            painter.restore()

        else:
            QtGui.QItemDelegate.paint(self, painter, option, index)

        painter.drawLine(rect.left(), middle, rect.right(), middle)
        painter.restore()

    else:
        QtGui.QItemDelegate.paint(self, painter, option, index)

paintEvent to paint current item in combo. You can, of course, paint it manually, but there is the simple way to draw combo box control itself (if you want an arrowbutton or smth in the current):

def paintEvent(self, e):
    data = self.itemData(self.currentIndex(), QtCore.Qt.UserRole)
    if data.isValid() and data.toPyObject() is not None:
        data = data.toPyObject()
        p = QtGui.QStylePainter(self)
        p.setPen(self.palette().color(QtGui.QPalette.Text))

        opt = QtGui.QStyleOptionComboBox()
        self.initStyleOption(opt)
        p.drawComplexControl(QtGui.QStyle.CC_ComboBox, opt)

        painter = QtGui.QPainter(self)
        painter.save()

        rect = p.style().subElementRect(QtGui.QStyle.SE_ComboBoxFocusRect, opt, self)
        rect.adjust(+5, 0, -5, 0)

        pen = QtGui.QPen()
        pen.setColor(QtCore.Qt.black)
        pen.setWidth(3)
        pen.setStyle(data)
        painter.setPen(pen)

        middle = (rect.bottom() + rect.top()) / 2

        painter.drawLine(rect.left(), middle, rect.right(), middle)
        painter.restore()

    else:
        QtGui.QComboBox.paintEvent(self, e)
话少心凉 2024-07-20 23:30:46

我想我以前遇到过这个问题,有一个委托在下拉菜单中正确显示该行,但在组合框本身中却没有。

文档 (http://doc.trolltech.com/4.4/qcombobox.html )指出:

“对于组合框标签中的文本和图标,使用具有 Qt::DisplayRole 和 Qt::DecorationRole 的模型中的数据。”

我怀疑涉及为 DecorationRole 返回合适数据的模型的方法可能有效,但让它按照您想要的方式运行可能会出现问题。

I think I've encountered this problem before, having a delegate that displays the line correctly in the drop-down menu but not in the combo box itself.

The documentation (http://doc.trolltech.com/4.4/qcombobox.html) states that:

"For the text and icon in the combobox label, the data in the model that has the Qt::DisplayRole and Qt::DecorationRole is used."

I suspect that an approach which involves a model that returns suitable data for the DecorationRole might work, but it could be problematic to get it to behave just the way you want it to.

弱骨蛰伏 2024-07-20 23:30:46

您还可以将图像保存为图标并使用 QComboBox::setIconSize() 以避免缩放。

You can also save your images to icons and use QComboBox::setIconSize() to avoid scaling.

悍妇囚夫 2024-07-20 23:30:46

只需重写paintEvent。 这是一些草图代码:

void PenComboBox::paintEvent( QPaintEvent* pEvent)
{
  QComboBox::paintEvent( pEvent);
  QVariant itemData = this->itemData( this->currentIndex(), Qt::DisplayRole);
  if( !itemData.isNull() && qVariantCanConvert<QPen>( itemData))
  {
    QPainter painter(this);
    // .. etc
  }
}

Just override paintEvent. Here is some sketch code:

void PenComboBox::paintEvent( QPaintEvent* pEvent)
{
  QComboBox::paintEvent( pEvent);
  QVariant itemData = this->itemData( this->currentIndex(), Qt::DisplayRole);
  if( !itemData.isNull() && qVariantCanConvert<QPen>( itemData))
  {
    QPainter painter(this);
    // .. etc
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文