Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. resize qlabel with image in splitter
Forum Updated to NodeBB v4.3 + New Features

resize qlabel with image in splitter

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 723 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    superdu
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        superdu
        wrote on last edited by superdu
        #3

        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.

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi

          QLabel likes to expand on its own.
          What you can do it

          
          void 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
          alt text

          then it works pretty ok.

          alt text

          1 Reply Last reply
          3
          • S Offline
            S Offline
            superdu
            wrote on last edited by
            #5

            thank you so much. indeed that work for compression now.

            mrjjM 1 Reply Last reply
            0
            • S superdu

              thank you so much. indeed that work for compression now.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @superdu
              Super.
              Do note you can do
              ui->label->setPixmap(p.scaled(w,h,Qt::KeepAspectRatio, Qt::SmoothTransformation));
              to make it look better when scale up alot.

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved