QLabel show image and drawLine
-
I want to draw a line in a image, and I am using QLabel.
first:
ui.mylabel->setPixmap(QPixmap::fromImage(img1));//display an imagesecond:
void paintEvent(QPaintEvent* ev){//override the paintEvent of Main WindowQPainter draw(this); draw.drawLine(100, 100, 200,200);
}
But the line cannot be shown, because it is covered by the image.
The image should be a background for mylabel, and the line should be in the top layer.How can I solve the problem?
-
Hi,
Have you subclassed QLabel and reimplemented paintevent() ?If so, then you must first drawPixmap first and then drawLine to make it overlap.
-
Is there any other solutions? I have reimplemented the paintEvent of the main windows, which contains a label.
[quote author="p3c0" date="1381121484"]Hi,
Have you subclassed QLabel and reimplemented paintevent() ?If so, then you must first drawPixmap first and then drawLine to make it overlap.[/quote]
-
What do you exactly want to do ? Can you elaborate more ?
So far i understood is
You have a QMainWindow whose paintEvent you have reimplemented(not a good idea) then it has a QLabel which you have added in your form and on which you want to draw the line ? -
[quote author="leafjungle" date="1381125023"]Is there any other solutions? I have reimplemented the paintEvent of the main windows, which contains a label.[/quote]
That's not how it works. Each widget just draws it's own contents, but not it's child widgets. Child widgets are stacked on top and draw it's own contents, and so on...
-
Yes.
What I want is:
put an image on the QMainWindow, then draw a line on the image.And the problem is:
The line is covered by the image, thus I can only see the image, the line is not visible.[quote author="p3c0" date="1381125914"]What do you exactly want to do ? Can you elaborate more ?
So far i understood is
You have a QMainWindow whose paintEvent you have reimplemented(not a good idea) then it has a QLabel which you have added in your form and on which you want to draw the line ?[/quote] -
I understand your points.
But if I want to put an image on the QMainWindow, then draw line on the QMainWindow. How can I make the line on top layer to make sure that it will not be covered by the image.
[quote author="raven-worx" date="1381127103"][quote author="leafjungle" date="1381125023"]Is there any other solutions? I have reimplemented the paintEvent of the main windows, which contains a label.[/quote]
That's not how it works. Each widget just draws it's own contents, but not it's child widgets. Child widgets are stacked on top and draw it's own contents, and so on...
[/quote] -
!http://c.hiphotos.baidu.com/album/w=2048;q=90/sign=27d2541638f33a879e6d071af2642b49/d31b0ef41bd5ad6e2f2fb3cc83cb39dbb7fd3cc6.jpg(image1)!
http://f.hiphotos.bdimg.com/album/s=1400;q=90/sign=bc133aefd52a60595610e51e18040fea/d53f8794a4c27d1e3f07ede219d5ad6eddc438f7.jpg!http://f.hiphotos.baidu.com/album/w=2048;q=90/sign=b6a92afc17ce36d3a20484300ecb01f6/d53f8794a4c27d1e3f07ede219d5ad6eddc438f7.jpg(image2)!
http://c.hiphotos.baidu.com/album/w=2048;q=90/sign=27d2541638f33a879e6d071af2642b49/d31b0ef41bd5ad6e2f2fb3cc83cb39dbb7fd3cc6.jpgLook at the image above. what I want is image1, but the problem is shown in image2.
[quote author="raven-worx" date="1381127103"][quote author="leafjungle" date="1381125023"]Is there any other solutions? I have reimplemented the paintEvent of the main windows, which contains a label.[/quote]
That's not how it works. Each widget just draws it's own contents, but not it's child widgets. Child widgets are stacked on top and draw it's own contents, and so on...
[/quote] -
-
hi,
Both seems to be identical images. Can't see the problem.
-
if you First draw line and then set image then it may be solve.
sorry for poor eng. -
If you want the label to have the image with a line drawn on top then it is the paintEvent() of the QLabel you want to enhance.
@
class MyLabelSubclass: public QLabel {
...
protected:
void paintEvent(QPaintEvent* ev);
};//override the paintEvent of QLabel to draw a line
void MyLabelSubclass::paintEvent(QPaintEvent* ev) {
QLabel::paintEvent(ev); // do the default label displayQPainter draw(this);
draw.setPen(QPen(QColor(Qt::red)));
draw.drawLine(rect().topLeft(), rect().bottomRight());
}
@
Then use MyLabelSubclass on the form rather than a base QLabel. -
In the second image, a part of the line is covered by the image.
[quote author="p3c0" date="1382158070"]hi,
Both seems to be identical images. Can't see the problem.[/quote]
-
There is no "In the second image, a part of the line is covered by the image," because there is no "part of the line" visible in the second. The two images above are byte for byte identical.
@
chrisw@newton /tmp $ md5sum *jpg
1b4061d36b2069e2956457d51fc6d3ad d31b0ef41bd5ad6e2f2fb3cc83cb39dbb7fd3cc6.jpg
1b4061d36b2069e2956457d51fc6d3ad d53f8794a4c27d1e3f07ede219d5ad6eddc438f7.jpg
@