PDA

View Full Version : 2 different custom paintevents are not working



jho
26th April 2015, 19:44
Hi,
I have a strange problem. I have custom qlabel with a new paintevent function. This function will paint and connect points on a QLabel image. So far so good. This works, when I only use one label.

But if I create another label or copy the code and rename "RenderArea" to "RenderArea2", it is not working. Painting inside the first label also paints in the second label. If I raise another window, e.g. taskmanager, the application crashed. Really weired.

I hope you can understand my problem. Thanks.



class RenderArea : public QLabel {
Q_OBJECT

public:
RenderArea(QWidget *parent = 0);

protected:
void paintEvent(QPaintEvent *event);

private:
}





void RenderArea::paintEvent(QPaintEvent *event)
{
QPainter painter(this);

painter.setRenderHint(QPainter::SmoothPixmapTransf orm, true );
painter.setRenderHint( QPainter::Antialiasing, true);
painter.fillRect(event->rect(), QBrush(Qt::white));
painter.translate(30, 30);
painter.save();
painter.restore();

QPen pen;
pen.setColor(Qt::green);
pen.setWidth(4);
pen.setJoinStyle(Qt::RoundJoin);
pen.setCapStyle(Qt::RoundCap);
painter.setPen(pen);
QPoint p;
p.setX(globalX-30);
p.setY(globalY-30);

for(int i=0; i<pValues.count();i++)
{
painter.drawPoint(pValues.at(i));
if(i > 0)
{
if(pValues.at(i).x() == pValues.at(i-1).x() && pValues.at(i).y() == pValues.at(i-1).y())
{
i++;
}
else
{
painter.drawLine(pValues.at(i-1), pValues.at(i));
}


}

}



pValues just stores the painted points so I can repaint them. The strange thing is that the paintevent from label1 paints also the in label2!
I also get the error message "QWidget::repaint: Recursive repaint detected".

anda_skoa
27th April 2015, 07:53
That looks fairly ok (other than then obvious unnecessary save/restore).

So you have two instances of that label?
In a layout?
With different data?

Cheers,
_

jho
27th April 2015, 08:57
Yes, two instances of the label. I also tried to use two different classes instead of instancing. Same result.

I added them to the same layout. The data is different. But it seems that the paint event from "label1" is also applied on "label2". I try to provide the whole code in a simplified way so you can understand the problem better. Maybe it is something very simple.

jho
27th April 2015, 18:58
I attached a simple project.

The window shows 2 large labels in white. Try painting on the first one. It will also paint onto the lower one.

I guess that the problem here is that paintEvent will be triggered for every label. But it is strange that it also paints inside the 2nd label. When I paint onto the 2nd label nothing happens.

11139

anda_skoa
27th April 2015, 19:31
So you have two labels that draw the same data and you are surprised that they look identical?

Cheers,
_

jho
27th April 2015, 21:48
I found my mistake by trying it with another approach (promoting a custom class via the gui). I put all the globals into the custom label class and now every label has its own values. Damn i forgot about the the attributes of an objects. :D

anda_skoa
28th April 2015, 06:57
You know, I asked you if the two instances had different data.
A reasonable approach would have been to check that before answering yes despite the facts pointing to the opposite.

Cheers,
_

jho
28th April 2015, 13:32
I thought they were different. At least I thought that the MousePos were Screen Positions. Because the coordinates are based on the widget coordinates they are also appearing in the 2nd widget.

And in my last reply I already said that I did a mistake.

anda_skoa
28th April 2015, 17:16
I thought they were different. At least I thought that the MousePos were Screen Positions. Because the coordinates are based on the widget coordinates they are also appearing in the 2nd widget.

No, that's not the cause at all.
What the data contains is totally irrelevant.

You are using the same list of points, literally the exactly the same data.

Cheers,
_