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);
}
};
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);
}
};