Is it possible to make a QLabel Pixmap image appear in a circle using stylesheet - c++

I currently have an image with no borders. I am displaying these images using QLabel and they have and they a square boundary. I wanted to know if it possible to display these images in a circular boundary. In short is it possible to make the boundary of QLabel circular. I want to achieve something like this

You can use a mask to modify the shape of your widget:
QPixmap pix( "pathToImage.png" );
QLabel label;
label.setPixamp( pix );
label.setMask( pix.mask() );
Your QLabel will have the same shape than your image.

Related

Set Image as Background and Text on Top of it in qlabel

I have a PNG file with 45x45 ratio
I have Qlabel with 270x30 Pixel ratio
what I want is to insert PNG file as background and Text on it dynamilaclly.
Text length may change from large to small and vise versa,
accorindly our image changes and fit the text inside the images its mean flexible image according to the text
QPixmap pixmapTarget = QPixmap(":/.png");
pixmapTarget = pixmapTarget.scaled(250, 27, Qt::IgnoreAspectRatio , Qt::SmoothTransformation);
ui->lable_1->setIcon(pixmapTarget);
but dont to insert text in the image and how to get flexible thing accoudning to the text.
A QLabel can contain either a text or an image. Not both at same time. If you want to have an image as a background to a text, you will need to either use a QTextView and setup your text and background image as rich text, overlay two QLabel instances on top of each other with the image being the bottom one, or implement your own custom QWidget. The latter can be done in a variety of ways, including subclassing QLabel to in paintEvent first render the background image (see QPainter's documentation on how to draw a QImage or better, a QPixmap) and then call the base implementation of paintEvent to render the text.

How to set QPixmap in a specific coordinates of QLabel?

I want to display an image in a QLabel in a specific position with covering a particular area so that I can use the rest of the QLabel part by other images. By the following, I was able to display image with my set size.
QPixmap pix("....image_path..../image.png");
ui->label->setPixmap(pix);
int width = 300;
int height = 320;
ui->label->setPixmap(pix.scaled(width,height));
But I am curious to know how to set the position of Image in QLabel. Are the any function in Qt by using which I can set my image coordinates in a QLabel?
Appreciate your help. Thanks in advance.
You can do this by painting a new pixmap in layers. Pseudo code
QPixmap map;
QPainter p(&map)
p.drawImage(w1,h1,img_1)
...
p.drawImage(wn,hn,img_n)
label.setPixmap(map)

How to use QImage and mousetracking event at the same time in QT?

I am starting to use Qt and I am trying to do an application that uses mouseEvents on a picture QImage. Since I know that QImage is part of QPaintDevice and mouseEvents is part of QWidgets, I don't know how to use both on the same class or even if it would require me to make two independent widgets and overlap them on top of each other. I tried with pixmap, but it is not the best choice since I need pixel manipulation.
Evaluate mouse position/events in a QWidget. Draw the image on that QWidget either in a paintEvent or simply by placing QLabel with an image without margins/borders on that widget.
When the image is not scakled, you can calculate the pixel position having the mouse-position within the image.
I think the simplest way to start is to derive your widget from QLabel and overwrite required mouse events. The offset to the pixel is the contentsRect-position

Interactively editing an existing rectangle on a QPixmap?

I'm trying to creat a Dicom GUI Toolkit where the user selects some dicom images and the image of first dicom image from the selected ones will be shown. Then the user clicks on the image and the image pops out with bigger image window. In this shown bigger image, the image will consist of a red colored rectangle that contains necessary regions of the Dicom image while the unnecessary region is outside the rectangle. The user should then have the option to change the rectangle by mouse.
Until now, I have been able to show the big dicom image with the rectangle in it using QLabel which is by the following code snippets.
void MainWindow::showBigImage()
{
QPixmap bigimage;
bigimage.load(imageName.c_str());
QPainter painter(&bigimage);
painter.setPen(Qt::red);
QRectF rect(xmin, ymin, xmax, ymax);
painter.drawRect(rect);
QSize bigsize = ui->bigImageLabel->size();
ui->bigImageLabel->setPixmap(bigimage.scaled(bigsize, Qt::IgnoreAspectRatio, Qt::FastTransformation));
ui->bigImageLabel->show();
}
and the big image on the app looks like the following:
Can you please suggest me how I should now make the rectangle editable by the user where the user can set the existing red rectangle as per his or her wish?
I also tried similar thing using QGraphicsView and QGraphicsScene with the following code:
void MainWindow::showBigImage()
{
QGraphicsScene* scene = new QGraphicsScene;
scene->addPixmap(bigimage);
ui->bigImageView->setScene(scene);
ui->bigImageView->show();
}
And this code gives me the following look:
As you can see, I could not fit the image to the boundaries of QGraphicsView, could you suggest me how to do it? Could you also suggest me how to add the red rectangle(that I showed in the example using QLabel) on the QGraphicsView without adding the rectangle on the QPixmap?
In order to get the red selection rectangle, Qt provides the class QRubberBand. The docs state:
The QRubberBand class provides a rectangle or line that can indicate a selection or a boundary.
By subclassing the image object and implementing the mouse handling functions, to create the rubber band on mousePressEvent, update its position on mouseMoveEvent and grab its final rect on mouseReleaseEvent, the QRubberBand will simplify the problem.
If you want the QRubberBand to show all the time, just create it when you display the enlarged image and don't hide it on releasing the mouse button.
As for displaying the image in the QGraphicsView, the code you displayed doesn't set the geometry of the QGraphicsScene and QGraphicsView, so you're seeing a border. If you don't want that, you should set them accordingly. Also note that QGraphicsView has a function fitInView, which you could use, after having retrieved an area from the QRubberBand, in order to zoom into the selected area.

Drawing a scalable QIcon using QPainter

I want to design a new QIcon and want it to look like a fixed text with a rounded rectangle around it
.-----.
| Phy |
`-----ยด
The icon is supposed to scale without the "pixel-blocks" effect when painting on a QPainter that eventually has a scale transformation applied (for example when I paint into a widget that is part of a QGraphicsView with a scale applied on its scene).
Therefor, I have difficulties knowing how I should paint my QIcon. If I do it in the following way, I will paint a QPixmap that always has a fixed amount of pixels, thus introducing the pixel-blocks effect inevitably when the scale is large enough
void MyWidget::drawIcon(QPainter *painter, QPoint pos) {
QPixmap pixmap = icon.pixmap(QSize(22, 22),
isEnabled() ? QIcon::Normal
: QIcon::Disabled,
isChecked() ? QIcon::On
: QIcon::Off);
painter->drawPixmap(pos, pixmap);
}
What I am looking for is a way similar to how QFont with drawText works. Regardless on how large my scale is, when I draw fonts it always looks sharp and I cannot detect individual pixels.
I imagine that I could tell QPainter to paint my icon into a given pixel rectangle, and QPainter transforms the rectangle itself before letting my QIconEngine::paint render the item into a possibly larger rectangle or pixmap. But I see no way how I could do something like this.
Am I just being stupid and not seeing the obvious solution?
I was indeed completely dump. I can just use QIcon::paint and pass it the rectangle. It will correctly delegate the request to the icon engine.
I do this by creating my icons/images as SVG files, and using QSvgRenderer to paint them onto a QPainter. The required classes are in the SVG module.