PDA

View Full Version : issue with drawLines



Maluko_Da_Tola
1st November 2013, 11:43
Hi everyone,

I have been having a problem using QPainter. I would like to be able to draw lines whose coordinates (x0,y0,x1,y1) are defined outside the paintEvent. I have set the values of this coordinates at the constructor of the Label, where I will be carrying the drawing. Hence, on the constructor of the label I initialized the values for the line coordinates and a QImage that I will be also drawing in the paintEvent. The problem is that only the Image appears on the label and there is no line. However, if on the code below I type drawLine(10,10,50,50) instead of drawLine(x0,y0,x1,y1) the line will actually be drawn (but, of course, I don't want to do this). Are there any suggestions on how to solve this problem? Thank you.

Here is the code for the Label:


class main_Graphics_Label : public QLabel
{
Q_OBJECT
public:

QImage ImageP;
int x0,y0,x1,y1;

main_Graphics_Label(QWidget *parent = 0) : QLabel(parent)
{
ImageP = QImage(200, 200, QImage::Format_ARGB32);
ImageP.fill(qRgb(0, 0, 255 ));
x0=10;
y0=10;
x1=50;
y1=50;
}

void paintEvent(QPaintEvent *e)
{
QPainter painter(this);
painter.drawImage(1,1, ImageP);
painter.drawLine(x0,y0,x1,y1);
}


};

anda_skoa
1st November 2013, 11:52
Can you verify the coordinates?
e.g. in paintEvent()


qDebug() << x0 << y0 << x1 << y1;


Cheers,
_

Maluko_Da_Tola
1st November 2013, 13:28
Thanks, the coordinates were assigned completely cray values:

323144952 1644674568 -2013265920 0

Why is this happening? Shouldn't their value be defined at the constructor of the main_Graphics_Label ?

Added after 1 28 minutes:

The code works now. I went to have some lunch and, when I returned, the code was simply working. Thanks for the help anyways ;)