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

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.

Related

Color the Image setting on QProgressBar

Actually I set an image on a QProgressBar and want to change the color of image according to selected part of QProgressBar.
I tried covering my image with QProgressBar color but that fills the image, but as my image is not a rectangle it color the whole QProgressBar and not just the image.
Is it possible to change color of image only using qt (with C++)
You are looking for Qt Style Sheet Documentation. You can customize your own QProgressBar, and add your own setProgress() function which should update the value property. You could modify the image background, the border, margins... depending of the value of the current value.
See QSS examples for more detail: Example Style Sheet

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

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.

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.

Adding/resizing a background image to centralWidget in Qt

I'm new to Qt. I searched for my question on multiple sites, but I couldn't find an answer. How can I add an image to centralWidget?
I tried:
MainWindow w;
w.centralWidget()->setStyleSheet("image: url(image)");
it worked fine but the image isn't stretched for the entire window/widget. How can I resize the image through setStyleSheet?
I want to display an image in the background and not the gray color, when the application opens. I tried changing the color. That worked, but it doesn't look good with buttons and labels.
If you want it to stretch for the entire widget use background-image, but I'm guessing what you probably want is an image that expands in one direction, either vertically or horizontally, in that case use background-position to fix it to one of the borders of your widget:
image: url(:/path/to/image);
background-position: bottom left;