How top place a Image incide a custom dial without using Qml
-
void DialWidget::drawImage(QPainter *painter) { QImage pix("J:/#Curneu/UI/Head Positioning/front.png"); QRectF target(m_SIDE_LENGTH / 2, m_SIDE_LENGTH / 2,80,60); QRectF source(m_SIDE_LENGTH / 2, m_SIDE_LENGTH / 2,4.0,4.0); painter->save(); painter->drawImage(target,pix,source); painter->restore(); }
So this is the function that i am using and I didn't get any errors but , I can't able to see the image at the center of the dial . Help me with this
-
@Jaswenth-S
Hi
You can check with
https://doc.qt.io/qt-5/qpixmap.html#isNull
if it was loaded.You are using an overload that also accepts a rect for source.
Do you mean to only use some part of the image ?
as else you can just draw it without the source and full image is used.
https://doc.qt.io/qt-5/qpainter.html#drawImage-4
this will also scale the image to the rest.
So it the goal to fit the image to some rect or do you just want to draw the image as is ? -
Hi
So in PaintEvent, you call drawImage before the normal paint for dial or how did
Did you integrate it with the normal drawing ? -
void DialWidget::paintEvent(QPaintEvent *) { QPainter *painter = new QPainter(this); painter->setRenderHints(QPainter::Antialiasing, true); // Set the origin to the center of the dial widget painter->translate(m_SIDE_LENGTH / 2, m_SIDE_LENGTH / 2); drawFace(painter); drawTickMarks(painter); drawTitleAndUnitLabels(painter); drawValueLabels(painter); drawImage(painter); painter->end(); }
This is how I did
First I tried using indicator needle at the center its working fine .. but when i try with an image it doesn't turn up
-
Ok. so it's a total custom dial.
Could be the
painter->translate
You can test what by replace
painter->drawImage(target,pix,source);
with
painter->drawRect(target);If path is surely valid, then it must be the rects.
-
okei I will try that and inform ....
-
Now I can able to display a rectangle inside the dial ,, but when trying with an image its not coming even with the same target , I think it has some issue with the source when trying as a image . how to set source for a painter->drawImage(target,image,source) ???
-
Hi,
@Jaswenth-S said in How top place a Image incide a custom dial without using Qml:
QImage pix("J:/#Curneu/UI/Head Positioning/front.png");
That path looks wrong.
Did you check that the image is successfully loaded ?
-
@Jaswenth-S
Hi
You can check with
https://doc.qt.io/qt-5/qpixmap.html#isNull
if it was loaded.You are using an overload that also accepts a rect for source.
Do you mean to only use some part of the image ?
as else you can just draw it without the source and full image is used.
https://doc.qt.io/qt-5/qpainter.html#drawImage-4
this will also scale the image to the rest.
So it the goal to fit the image to some rect or do you just want to draw the image as is ?