How to Draw Any Shape on QLabel Image
-
I am using QLabel For Image Displaying Now I want To Draw Shape On Image
Like Rectangle Or Lineplz Help Me For Slove This Problem
-
@Ketan__Patel__0011 Subclass QLabel and override paintEvent where you then use QPainter to draw what ever you need.
Here an example: https://doc.qt.io/qt-5/qtwidgets-widgets-analogclock-example.html -
Hi
You can also draw on a pixmap and show in label.However, if you plan to let user draw the rect or need to be able to erase them again or similar then
subclassing is the way to go.int GetBarHeight(int MAX) {
return rand() % (MAX-5)+5;
}void MainWindow::on_pushButton_released() {
int h = ui->label->height();
int w = ui->label->width();
QPixmap pix(w, h);
QPainter paint(&pix);
// paint the image over the pix map here.
pix.fill( Qt::white );
paint.setPen(QColor(0, 0, 0, 255));
int y = 0;
int x = 0;
int bw = 10; // bar width
for (int barcount = 0; barcount < 12; ++barcount) {
paint.setBrush(QColor(255-x, 34+x, 255, 255));
paint.drawRect(x, h - GetBarHeight(h), bw, h );
x += bw + 4;
}
paint.end();
ui->label->setPixmap(pix);
} -
ok then you should create your own label subclass for that as that is much easier than doing it via MainWIndow.
You can use this class
https://doc.qt.io/qt-5/qrubberband.htmlto make it possible to drag out a rect very easy if you don't want to hand program that effect your self.
There is example here of such widget
http://mangoprojects.info/c/qtc-drawing-a-line-or-rectangle-dynamically/ -
@Ketan__Patel__0011 Then, in addition what I already wrote, you will need to override mousePressEvent, mouseMoveEvent and mouseReleaseEvent