【问题标题】:painting the widget on calling slot在调用槽上绘制小部件
【发布时间】:2018-05-27 08:57:16
【问题描述】:

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

在插槽上使用了画家函数调用,但它不起作用

#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 中。请给点建议

【问题讨论】:

  • 你应该在QImage上作画。并将这张图片输出到paintEvent
  • 您应该只使用setColors 来更新用于绘制椭圆的颜色(假设颜色是您的类的成员),然后调用QWidget::update(),最终将触发paintEvent。然后在您的paintEvent 方法中,只需添加使用您最近更新的成员“颜色”的椭圆的绘图。

标签: c++ qt qt4


【解决方案1】:

所有的绘画工作都应该在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 的update slot

[...] 当 Qt 返回到 主事件循环。

因此将调用paintEvent 方法,并且您的绘画会根据您的类的状态(colordraw_ellipse 变量)进行更新。

【讨论】:

  • 有没有办法在椭圆旁边保持行编辑,水平布局,以便用户可以为每种颜色输入一些文本。用户应该能够随时编辑和更改文本
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多