简体   繁体   English

在QPixmap上使用QPainter时应用崩溃

[英]App crash when using QPainter on QPixmap

I am trying to make a class that will make a dot graphic. 我正在尝试制作一个将制作点图形的课程。 The class inherits from QWidget. 该类继承自QWidget。 I want it to draw lines and dots on a QPixmap that will be displayed in a QLabel. 我希望它在将显示在QLabel中的QPixmap上绘制线条和点。

The constructor of the class looks like this: 该类的构造函数如下所示:

MyClass::MyClass()
{
    calcul_proprietes(); // Function that makes calculation of what to draw.
    pix = new QPixmap(760,350);

    dessiner_graphique(); // Function that does the drawing.

    //Displaying the qpixmap
    layout_principal = new QVBoxLayout(this);
    label_pix = new QLabel(this);
    label_pix->setPixmap(*pix);
    layout_principal->addWidget(label_pix);
    this->setLayout(layout_principal);
}

And a short part of the function that does the drawing 和做图的功能的一小部分

void MyClass::dessiner_graphique()
{
    // ...
    QPainter painter(pix);
    QRect contour(x_depart,y_depart,largeur_grille,hauteur_grille);
    painter.drawRect(contour);
    // ...
}

I don't want to use the paintEvent function because it gets called all the time and i only need my graphic to be painted once. 我不想使用paintEvent函数,因为它一直被调用,并且我的图形只需要绘制一次。 What do i do wrong? 我做错了什么?

Did you call the default base class constructor before Your class constructor ? 您是否在类构造函数之前调用了默认的基类构造函数?

MyClass( QObject *parent )
: QWidget( parent )
{
    // Your posted code.
}

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

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