On Wed, Oct 3, 2018 at 6:27 AM Murphy, Sean <smur...@walbro.com> wrote:

>
> This looks promising - I had never stumbled across QWidgetAction before.
> Although now
> that you point it out, I've discovered the section of the QMenu
> documentation that I
> previously skimmed over that mentions it!
>
> Is the code in your paintEvent() something you can share, if there's
> anything noteworthy
> happening in there?
>

I don't think it's very noteworthy, but here's what we're using:

void WMColorPickerToolButton::paintEvent(QPaintEvent* event) {

        Q_UNUSED(event);


        QStylePainter painter(this);

        painter.save();

        painter.setPen(palette().color(QPalette::Text));


        // Draw the regular tool button elements.

        QStyleOptionToolButton opt;

        initStyleOption(&opt);


        // Clear the text in the opt object since text that we don't

        // draw in this function will get in the way of the button's

        // regular text.

        opt.text.clear();


        painter.drawComplexControl(QStyle::CC_ToolButton, opt);


        // Get the rectangle into which the sample should be drawn.

        QRect destField(style()->subControlRect(QStyle::CC_ToolButton, &opt,
QStyle::SC_ToolButton));


        // Offset the rectangle by the style's button margin pixel metric.

        int margin = style()->pixelMetric(QStyle::PM_ButtonMargin,  &opt, this);

        destField.adjust(margin, margin, -margin, -margin);


        // Draw the patterned background

        WMColorPickerWidget::drawPatternedBackground(&painter, destField);


        // Draw the color sample.

        painter.fillRect(destField, _colorPickerAction->currentColor());


        // Draw an outline rectangle.

        QRect outlineRect(destField);

        outlineRect.adjust(0, 0, -1, -1);

        painter.setPen(palette().color(QPalette::Text));

        painter.drawRect(outlineRect);

        int thickness = 1;

        outlineRect.adjust(thickness, thickness, -thickness, -thickness);

        painter.setPen(palette().color(QPalette::Light));

        painter.drawRect(outlineRect);


        // Restore the painter.

        painter.restore();

}

WMColorPickerWidget::drawPatternedBackground draws a checkerboard
background so that it's more obvious if a color's alpha value is not fully
opaque.

To be clear, that's the paintEvent for the tool button itself.  The QWidget
that's used by the QWidgetAction is just a container for other widgets in a
layout and doesn't have any need to reimplement paintEvent.

Adam
_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Reply via email to