How to set QPixmap in a specific coordinates of QLabel? - c++

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)

Related

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

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.

QLabel rotation

I am having a label set from a pixmap as follow:
QLabel* label_image;
label_image= new QLabel (this);
label_image->setGeometry(0, 0, 500, 30);
QPixmap pm;
pm ...
label_image->setPixmap(pm);
I would like now to rotate it by 90 degrees. How to do so?
You have two options here. The first is to subclass QLabel and provide the rotation functionality that you require. Alternatively, you can use QTransform to rotate the QPixmap that you set on the QLabel.
Rather than regurgitating the answer, this link explains how to do the rotation and maintain the original size of the image.
Update due to invalid link...
Essentially, you can't rotate the actual label, but you can rotate the pixmap, then set this on the label widget
QPixmap pm;
...
QTransform trans;
trans.rotate(90);
label_image->setPixmap(pm.transformed(trans));
If you keep rotating the same image, it will distort, so ensure any rotation is always from a stored, non-rotated pixmap.

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.

How to set QLabel's size to that of the image in its pixmap?

I am using QT for my application, and ended up using QLabel to display image. However, it is displayed cropped.
How can the size of QLabel set to one of its pixmap? How can that be done in such a way that it would change if the pixmap changes?
// Call this function whenever you want to change the label's pixmap
void SetPixmapLabel(QLabel * myLabel, const QPixmap & pixmap)
{
myLabel->setPixmap(pixmap);
myLabel->setFixedSize(pixmap.size());
}