QLabel color and separateur - qt

IplImage *img_lb = cvCloneImage(img_orig);
QImage qt_img = IplImage2QImage(img_lb);
QLabel label_3;
// display on label
ui->label_3->setPixmap(QPixmap::fromImage(qt_img));
// resize the label to fit the image
ui->label_3->resize(ui->label_3->pixmap()->size());
// ui->label_3->show();
`hi i use Qt creator i would like to play video in the size of declared size and not his size ,someone can help?this is my code

If you want your Image scaled to you Label size, Use
ui->label_3->setPixmap(QPixmap::fromImage(qt_img).scaled(ui->label_3->size(), Qt::IgnoreAspectRatio));
This would make your pixmap perfectly fit to the Label while ignoring the aspect ratio. For more look here.

Related

How to set fixed Icon size that scales with HiDPI in Qt

How can I set Icon size in my application so that it's still scaled when the users uses Screen Scaling?
In my application I have a QToolBar in the MainWindow that seems to use an Icon size of 24x24. I have some QToolButton which seem to get an Icon size of 20x20 by default, so I had to manually set it to 24x24 in order to have all Icons with same size, with setIconSize(QSize(24, 24));. Works fine without scaling:
When the Desktop has some scaling enabled, the Icons with the Fixed size don't get scaled, this looks then like this:
Another use case that I have is showing Icons in QLabels, there I have to specify the size when converting QIcon to QPixmap, this also doesn't scale mImageLabel->setPixmap(icon().pixmap(QSize(24, 24)));
Is there any better why then multiplying with the scale factor? How to get the scale factor?
It looks like setting the fixed size prevents any scaling from Qt side so we need to manually adjust the size. In my case it seems that I can get the correct scale factor by dividing the logicalDpi by 96, which is the DPI with scaling factor 1. I'm not sure if this is the best solution for all uses cases (haven't tested against MacOS for example) but fixes my use case.
I wrote a simple class that scales all my fixed size to the correct value:
#include <QApplication>
#include <QDesktopWidget>
QSize ScaledSizeProvider::getScaledSize(const QSize &size)
{
return {static_cast<int>(size.width() * getXScaleFactor()), static_cast<int>(size.height() * getYScaleFactor())};
}
qreal ScaledSizeProvider::getXScaleFactor()
{
auto desktopWidget = QApplication::desktop();
return desktopWidget->logicalDpiX() / getReferenceDpiValue();
}
qreal ScaledSizeProvider::getYScaleFactor()
{
auto desktopWidget = QApplication::desktop();
return desktopWidget->logicalDpiY() / getReferenceDpiValue();
}
qreal ScaledSizeProvider::getReferenceDpiValue()
{
return 96.0;
}
And the simple fetch the correct value with:
ScaledSizeProvider::getScaledSize(QSize(24, 24))

Fit QDialog window to size of text

I have a QDialog class that takes in a QString. I am calling setFixedSize with a set width and height but I want the QDialog to be more dynamic and fit to the size of the text.
I have tried adjustSize() but all that did was shrink the window to the point where the text was cut off.
ConfirmDialog::ConfirmDialog(const QString& message, QWidget* parent)
: QDialog(parent)
{
setFixedSize(WIDTH, HEIGHT);
statusLabel->setText(tr("Confirmation"));
statusDetailsLabel->setText(message);
statusDetailsLabel->setWordWrap(true);
}
I always see a Window with size of dimensions WIDTH and HEIGHT. I want it to fit the test.
One way would be to use Font Metrics to get the bounding rects of each label, and then set the window size to the sum of both rects + some padding to make it look nice.
One problem you will run into is having wordwrap on. How do you determine the width of the window, if you are word wrapping? So I've added a "MAXWIDTH" for the window. If your text is shorter and does not require word wrap - the window will shrink to fit it. If it does require word wrap, it will not go over your set size.
ConfirmDialog::ConfirmDialog(const QString& message, QWidget* parent)
: QDialog(parent)
{
const int MAXWIDTH = 400;
const int VERTICALPADDING = 50;
// Create Layout
QLabel *statusLabel = new QLabel(this);
QLabel *statusDetailsLabel = new QLabel(this);
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(statusLabel);
layout->addWidget(statusDetailsLabel);
setLayout(layout);
// Populate Text
statusLabel->setText(tr("Confirmation"));
statusDetailsLabel->setText(message);
statusDetailsLabel->setWordWrap(false); // Start w/ word wrap off.
// Font metrics to get the sizes of our text.
QFontMetrics fontMetricsLabel(statusLabel->font());
QFontMetrics fontMetricsDetail(statusDetailsLabel->font());
// Get max width - label or detail lable, whichever is longer.
int width = std::max(fontMetricsLabel.boundingRect("Confirmation").width(),
fontMetricsDetail.boundingRect(message).width());
// Check that we do not go over our MAXWIDTH.
if(width > MAXWIDTH) width = MAXWIDTH;
// Enable word wrapping.
statusDetailsLabel->setWordWrap(true);
// Get the heigts of both boxes.
int height = std::max(fontMetricsLabel.boundingRect("Confirmation").height(),
fontMetricsDetail.boundingRect(message).height());
// Set window size.
this->setFixedSize(width, height + VERTICALPADDING);
}

How to make size of button in the LineEdit smaller?

I made the X button in the LineEdit, when i click on this button, the LineEdit is clear. But with my method, the X button looks a little big and not beautiful, I need to make it smaller. How I can do it?
myLineEdit = new LineEdit;
myLineEdit->setFixedHeight( 25 );
m_clear = m_lineEdit->addAction( QIcon( ":/clearButton" ), QLineEdit::TrailingPosition );
the size of clearButton.png is 12x12 px, so in this case it is enlarged and looks not beautiful like this.
For this solution it is assumed that in the original image the relationship between the foreground size and the background is 1: 1 (this is normal in the icons), so the solution is to increase that relationship, for this we create a new image
QPixmap in(":/clearButton");
QPixmap out(in.size()*10/7);
QRect r= in.rect();
r.moveCenter(out.rect().center());
out.fill(Qt::transparent);
QPainter painter(&out);
painter.drawPixmap(r , in);
painter.end();
QLineEdit *m_lineEdit = new QLineEdit;
m_lineEdit->setFixedHeight(25);
m_lineEdit->addAction(QIcon(out), QLineEdit::TrailingPosition);
Before:
After:

Fill the QLabel/QGraphics widget placed in the grid with the image

I have placed two QGraphicsView's and one QLabel inside a horizontal layout (QHBoxLayout) with its layoutStretch set to 1, 1, 1. The problem is when I try to load images inside them, the images does not fill the widgets area. Here is the code:
QPixmap pix1("image1.jpg");
pix1 = pix1.scaled(ui->label1->size());
ui->label1->setPixmap(pix);
QPixmap pix2("image2.jpg");
pix2 = pix2.scaled(ui->graphicsView1->size());
ui->graphicsView1->scene()->addPixmap(pix2);
QPixmap pix3("image3.jpg");
pix3 = pix3.scaled(ui->graphicsView2->size());
ui->graphicsView2->scene()->addPixmap(pix3);
And here is the undesired output:
I have tried setting HorizontalPolicy and VerticalPolicy property of widget to Expanding and also Minimum, but none of them helped either.
Set size policy to QSizePolicy::Ignored and scale with Qt::KeepAspectRatioByExpanding:
ui->label1->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
pix1 = pix1.scaled(ui->label1->size(), Qt::KeepAspectRatioByExpanding);
ui->label1->setPixmap(pix);
You probably want to scale pixmaps in resizeEvent().

PyQt/Qt: How to stretch an image in Qlabel widget?

I want to display an image in my app. I use QtDesigner to design UI, then use pyqt to coding. The problem is the image that will be shown is lager than the widget size on the UI. I refer to offical demo:
QT - Widget Image Viewer Demo
add imagelabel and scrollArea, code as follows:
---- UI init ----
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(40, 140, 361, 511))
self.label.setSizePolicy(QtGui.QSizePolicy.Preferred,QtGui.QSizePolicy.Preferred)
self.label.setObjectName(_fromUtf8("label"))
self.scrollArea = QtGui.QScrollArea(self.centralwidget)
self.scrollArea.setGeometry(QtCore.QRect(40, 140, 361, 511))
self.scrollArea.setWidget(self.label)
self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
---- function ----
filename = "./Penguins.jpg"
image = QtGui.QImage(filename)
pp = QtGui.QPixmap.fromImage(image)
lbl = QtGui.QLabel(self.label)
lbl.setPixmap(pp)
self.scrollArea.setWidgetResizable(True)
lbl.show()
but it doesn't stretch the image, even no scroll bar appear!
You need to call self.label.setScaledContents(true);. So that QLabel will resize itself to the size of pixmap/image and scroll-bar will get visible. See this documentation.
The default implementation of QLabel::setScaledContents wasn't working for me, since it didn't allow me to keep the aspect ratio when the images where larger then the label's
maximum sizes.
This little helper will scale the image down to fit into a label's maximum size if needed (but not up), always keeping the aspect ratio:
/**
* Fill a QLabel widget with an image file, respecting the widget's maximum sizes,
* while scaling the image down if needed (but not up), and keeping the aspect ratio
* Returns false if image loading failed
****************************************************************************/
static bool SetLabelImage(QLabel *label, QString imageFileName)
{
QPixmap pixmap(imageFileName);
if (pixmap.isNull()) return false;
int w = std::min(pixmap.width(), label->maximumWidth());
int h = std::min(pixmap.height(), label->maximumHeight());
pixmap = pixmap.scaled(QSize(w, h), Qt::KeepAspectRatio, Qt::SmoothTransformation);
label->setPixmap(pixmap);
return true;
}
I do not use PyQt but the QtPixmap control has scaled() functions. You can resize the image before put in the label:
scaled()
scaledToHeight()
scaledToWidth()
This is the sample code I use in C++ to resize an image to the QLabel size:
imatge.load("sprite.png");
QPixmap imatge2 = imatge.scaled(ui->label->width(),ui->label->height());

Resources