繁体   English   中英

在调用插槽上绘制小部件

[英]painting the widget on calling slot

我想用用户提供的颜色绘制一个圆,并在水平对齐时对其进行线条编辑调整。

在插槽上使用了Painter函数调用,但无法正常工作

#include <QPainter>
#include "cascadeColorHighlightWidget.h"

CascadeColorHighlightWidget::CascadeColorHighlightWidget(QWidget *parent) : QWidget(parent)
{
    setWindowFlags(Qt::FramelessWindowHint | Qt::Widget);
    setAttribute( Qt::WA_DeleteOnClose, true );
    setFixedSize(187,164);
    setContentsMargins(0,0,0,0);
}

void CascadeColorHighlightWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    QRectF contRect = contentsRect().adjusted(1, 1, -1, -1);
    painter.setPen(QPen(QColor(176, 176, 176),1));
    painter.setBrush(QColor(255,255,255));
    painter.drawRect(contRect);

    painter.setPen(QPen(QColor(51,51,51),1));
    QFont font( "Calibri" );
    font.setPixelSize(14);
    painter.setFont( font );

    painter.drawText(QPointF(contRect.x() + 18, contRect.y() + 28), "Color Highlight");
}

void CascadeColorHighlightWidget::focusOutEvent(QFocusEvent *event)
{
   Q_UNUSED(event);
   close();
}
void CascadeColorHighlightWidget::setColors(QColor color)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    QRectF contRect = contentsRect().adjusted(1, 1, -1, -1);

    int rectYPos = contRect.y() + 55;

    painter.setPen(Qt::NoPen);
    QRectF ellipseRect = QRectF(contRect.x() + 18, rectYPos, 16, 16);

    painter.setPen(Qt::NoPen);
    painter.setBrush(color);
    painter.drawEllipse(ellipseRect);
    /*After this ellipse I need to draw a line edit where user can edit anytime*/

    }

但是通过调用setcolot不能在小部件上绘制椭圆。 只有paintEvent中的项目起作用。

是否可以使用painter进行操作,或者我需要保留widgetItems并插入此wideget中。 请给一些建议

所有绘画工作都应在paintEvent 您必须保持状态,并相应地绘制项目。 具有将QPainter作为参数的方法,并从paintEvent方法中调用它们,并将在此创建的QPainter对象传递给它们。

例:

在您的小部件标题中有:

private:
    void setColors(QColor c) { color = c; }
    void drawEllipse(QPainter & painter);
    QColor color;
    bool draw_ellipse;

如您所见,setColors方法仅设置一种颜色,并且将该颜色保留在私有实例变量color

一种新方法承载绘画作业(以前在setColors中):

void CascadeColorHighlightWidget::drawEllipse(QPainter &painter)
{
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    QRectF contRect = contentsRect().adjusted(1, 1, -1, -1);

    int rectYPos = contRect.y() + 55;

    painter.setPen(Qt::NoPen);
    QRectF ellipseRect = QRectF(contRect.x() + 18, rectYPos, 16, 16);

    painter.setPen(Qt::NoPen);
    painter.setBrush(color);
    painter.drawEllipse(ellipseRect);
    /*After this ellipse I need to draw a line edit where user can edit anytime*/
}

此行中的可变color

painter.setBrush(color);

是使用setColors方法设置的。

paintEvent方法应类似于:

void CascadeColorHighlightWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    QRectF contRect = contentsRect().adjusted(1, 1, -1, -1);
    painter.setPen(QPen(QColor(176, 176, 176),1));
    painter.setBrush(QColor(255,255,255));
    painter.drawRect(contRect);

    painter.setPen(QPen(QColor(51,51,51),1));
    QFont font( "Calibri" );
    font.setPixelSize(14);
    painter.setFont( font );

    painter.drawText(QPointF(contRect.x() + 18, contRect.y() + 28), "Color Highlight");

    if(draw_ellipse)
    {
        drawEllipse(painter);
    }
}

最后,您测试draw_ellipse (不要忘记在构造函数中将其初始化为false ),如果为true ,则调用drawEllipse方法。

让我们绘制椭圆,例如使用QWidgetmousePressEvent

void CascadeColorHighlightWidget::mousePressEvent(QMouseEvent *event)
{
    setColors(QColor(Qt::red));
    draw_ellipse = true;
    update();
}

在这里,您先设置颜色,然后将draw_ellipse设置为true ,然后(非常重要)调用QWidget的更新插槽

[...]当Qt返回主事件循环时,它计划一个绘画事件进行处理。

因此将调用paintEvent方法,并且您的绘画将相应地更新为类的状态( colordraw_ellipse变量)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM