PDA

View Full Version : Painting outside widget geometry?



mbjerkne
8th February 2006, 00:50
I have a QWidget that I am overriding the paintEvent method. The widget is basically a square with some "arms" . For the most part I want to be able to move this widget around using widget->move(x,y); But I added a new method to extend the "arms" of the widget. My problem is that the widget's size is the size of the rectangle I want to draw. And when I try to draw the "arms" they do not get drawn.

Here is my code in the paint event.



void MyWidget::paintEvent(QPaintEvent * event)
{
double dWidth = width();
double dHeight = height();

QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);


painter.save();
QPen pen(Qt::black);
pen.setWidth(5);
painter.setPen(pen);

QRectF rect(0.0,0.0,dWidth,dHeight);
painter.drawRect(rect);

pen.setColor(Qt::red);
painter.setPen(pen);

if ( m_nArmY < 0 )
{
QLineF arm1(0.0,0.0,0.0,m_nArmY);
QLineF arm2(dWidth,0.0,dWidth,m_nArmY);

painter.drawLine(arm1);
painter.drawLine(arm2);
}
else
{
QLineF arm1(0.0,dHeight,0.0,m_nArmY + dHeight);
QLineF arm2(dWidth,dHeight,dWidth,m_nArmY + dHeight);

painter.drawLine(arm1);
painter.drawLine(arm2);
}


painter.restore();
}

wysota
8th February 2006, 10:29
The painter is clipped to the widget rectangle, so that you can't paint over other widgets, you shouldn't try to override it. You could create external widgets for your arms and set their background to be invisible and move those widgets along with the "main" widget.

mbjerkne
8th February 2006, 15:59
Thanks. That's what I figured the problem was. I'll try to create the arms the way you said, and see how that works.