PDA

View Full Version : Problem with QPainter



vijay_kansal
24th July 2010, 12:10
Hi,
I was just beginning to use QPainter and this my sorce code :

QLine line( 0, 10, 10, 10 );

QPen pen( Qt::blue, 10 );

QPainter painter;

painter.begin(wave->device));

painter.drawLine(line);


wave->device is a pointer to a QPushButton object.
painter.begin is returning false and at the application output i could see the statment

QPainter::begin: Paint device returned engine == 0, type: 1

I am unable to make out why my painter object is unable to begin
Is there anything wrong with my source code.
Please help.
I am using Windows XP .

Zlatomir
24th July 2010, 12:17
As far as i know QPainter can paint on widgets only within paintevent, something like this:


void YourWidget::paintEvent(QPaintEvent *) {
//your painting code goes here
}

link to reference (http://doc.trolltech.com/4.6/qpainter.html)

vijay_kansal
24th July 2010, 12:40
I tried your suggestion by making a function in my class Waveform which has a member function device (ptr. to a QPushButton object) and i called this function from my MainWindow's constructors with no parameters.I got same problem once again i.e. application output showed the statement

QPainter::begin: Paint device returned engine == 0, type: 1

and begin function is returning false.

Here is the code of the public member function paintEvent that i made in my class WaveForm

void WaveForm:: paintEvent(QPaintEvent * event=0)
{
QLine line( 0, height() / 2.0, width(), height() / 2.0 );
QPen pen( Qt::blue, 10 ); // red solid line, 1 pixels wide

QPainter *painter=new QPainter();
if(!painter->begin(device))
qDebug()<<"Painter failed to begin";

painter->drawLine(line);
}

Please HELP!!

Zlatomir
24th July 2010, 12:55
You didn't understood me, the QPainter can paint to a widget from it's own paintEvent (so use that code in the paintEvent of widget you are trying to paint <your "device" paintEvent> not in your mainWindow)

LE: You will call: painter->begin(this); //the pointer to the object itself