PDA

View Full Version : Problems with QPainter



franco.amato
10th March 2010, 07:19
Hi to all,
I'm writing a config dialog that I exec from my main application.
With the dialog the user can choose the colors that will be used to draw an audio waveform.
In the dialog I would show a preview of the waveform so I added a little QFrame named previewFrame and in the paintEvent I would
draw a little sine function over it.

This is part of the code of the paint event.


void ConfigDlg::paintEvent( QPaintEvent* event )
{
int h = ui.previewFrame->height();
int w = ui.previewFrame->width();

QPainter p( ui.previewFrame ); //<--give error!!!!!!!!
p.setRenderHint( QPainter::Antialiasing, true );
p.fillRect( 0, 0, w, h, bgColor );
QPen pen = QPen( Qt::darkRed, 1 );
}

The problem is that I get the following error

QPainter::begin: Paint device returned engine == 0, type: 1
QPainter::setRenderHint: Painter must be active to set rendering hints
Where the code is wrong?

Best Regards

zgulser
10th March 2010, 07:40
hi,

put; p.begin(this); and try again.

franco.amato
10th March 2010, 07:53
hi,

put; p.begin(this); and try again.

I set
p.begin(ui.previewFrame);

but the error is still there :(

zgulser
10th March 2010, 08:02
Hımm..Then once more;

1. check paintEvent if it's virtual
2. it seems -unexpectedly- paintEngine doesn't work. So you force it to work by putting the line; p.paintEngine()->setActive(true);

zgulser
10th March 2010, 08:03
Please let me know the result

kwisp
10th March 2010, 10:21
What type ui.previewFrame have?
I think, in paintEvent you can paint only on this object, if the object inherit QWidget. If object inherit QImage QPixmap, you cat paint on it everywhere.

franco.amato
10th March 2010, 15:08
What type ui.previewFrame have?
I think, in paintEvent you can paint only on this object, if the object inherit QWidget. If object inherit QImage QPixmap, you cat paint on it everywhere.

Hi ui.previewFrame is a QFrame, so the problem is that I don't paint on this ?

So how can I draw on my ui.previewFrame object tha's part of the dialog?

Lykurg
10th March 2010, 15:11
so the problem is that I don't paint on this ?
Yes. The paint event only affect the current widget, no other.

franco.amato
10th March 2010, 15:21
I changed with:


QPainter p( this );

and I don't get the error but it doesn't paint where it should!

Lykurg
10th March 2010, 16:16
Ehm, what is so hard to understand: You CAN'T paint on your frame from an other widgets paint method. If you want to paint on that frame you have to subclass that frame and use its paint event.

franco.amato
10th March 2010, 22:45
Ehm, what is so hard to understand: You CAN'T paint on your frame from an other widgets paint method. If you want to paint on that frame you have to subclass that frame and use its paint event.

Lykurg please don't scream I'm not deaf and it can affect your heart.
I wrote my last post WITHOUT seeing your FIRST reply :cool:.
Meanwhile thank you and I think Qt should allow to draw on a widget wihout subclass everything.

Best Regards

Lykurg
11th March 2010, 07:39
Qt should allow to draw on a widget wihout subclass everything.That would break the logic of OOP. You could never say how your widget would look like since it could altered from everywere. And why is subclassing always a problem. I like it...

wysota
11th March 2010, 08:39
Meanwhile thank you and I think Qt should allow to draw on a widget wihout subclass everything.

It does. You can install an event filter on the widget you wish to paint on and intercept its paint events. Of course you will still have to subclass some QObject to implement the filter.

suneyes
15th March 2010, 07:29
Yes. U could install event filter on widget and handle its paint event as follows :


bool ConfigDlg::eventFilter(QObject *obj, QEvent *eve)
{
switch(eve->type())
{
case QEvent::Paint:
{
return handlePaintEvent(obj, eve);
}

default:
{
return QObject::eventFilter(obj, eve);
}
}
} // eventFilter

bool ConfigDlg::handlePaintEvent(QObject *obj, QEvent *eve)
{
QPainter painter(static_cast<QWidget *>(obj)); //here u may check if obj is your ui.frame()
......
}