I want to set pixmap to my QLabel. Problem is , not whole image is displayed in the label, i.e only small part of the image is displayed. Here is the code :
QPixmap pix("c:\\images\\myimg.png"));
mLabel->setPixmap(pix);
What can be the problem
Try setting pixmap size to label size
mLabel->setFixedSize(pix.size());
If you want to keep aspect ratio of the image you must do that:
QPixmap pix("c:\\images\\myimg.png");
pix = pix.scaled(QSize(mLabel->width(),mLabel->height()), Qt::KeepAspectRatio);
mLabel->resize(pix.size());
mLabel->setPixmap(pix);
If you don't want to keep aspect ratio of the image you must do that:
QPixmap pix("c:\\images\\myimg.png");
pix = pix.scaled(QSize(mLabel->width(),mLabel->height()), Qt::IgnoreAspectRatio);
mLabel->setPixmap(pix);
The Label was not added in layout. After adding it, it works.
Related
I am write a paint program.
pix = QPixmap(600,500); // set size to 600X500
How to change size after this? Someting like:
pix.setSize(800,600); // Change size to 800X600
I think, QPixmap::scaled is what you need.
Returns a copy of the pixmap scaled to a rectangle with the given width and height according to the given aspectRatioMode and transformMode.
Earlier last week I created a window in Python that resized the main window to the dimensions of the background image. I wanted to do the same in QT. I've managed to figure out the syntax to resize the main window through its constructor.
this->setFixedSize(QSize(600, 600));
I'm curious how I might now set the width and height parameters to the same parameters of an image in the resource file. I was thinking something like this:
QGraphicsPixmapItem image(QPixmap("url(:/images/background.png);"));
int x = image.width
int y = image.length
this->setFixedSize(QSize(x, y));
edit:
In summary, I want to resize the main window to the same dimensions of an image file which so the window wraps around the background image. According to this post I have to parse the image header to read the dimensions... sounds too complicated. Is there a third party library?
Instead of using QGraphicsPixmapItem use QImage to get the size of your image.
QImage image(":/images/background.png");
if(!image.isNull())
setFixedSize(image.size());
else
//loading the image failed, show some error message or something
Have you tried to adjustSize , after setting to new ?
this->setFixedSize(QSize(x, y));
this->adjustSize();
I have a QImage and a QPainter that paints on this image. After painting I tried to insert this QImage to QTextDocument with QTextCursor's insertImage method :
QImage image(width ,height,QImage::Format_RGB32);
QPainter paint;
paint.begin(&image);
paint.drawText(25,25,someText);
paint.end();
QTextCursor cursor(doc);
cursor.movePosition(QTextCursor::End);
cursor.insertImage(image);
But after doing this what I got is a text with a low resolution or a line width saggy pixels like in this image :
!(http://imgim.com/4698inciz7774617.png)line image
I tried setting QTextDocument's layout's paint device but it gave an error because of null pointer, this is most probably because of the document does not have a layout
I tried setting render hints of painter but it does not work too.
I inserted some html before the image in the document but they are good in terms of resolution, also there are no problem in flat lines.
When I show this document in a QTextEdit it seems fine, but when this is in a pdf file in a preview or something something goes wrong.
My printer is defined like that in preview :
QPrinter printer(QPrinter::HighResolution);
QPrinter highprinter(QPrinter::ScreenResolution);
printer.setPaperSize(QPrinter::A4);
And I call QTextDocument's print method for printing.
Do you have any solutions for this?
thanks
Saggy pixels can be eliminated somehow with : QPainter::setRenderHint(QPainter::Antialiasing, true); however it seems it does not eliminate saggy pixels in letters very much.
Other way of eliminating saggy pixels fully is creating a document with large pixel sizes (resolution) and increasing size of the image.After that we can increase font point size and line widths it prevents aliasing as I saw in my trials.
//increasing line width when drawing line
paint.setPen(QPen(Qt::gray,20, Qt::SolidLine, Qt::RoundCap, Qt::MiterJoin));
//increasing font's point size when using text
qFont2.setPointSize(100);
paint.setFont(qFont2);
However increasing font point size does not prevent aliasing when the image's pixel size (resolution) remains same
I have a qt label which by default has a place holder image in it:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("place_holder.jpg")))
There is a function to update the image contained in the label which is:
def selectFile(self):
image = QtGui.QFileDialog.getOpenFileName(None, 'Select Reference Image', '', '*.jpg')
self.label.setPixmap(QtGui.QPixmap(_fromUtf8(image)))
This works fine (the image is updated), but it is also deformed if the image used to update the label has different size from the place holder image.
Is there any way to fix this? I mean to adapt the image and to keep fix the size of the label?
You can try to set the scaledContents property:
self.label.setScaledContents(True)
self.label.setPixmap(QPixmap("your_image.jpeg"))
It works fine for me.
Beware that this scale the image to fill all available space. It means that the aspect ratio of the image wont be preserved unless the label size and the image have the same aspect ratio. You will have the same problem using the QPixmap.scale method as you can see in the images here.
Scale the image each time you import it. If you already have a size in mind for your label, you can even scale the place holder.
try (c++)
//initial setup
QPixmap pixPlaceHolder = QPixmap(_fromUtf8("place_holder.jpg");
QSize desiredsize = pixPlaceHolder.size(); // or any of the size you want
Each time you select a file:
label.setPixmap(QPixmap(_fromUtf8(image).scaled(desiredsize));
try this, this helped me for scaling the image to fit the label while maintaining the aspect ratio. Will also help to resize the image based on the label size.
pixmap = QPixmap(x)
pixmap = pixmap.scaled(self.label.size().width(),self.ui.label_4.size().height(), Qt.KeepAspectRatio)
self.label.setPixmap(QPixmap(pixmap))
I have two widgets, one arbitrary (usually a QLineEdit), and one QLabel which displays a Pixmap. They are placed next to each other with a QHBoxLayout. The widget with this layout is in turn placed in another layout.
Now, what I want is that the label with the pixmap is automatically resized so that it is as high as the arbitrary widget next to it. However, even when I set the label's sizePolicy to Maximum, it still seems to expand to the original pixmap size, instead of resizing the pixmap and shrinking to match the other widget. Instead of having two equally large widgets I have the arbitrary one which is smaller than the pixmap next to it.
Any ideas how to get the size of the pixmap label to match the size of the widget next to it?
How about:
int height = arbitraryWidget->height(); // get desired height.
label->setSizeHint(QSize(label->width(), height); // set size hint to current width and desired height.
label->setSizePolicy(QSizePolicy::Fixed); // optional, but this ensures size is desired.