resize qlabel with image in splitter
-
hello everyone,
I have a horizontal splitter with, on the top, a widget which containe a qlabel and on the bottom a tabwidget with an other widget.
in the qlabel i put an image.
I would like to be able to resize the qlabel and the pixmap inside with the splitter.
I tried to use a resize event with a qpixmap rescale. that function well for expande qlabel and compresse tabview but the reverse dont work. i dont arrive to compresse the qlabel.
if someone know how to manage that, i will be happy to learn about it
thank you in advance.
-
Hi
If you manually scale the pixmap up then there should be no reason it wont scale down ?
You are not showing any code, so hard to give suggestions but try to inspect the values used for scaling
when you try to "compress" it. -
yes sorry. here an exemple of code that reproduce my problem.
Header
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
CPP
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QImage I("path/of/an/image"); QPixmap pix = QPixmap::fromImage(I); ui->label->setPixmap(pix); } MainWindow::~MainWindow() { delete ui; }
UI
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>600</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <layout class="QGridLayout" name="gridLayout"> <item row="0" column="0"> <widget class="QSplitter" name="splitter"> <property name="orientation"> <enum>Qt::Vertical</enum> </property> <widget class="QLabel" name="label"> <property name="text"> <string>TextLabel</string> </property> </widget> <widget class="QTabWidget" name="tabWidget"> <widget class="QWidget" name="tab"> <attribute name="title"> <string>Tab 1</string> </attribute> </widget> <widget class="QWidget" name="tab_2"> <attribute name="title"> <string>Tab 2</string> </attribute> </widget> </widget> </widget> </item> </layout> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>21</height> </rect> </property> </widget> <widget class="QStatusBar" name="statusbar"/> </widget> <resources/> <connections/> </ui>
I didnt put the part with the resize event because that not work and... its normal because if the image dont allow qlabel to be compressed, the size will not change the event will not happen. i would like the compresse the qlabel with image when i use the splitter.
-
Hi
QLabel likes to expand on its own.
What you can do itvoid MainWindow::resizeEvent(QResizeEvent *) { QPixmap p(":/009408-rounded-glossy-black-icon-arrows-triangle-circle-down.png"); // load pixmap. maybe just do this once. int w = ui->label->width(); int h = ui->label->height(); // set a scaled pixmap to a w x h window keeping its aspect ratio ui->label->setPixmap(p.scaled(w,h,Qt::KeepAspectRatio)); }
and to allow it to be smaller, set its size policy to ignore
then it works pretty ok.