PDA

View Full Version : [PARTIALLY SOLVED] Error drawing?



QNewbie
7th June 2014, 00:52
EDIT: For anyone getting this kind of error, override the paintEvent(QPaintEvent*) method..
EDIT 2: The painting is happening as many times as something is drawn in the screen, aint there a way for this to happen only once without adding some bool in there?
Hey all, I've got a project going where I'm trying to learn about clipping regions and painting.
However, I'm stuck.
The code presented below shows the clipped widget. Now, when I try to paint a red line on the borders of the clipped widget, it will, no error thrown, not even try to paint, or am I missing something?


Cargador::Cargador(QWidget *parent) : QWidget(parent,Qt::FramelessWindowHint), ui(new Ui::Cargador) {
ui->setupUi(this);
resize(500,255);
QRegion esquina;
QRegion circulo;
QRegion region(0,0,width(),height(),QRegion::Rectangle);
// SUPERIOR IZQUIERDO
circulo = QRegion(0,0,50,50,QRegion::Ellipse); // Circulo hardcodeado
esquina = QRegion(0,0,25,25,QRegion::Rectangle); // Cuadrado de un cuarto del circulo
region = region.subtracted(esquina.subtracted(circulo));
// INFERIOR DERECHO
circulo = QRegion(width()-50,height()-50,50,50,QRegion::Ellipse);
esquina = QRegion(width()-25,height()-25,25,25,QRegion::Rectangle);
region = region.subtracted(esquina.subtracted(circulo));
setMask(region);
QPixmap logo(":/Cargador/Imagenes/Logo.png",0,Qt::AutoColor);
// qDebug() << logo;
ui->lblLogo->setPixmap(logo);
ui->lblLogo->show();
// Labels transparentes
ui->label->setAttribute(Qt::WA_TranslucentBackground);
ui->label_2->setAttribute(Qt::WA_TranslucentBackground);
ui->lblCargando->setAttribute(Qt::WA_TranslucentBackground);
ui->lblLogo->setAttribute(Qt::WA_TranslucentBackground);
// Borde en negro.
QPainter decorador(this);
//QPolygon poligono;
QPen linea;
QPainterPath zona;
zona.addRegion(region);
linea.setWidth(8);
linea.setColor(Qt::red);
decorador.setPen(linea);

decorador.drawPath(zona);



}

ChrisW67
7th June 2014, 09:38
The paintEvent() code should render the entire widget or just the region passed in with the event parameter. How you do that in an optimal fashion is entirely up to you and your application characteristics. The paint event code is called as often as required to keep the display current, for example every time the widget is resized, partly hidden, shown or exposed after being obscured. Qt already takes some actions to merge successive events that might trigger repaint into a single call.

Painting in the constructor, i.e. before the widget is visible, will simply be obliterated the first time the paint event fires after you call show() (if it draws anything at all).

QNewbie
7th June 2014, 15:56
You're right, that's why I moved the code to the overriden paintEvent() method.
Yet it paints something weird, the code I added to the function was:


void Cargador::paintEvent(QPaintEvent *evento) {
QPainter decorador(this);
QPen linea;
QPainterPath zona;
linea.setWidth(3);
decorador.setPen(linea);
zona.addRegion(region);
decorador.drawPath(zona);
}


However instead of drawing one line, I'm greated with this:
http://imgur.com/AdohrJV

wysota
8th June 2014, 09:13
Could you start again from telling us what is the intended behaviour and what is the current behaviour you are experiencing? The image you posted doesn't really explain the problem.