PDA

View Full Version : Problem with QPainter



Eevo
4th November 2012, 09:00
Hi,
I'm having a problem with my code. I gives me a segmentation fault from line 25 and I can't understand why.
Thank you in advance!

#include <QImage>
#include <QPainter>
#include <QString>
#include <QColor>
#include <QFont>
#include <QPoint>
#include <iostream>
#include "star.hh"
#include "galaxy.hh"

class Visualization{
public:
Visualization() {
image = new QImage(1920,1080, QImage::Format_RGB32);
}
~Visualization() {
delete image;
}
void text(size_t inside) {
QPainter painter;
painter.begin(image);
painter.setPen(QColor(245,245,220));
painter.setFont(QFont("Times", 12));
painter.setLayoutDirection(Qt::LeftToRight);
QString text= "Stars inside the box: ";
text.setNum(inside);
painter.drawText(100,100,text); //seqfault
painter.end();
}
private:
QImage* image;
};

ChrisW67
5th November 2012, 22:40
Compiles are runs just fine here. Rebuild your application and, if the crash still exists, run it/single-step it in a debugger until it crashes then inspect the backtrace to find where it is really crashing.

The pointless initilialisation of text at line 25 discarded at line 26... perhaps you wanted QString::arg().
There does not seem to be much point allocating image on the heap.