本文介绍了在paintevent之外的QPainter画家对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 QWidget 窗口中绘制一个简单的二维图形.定义了一个paintEvent,同时定义了一个QPainter 类的painter 对象.我的绘图包含我需要在不同位置多次绘制的元素,例如线条、文本等.为此,我使用函数以不同的位置绘制这些线条.文本也是如此.为了使程序更短,也模块化.

I am trying to draw a simple two dimensional figure in a QWidget window.There is a paintEvent defined and a painter object of the QPainter class is also defined.My drawing contains elements which I need to draw more than once at various locations, such as lines, text etc. For this purpose, I am using functions to draw these lines with varying positions. Similarly for text. In order to make the program shorter, also modular.

paintEvent 函数正在调用用于计算和绘制的函数.

The paintEvent function is calling functions which are used to calculate and draw.

如何将 paintEvent 中定义的 QPainter 画家对象传递给函数.

How do I pass the QPainter painter object defined in the paintEvent into the functions.

例如

void Classname::drawText(QString text, int PosX, int PosY, QPainter painter)
{
    QSize size=this->size();

    QFont times("Helvetica [Cronyx]", 10);
    QFontMetrics box(times);

    int boxWidth = box.width(text);
    int boxHeight = box.height();

    painter.setFont(times);
    painter.setPen(Qt::white);
    painter.drawText(PosX,PosY,text);
}

然后我收到一个错误,其中 vc++ 环境告诉我 QPainter 类的画家对象不允许使用该类型名.

then I get an error where the vc++ environment is telling me that the typename is not allowed for the painter object of QPainter class.

如果我定义QPainterpainter1对象如下图:

If I define QPainter painter1 object as shown below:

void Classname::drawText(QString text, int PosX, int PosY, QPainter painter)
{
    QPainter painter1;

    QSize size=this->size();

    QFont times("Helvetica [Cronyx]", 10);
    QFontMetrics box(times);

    int boxWidth = box.width(text);
    int boxHeight = box.height();

    painter.setFont(times);
    painter.setPen(Qt::white);
    painter.drawText(PosX,PosY,text);
}

程序编译但没有输出.

the program compiles but there is no output.

这是代码的一部分,我在所有函数中定义了QPainter类的对象.

This is a part of the code, I am defining objects of the QPainter class in all the functions.

我阅读了这个帖子,但是说明不明确.begin()end() 函数必须在所有绘图实例中调用还是只在 paintEvent 函数中调用一次?

I read this thread, but the instructions are not clear. Must the begin() and end() function be called at all instances of drawing or just once in the paintEvent function?

推荐答案

正如你提到的,你应该在你的类中实现这些功能.

As you mentioned you shall implement those functions in your class.

在您的标题中:

class Class
{
// ...
protected:
    virtual void paintEvent( QPaintEvent* aEvent ) override;

private:
    void drawText( QPainter* aPainter, const QString& aText, int aPosX, int aPosY );
    // void drawLine( ... );
};

在您的来源中:

void Class::paintEvent( QPaintEvent* aEvent )
{
    QPainter painter( this );

    // ...
    drawText( &painter/*, ... */ );
    drawLine( &painter/*, ... */ );
}

void Class::drawText( QPainter* aPainter, const QString& aText, int aPosX, int aPosY )
{
    // Your drawing ...
}

这篇关于在paintevent之外的QPainter画家对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 22:05