1
votes

I am trying to make a class that will make a dot graphic. The class inherits from QWidget. I want it to draw lines and dots on a QPixmap that will be displayed in a QLabel.

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. What do i do wrong?

1
We'd need to see more of your code to tell what exactly you're doing wrong. - Venemo
Actually it works for me (Windows 8.1, Qt 5.4.1, VS 2013). So please describe the problem. By the way I think you don't need to store the layout and the pixmap as a member variable, or do you use it later as well ? - p.i.g.
@Venemo: All my drawing code works if i put it in paintEvent but it crashes when i try doing it with the pixmap. So the rest of the drawing code is not really relevant. - Antoine LeBrun
@vizhanyolajos With this code, the app will crash without compiler error message. - Antoine LeBrun
@vizhanyolajos And since the pixmap is used in a function and not directly in the constructor I figured it would be easier to make it a member variable rather than passing an argument to the function. - Antoine LeBrun

1 Answers

0
votes

Did you call the default base class constructor before Your class constructor ?

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