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. QLabel size won't expand even after setting sizepolicy to expanding
Forum Updated to NodeBB v4.3 + New Features

QLabel size won't expand even after setting sizepolicy to expanding

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 5 Posters 22.5k Views 2 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.
  • M magicstar

    This post is deleted!

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

    @magicstar
    In your first post, in Topic Tools button, there is menu to say solved.

    1 Reply Last reply
    1
    • VRoninV VRonin

      The viewport of the scroll area is not a layout so it can't act like one.
      To give you the intuition: the viewport is (theoretically) infinite in width and height so when you say "expanding" where does the label stop?
      The scroll area does not use the size policy for this reason. The size you get on the first show is simply ui->label->setFixedSize(ui->label->sizeHint());

      M Offline
      M Offline
      magicstar
      wrote on last edited by
      #7

      @VRonin ,
      It seems I implemented in wrong way. I am newbie and I really don't have much knowledge of QT. I see QT forum flooded with qlabel resizing problem. But, there is hardly any working solution.

      Can you please tell how should be size policy of qlabel, scrollarea and scrollarea widget and how can I switch between two different views:

      1. display as it is
      2. display to fit to content
      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #8

        Scroll area does not lay out the contents of the viewport. The sizing is left entirely to the user.
        Make sure you have ui->label->setScaledContents(true); somewhere (in the constructor or set in designer directly)

        • resize label to the image size: ui->label->resize(ui->label->pixmap()->size());
        • resize label to 1024x768: ui->label->resize(1024,768);

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2
        • M Offline
          M Offline
          magicstar
          wrote on last edited by VRonin
          #9

          Thank you so much for your help so far. I modified code as per your suggestion
          here is my new dialog.ui file

          <?xml version="1.0" encoding="UTF-8"?>
          <ui version="4.0">
           <class>Dialog</class>
           <widget class="QDialog" name="Dialog">
            <property name="geometry">
             <rect>
              <x>0</x>
              <y>0</y>
              <width>400</width>
              <height>300</height>
             </rect>
            </property>
            <property name="windowTitle">
             <string>Dialog</string>
            </property>
            <layout class="QVBoxLayout" name="verticalLayout">
             <item>
              <widget class="QScrollArea" name="scrollArea">
               <property name="sizeAdjustPolicy">
                <enum>QAbstractScrollArea::AdjustToContents</enum>
               </property>
               **<property name="widgetResizable">
                <bool>true</bool>**
               </property>
               <widget class="QWidget" name="scrollAreaWidgetContents">
                <property name="geometry">
                 <rect>
                  <x>0</x>
                  <y>0</y>
                  <width>380</width>
                  <height>224</height>
                 </rect>
                </property>
                <property name="sizePolicy">
                 **<sizepolicy hsizetype="Expanding" vsizetype="Expanding">**
                  <horstretch>0</horstretch>
                  <verstretch>0</verstretch>
                 </sizepolicy>
                </property>
                **<layout class="QGridLayout" name="gridLayout">
                 <property name="sizeConstraint">
                  <enum>QLayout::SetNoConstraint</enum>**
                 </property>
                 <item row="0" column="0">
                  **<widget class="QLabel" name="label">
                   <property name="sizePolicy">
                    <sizepolicy hsizetype="Ignored" vsizetype="Ignored">**
                     <horstretch>0</horstretch>
                     <verstretch>0</verstretch>
                    </sizepolicy>
                   </property>
                   <property name="text">
                    <string/>
                   </property>
                   **<property name="scaledContents">
                    <bool>true</bool>**
                   </property>
                  </widget>
                 </item>
                </layout>
               </widget>
              </widget>
             </item>
             <item>
              <widget class="QSlider" name="horizontalSlider">
               <property name="orientation">
                <enum>Qt::Horizontal</enum>
               </property>
              </widget>
             </item>
             <item>
              <layout class="QHBoxLayout" name="horizontalLayout">
               <item>
                <widget class="QPushButton" name="pushButton_fixsize">
                 <property name="text">
                  <string>Fix Size</string>
                 </property>
                </widget>
               </item>
               <item>
                <widget class="QPushButton" name="pushButton_expandsize">
                 <property name="text">
                  <string>Expand Size</string>
                 </property>
                </widget>
               </item>
               <item>
                <widget class="QPushButton" name="pushButton_openimg">
                 <property name="text">
                  <string>Open Image</string>
                 </property>
                </widget>
               </item>
              </layout>
             </item>
            </layout>
           </widget>
           <layoutdefault spacing="6" margin="11"/>
           <resources/>
           <connections/>
          </ui>
          

          And here is dialog.cpp

          #include "dialog.h"
          #include "ui_dialog.h"
          #include <QFileDialog>
          
          Dialog::Dialog(QWidget *parent) :
              QDialog(parent),
              ui(new Ui::Dialog)
          {
              ui->setupUi(this);
          }
          
          Dialog::~Dialog()
          {
              delete ui;
          }
          
          void Dialog::on_pushButton_fixsize_clicked()
          {
          }
          
          void Dialog::on_pushButton_expandsize_clicked()
          {
          }
          
          void Dialog::on_pushButton_openimg_clicked()
          {
              QString ImagePath = QFileDialog::getOpenFileName(
                          this,tr("Open Image"),"","Images (*.png *.xpm *.jpg)");
              QPixmap wPixmap(ImagePath);
              ui->label->setPixmap(wPixmap);
              ui->label->resize(ui->label->pixmap()->size());
          
          }
          

          The problem is, scrollbars won't appear even if size is bigger. And the image would always fit to label-size.

          May I please know what is wrong? I am struggling very badly.

          jsulmJ VRoninV 2 Replies Last reply
          0
          • M magicstar

            Thank you so much for your help so far. I modified code as per your suggestion
            here is my new dialog.ui file

            <?xml version="1.0" encoding="UTF-8"?>
            <ui version="4.0">
             <class>Dialog</class>
             <widget class="QDialog" name="Dialog">
              <property name="geometry">
               <rect>
                <x>0</x>
                <y>0</y>
                <width>400</width>
                <height>300</height>
               </rect>
              </property>
              <property name="windowTitle">
               <string>Dialog</string>
              </property>
              <layout class="QVBoxLayout" name="verticalLayout">
               <item>
                <widget class="QScrollArea" name="scrollArea">
                 <property name="sizeAdjustPolicy">
                  <enum>QAbstractScrollArea::AdjustToContents</enum>
                 </property>
                 **<property name="widgetResizable">
                  <bool>true</bool>**
                 </property>
                 <widget class="QWidget" name="scrollAreaWidgetContents">
                  <property name="geometry">
                   <rect>
                    <x>0</x>
                    <y>0</y>
                    <width>380</width>
                    <height>224</height>
                   </rect>
                  </property>
                  <property name="sizePolicy">
                   **<sizepolicy hsizetype="Expanding" vsizetype="Expanding">**
                    <horstretch>0</horstretch>
                    <verstretch>0</verstretch>
                   </sizepolicy>
                  </property>
                  **<layout class="QGridLayout" name="gridLayout">
                   <property name="sizeConstraint">
                    <enum>QLayout::SetNoConstraint</enum>**
                   </property>
                   <item row="0" column="0">
                    **<widget class="QLabel" name="label">
                     <property name="sizePolicy">
                      <sizepolicy hsizetype="Ignored" vsizetype="Ignored">**
                       <horstretch>0</horstretch>
                       <verstretch>0</verstretch>
                      </sizepolicy>
                     </property>
                     <property name="text">
                      <string/>
                     </property>
                     **<property name="scaledContents">
                      <bool>true</bool>**
                     </property>
                    </widget>
                   </item>
                  </layout>
                 </widget>
                </widget>
               </item>
               <item>
                <widget class="QSlider" name="horizontalSlider">
                 <property name="orientation">
                  <enum>Qt::Horizontal</enum>
                 </property>
                </widget>
               </item>
               <item>
                <layout class="QHBoxLayout" name="horizontalLayout">
                 <item>
                  <widget class="QPushButton" name="pushButton_fixsize">
                   <property name="text">
                    <string>Fix Size</string>
                   </property>
                  </widget>
                 </item>
                 <item>
                  <widget class="QPushButton" name="pushButton_expandsize">
                   <property name="text">
                    <string>Expand Size</string>
                   </property>
                  </widget>
                 </item>
                 <item>
                  <widget class="QPushButton" name="pushButton_openimg">
                   <property name="text">
                    <string>Open Image</string>
                   </property>
                  </widget>
                 </item>
                </layout>
               </item>
              </layout>
             </widget>
             <layoutdefault spacing="6" margin="11"/>
             <resources/>
             <connections/>
            </ui>
            

            And here is dialog.cpp

            #include "dialog.h"
            #include "ui_dialog.h"
            #include <QFileDialog>
            
            Dialog::Dialog(QWidget *parent) :
                QDialog(parent),
                ui(new Ui::Dialog)
            {
                ui->setupUi(this);
            }
            
            Dialog::~Dialog()
            {
                delete ui;
            }
            
            void Dialog::on_pushButton_fixsize_clicked()
            {
            }
            
            void Dialog::on_pushButton_expandsize_clicked()
            {
            }
            
            void Dialog::on_pushButton_openimg_clicked()
            {
                QString ImagePath = QFileDialog::getOpenFileName(
                            this,tr("Open Image"),"","Images (*.png *.xpm *.jpg)");
                QPixmap wPixmap(ImagePath);
                ui->label->setPixmap(wPixmap);
                ui->label->resize(ui->label->pixmap()->size());
            
            }
            

            The problem is, scrollbars won't appear even if size is bigger. And the image would always fit to label-size.

            May I please know what is wrong? I am struggling very badly.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by VRonin
            #10

            @magicstar said in QLabel size won't expand even after setting sizepolicy to expanding:

            ui->label->resize(ui->label->pixmap()->size());

            I would use

            ui->label->resize(wPixmap.size());
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • M magicstar

              Thank you so much for your help so far. I modified code as per your suggestion
              here is my new dialog.ui file

              <?xml version="1.0" encoding="UTF-8"?>
              <ui version="4.0">
               <class>Dialog</class>
               <widget class="QDialog" name="Dialog">
                <property name="geometry">
                 <rect>
                  <x>0</x>
                  <y>0</y>
                  <width>400</width>
                  <height>300</height>
                 </rect>
                </property>
                <property name="windowTitle">
                 <string>Dialog</string>
                </property>
                <layout class="QVBoxLayout" name="verticalLayout">
                 <item>
                  <widget class="QScrollArea" name="scrollArea">
                   <property name="sizeAdjustPolicy">
                    <enum>QAbstractScrollArea::AdjustToContents</enum>
                   </property>
                   **<property name="widgetResizable">
                    <bool>true</bool>**
                   </property>
                   <widget class="QWidget" name="scrollAreaWidgetContents">
                    <property name="geometry">
                     <rect>
                      <x>0</x>
                      <y>0</y>
                      <width>380</width>
                      <height>224</height>
                     </rect>
                    </property>
                    <property name="sizePolicy">
                     **<sizepolicy hsizetype="Expanding" vsizetype="Expanding">**
                      <horstretch>0</horstretch>
                      <verstretch>0</verstretch>
                     </sizepolicy>
                    </property>
                    **<layout class="QGridLayout" name="gridLayout">
                     <property name="sizeConstraint">
                      <enum>QLayout::SetNoConstraint</enum>**
                     </property>
                     <item row="0" column="0">
                      **<widget class="QLabel" name="label">
                       <property name="sizePolicy">
                        <sizepolicy hsizetype="Ignored" vsizetype="Ignored">**
                         <horstretch>0</horstretch>
                         <verstretch>0</verstretch>
                        </sizepolicy>
                       </property>
                       <property name="text">
                        <string/>
                       </property>
                       **<property name="scaledContents">
                        <bool>true</bool>**
                       </property>
                      </widget>
                     </item>
                    </layout>
                   </widget>
                  </widget>
                 </item>
                 <item>
                  <widget class="QSlider" name="horizontalSlider">
                   <property name="orientation">
                    <enum>Qt::Horizontal</enum>
                   </property>
                  </widget>
                 </item>
                 <item>
                  <layout class="QHBoxLayout" name="horizontalLayout">
                   <item>
                    <widget class="QPushButton" name="pushButton_fixsize">
                     <property name="text">
                      <string>Fix Size</string>
                     </property>
                    </widget>
                   </item>
                   <item>
                    <widget class="QPushButton" name="pushButton_expandsize">
                     <property name="text">
                      <string>Expand Size</string>
                     </property>
                    </widget>
                   </item>
                   <item>
                    <widget class="QPushButton" name="pushButton_openimg">
                     <property name="text">
                      <string>Open Image</string>
                     </property>
                    </widget>
                   </item>
                  </layout>
                 </item>
                </layout>
               </widget>
               <layoutdefault spacing="6" margin="11"/>
               <resources/>
               <connections/>
              </ui>
              

              And here is dialog.cpp

              #include "dialog.h"
              #include "ui_dialog.h"
              #include <QFileDialog>
              
              Dialog::Dialog(QWidget *parent) :
                  QDialog(parent),
                  ui(new Ui::Dialog)
              {
                  ui->setupUi(this);
              }
              
              Dialog::~Dialog()
              {
                  delete ui;
              }
              
              void Dialog::on_pushButton_fixsize_clicked()
              {
              }
              
              void Dialog::on_pushButton_expandsize_clicked()
              {
              }
              
              void Dialog::on_pushButton_openimg_clicked()
              {
                  QString ImagePath = QFileDialog::getOpenFileName(
                              this,tr("Open Image"),"","Images (*.png *.xpm *.jpg)");
                  QPixmap wPixmap(ImagePath);
                  ui->label->setPixmap(wPixmap);
                  ui->label->resize(ui->label->pixmap()->size());
              
              }
              

              The problem is, scrollbars won't appear even if size is bigger. And the image would always fit to label-size.

              May I please know what is wrong? I am struggling very badly.

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #11

              @magicstar said in QLabel size won't expand even after setting sizepolicy to expanding:

              scrollbars won't appear even if size is bigger.

              That's what you are telling it to do with:

              <property name="sizeAdjustPolicy">
                    <enum>QAbstractScrollArea::AdjustToContents</enum>
                   </property>
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              M 1 Reply Last reply
              2
              • VRoninV VRonin

                @magicstar said in QLabel size won't expand even after setting sizepolicy to expanding:

                scrollbars won't appear even if size is bigger.

                That's what you are telling it to do with:

                <property name="sizeAdjustPolicy">
                      <enum>QAbstractScrollArea::AdjustToContents</enum>
                     </property>
                
                M Offline
                M Offline
                magicstar
                wrote on last edited by magicstar
                #12

                @VRonin ,
                I am setting all the parameters in QT designer. I tried with all 3 QAbstractScrollArea policies,
                AdjustIgnored, Adjusttocontents, AdjustToContentsOnFirstShow. None Worked.

                I also tried, approach suggested by @jsulm, It did not work either.
                QString ImagePath = QFileDialog::getOpenFileName(this,tr("Open Image"),"","Images (*.png *.xpm *.jpg)");
                QPixmap wPixmap(ImagePath);
                ui->label->resize(wPixmap.size());
                ui->label->setPixmap(wPixmap);

                It would simply fit to screen

                I do feel the problem is somewhere with correct size policy. But, many people like me seems struggling with this.

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #13

                  Working example:

                  #include <QApplication>
                  #include <QScrollArea>
                  #include <QLabel>
                  #include <QGridLayout>
                  #include <QFileDialog>
                  #include <QPushButton>
                  int main(int argc, char *argv[])
                  {
                      QApplication a(argc, argv);
                      QWidget mainWid;
                      QGridLayout* mainLay=new QGridLayout(&mainWid);
                      QPixmap labelPix(200,200);
                      labelPix.fill(Qt::blue);
                      QLabel* imageLab = new QLabel(&mainWid);
                      imageLab->setPixmap(labelPix);
                      QScrollArea* imageScroll = new QScrollArea(&mainWid);
                      imageScroll->setWidget(imageLab);
                      QPushButton* bigButton =new QPushButton("Big",&mainWid);
                      QPushButton* normalButton =new QPushButton("Normal",&mainWid);
                      QPushButton* smallButton =new QPushButton("Small",&mainWid);
                      QPushButton* browseButton =new QPushButton("Browse Image",&mainWid);
                      QObject::connect(browseButton,&QPushButton::clicked,[imageLab,&mainWid,&labelPix]()->void{
                          const QString selectedImage = QFileDialog::getOpenFileName(&mainWid, "Open Image",QString(),"Images (*.png *.xpm *.jpg)");
                          if(!selectedImage.isEmpty()){
                              labelPix = QPixmap(selectedImage);
                              imageLab->setPixmap(labelPix);
                              imageLab->resize(imageLab->pixmap()->size());
                          }
                      });
                      QObject::connect(bigButton,&QPushButton::clicked,[imageLab,&labelPix]()->void{
                          imageLab->setPixmap(labelPix.scaled(labelPix.size()*2));
                          imageLab->resize(imageLab->pixmap()->size());
                      });
                      QObject::connect(smallButton,&QPushButton::clicked,[imageLab,&labelPix]()->void{
                          imageLab->setPixmap(labelPix.scaled(labelPix.size()/2));
                          imageLab->resize(imageLab->pixmap()->size());
                      });
                      QObject::connect(normalButton,&QPushButton::clicked,[imageLab,&labelPix]()->void{
                          imageLab->setPixmap(labelPix);
                          imageLab->resize(imageLab->pixmap()->size());
                      });
                      mainLay->addWidget(browseButton,0,0);
                      mainLay->addWidget(bigButton,0,1);
                      mainLay->addWidget(normalButton,0,2);
                      mainLay->addWidget(smallButton,0,3);
                      mainLay->addWidget(imageScroll,1,0,1,4);
                      mainWid.show();
                      return a.exec();
                  }
                  

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  M 1 Reply Last reply
                  2
                  • VRoninV VRonin

                    Working example:

                    #include <QApplication>
                    #include <QScrollArea>
                    #include <QLabel>
                    #include <QGridLayout>
                    #include <QFileDialog>
                    #include <QPushButton>
                    int main(int argc, char *argv[])
                    {
                        QApplication a(argc, argv);
                        QWidget mainWid;
                        QGridLayout* mainLay=new QGridLayout(&mainWid);
                        QPixmap labelPix(200,200);
                        labelPix.fill(Qt::blue);
                        QLabel* imageLab = new QLabel(&mainWid);
                        imageLab->setPixmap(labelPix);
                        QScrollArea* imageScroll = new QScrollArea(&mainWid);
                        imageScroll->setWidget(imageLab);
                        QPushButton* bigButton =new QPushButton("Big",&mainWid);
                        QPushButton* normalButton =new QPushButton("Normal",&mainWid);
                        QPushButton* smallButton =new QPushButton("Small",&mainWid);
                        QPushButton* browseButton =new QPushButton("Browse Image",&mainWid);
                        QObject::connect(browseButton,&QPushButton::clicked,[imageLab,&mainWid,&labelPix]()->void{
                            const QString selectedImage = QFileDialog::getOpenFileName(&mainWid, "Open Image",QString(),"Images (*.png *.xpm *.jpg)");
                            if(!selectedImage.isEmpty()){
                                labelPix = QPixmap(selectedImage);
                                imageLab->setPixmap(labelPix);
                                imageLab->resize(imageLab->pixmap()->size());
                            }
                        });
                        QObject::connect(bigButton,&QPushButton::clicked,[imageLab,&labelPix]()->void{
                            imageLab->setPixmap(labelPix.scaled(labelPix.size()*2));
                            imageLab->resize(imageLab->pixmap()->size());
                        });
                        QObject::connect(smallButton,&QPushButton::clicked,[imageLab,&labelPix]()->void{
                            imageLab->setPixmap(labelPix.scaled(labelPix.size()/2));
                            imageLab->resize(imageLab->pixmap()->size());
                        });
                        QObject::connect(normalButton,&QPushButton::clicked,[imageLab,&labelPix]()->void{
                            imageLab->setPixmap(labelPix);
                            imageLab->resize(imageLab->pixmap()->size());
                        });
                        mainLay->addWidget(browseButton,0,0);
                        mainLay->addWidget(bigButton,0,1);
                        mainLay->addWidget(normalButton,0,2);
                        mainLay->addWidget(smallButton,0,3);
                        mainLay->addWidget(imageScroll,1,0,1,4);
                        mainWid.show();
                        return a.exec();
                    }
                    
                    M Offline
                    M Offline
                    magicstar
                    wrote on last edited by
                    #14

                    @VRonin ,
                    Thank you so much for your help so far. These example does work and so does the image viewer example provided by QT. However, I have designed by dialog UI in QT designer. And Hence, wish to know how to achieve same results through QT designer. What are default size policies when we create above widgets programmatically. I let QT designer set default size policies. But, so far I haven't got desired output

                    VRoninV 1 Reply Last reply
                    0
                    • M magicstar

                      @VRonin ,
                      Thank you so much for your help so far. These example does work and so does the image viewer example provided by QT. However, I have designed by dialog UI in QT designer. And Hence, wish to know how to achieve same results through QT designer. What are default size policies when we create above widgets programmatically. I let QT designer set default size policies. But, so far I haven't got desired output

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by VRonin
                      #15

                      @magicstar said in QLabel size won't expand even after setting sizepolicy to expanding:

                      However, I have designed by dialog UI in QT designer

                      There's absolutely no difference:
                      widget.cpp

                      #include <QWidget>
                      
                      #include "ui_widget.h"
                      
                      
                      class Widget : public QWidget
                      {
                          Q_OBJECT
                          Q_DISABLE_COPY(Widget)
                      public:
                          explicit Widget::Widget(QWidget *parent=Q_NULLPTR) :
                              QWidget(parent)
                              ,ui(new Ui::Widget)
                            ,labelPix(200,200)
                          {
                              ui->setupUi(this);
                              labelPix.fill(Qt::blue);
                              ui->label->setPixmap(labelPix);
                              QObject::connect(ui->bigButton,&QPushButton::clicked,[this]()->void{
                                  ui->label->setPixmap(labelPix.scaled(labelPix.size()*2));
                                  ui->label->resize(ui->label->pixmap()->size());
                              });
                              QObject::connect(ui->smallButton,&QPushButton::clicked,[this]()->void{
                                  ui->label->setPixmap(labelPix.scaled(labelPix.size()/2));
                                  ui->label->resize(ui->label->pixmap()->size());
                              });
                              QObject::connect(ui->normalButton,&QPushButton::clicked,[this]()->void{
                                  ui->label->setPixmap(labelPix);
                                  ui->label->resize(ui->label->pixmap()->size());
                              });
                          }
                      
                          Widget::~Widget()
                          {
                              delete ui;
                          }
                      
                      private:
                          Ui::Widget *ui;
                          QPixmap labelPix;
                      };
                      

                      widget.ui

                      <?xml version="1.0" encoding="UTF-8"?>
                      <ui version="4.0">
                       <class>Widget</class>
                       <widget class="QWidget" name="Widget">
                        <property name="geometry">
                         <rect>
                          <x>0</x>
                          <y>0</y>
                          <width>400</width>
                          <height>300</height>
                         </rect>
                        </property>
                        <property name="windowTitle">
                         <string>Widget</string>
                        </property>
                        <layout class="QGridLayout" name="gridLayout">
                         <item row="0" column="0">
                          <widget class="QPushButton" name="bigButton">
                           <property name="text">
                            <string>Big</string>
                           </property>
                          </widget>
                         </item>
                         <item row="0" column="1">
                          <widget class="QPushButton" name="normalButton">
                           <property name="text">
                            <string>Normal</string>
                           </property>
                          </widget>
                         </item>
                         <item row="0" column="2">
                          <widget class="QPushButton" name="smallButton">
                           <property name="text">
                            <string>Small</string>
                           </property>
                          </widget>
                         </item>
                         <item row="1" column="0" colspan="3">
                          <widget class="QScrollArea" name="scrollArea">
                           <property name="widgetResizable">
                            <bool>true</bool>
                           </property>
                           <widget class="QWidget" name="scrollAreaWidgetContents">
                            <property name="geometry">
                             <rect>
                              <x>0</x>
                              <y>0</y>
                              <width>380</width>
                              <height>251</height>
                             </rect>
                            </property>
                            <layout class="QHBoxLayout" name="horizontalLayout">
                             <property name="spacing">
                              <number>0</number>
                             </property>
                             <property name="leftMargin">
                              <number>0</number>
                             </property>
                             <property name="topMargin">
                              <number>0</number>
                             </property>
                             <property name="rightMargin">
                              <number>0</number>
                             </property>
                             <property name="bottomMargin">
                              <number>0</number>
                             </property>
                             <item>
                              <widget class="QLabel" name="label">
                               <property name="text">
                                <string/>
                               </property>
                               <property name="alignment">
                                <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
                               </property>
                              </widget>
                             </item>
                            </layout>
                           </widget>
                          </widget>
                         </item>
                        </layout>
                       </widget>
                       <layoutdefault spacing="6" margin="11"/>
                       <resources/>
                       <connections/>
                      </ui>
                      

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      M 1 Reply Last reply
                      3
                      • VRoninV VRonin

                        @magicstar said in QLabel size won't expand even after setting sizepolicy to expanding:

                        However, I have designed by dialog UI in QT designer

                        There's absolutely no difference:
                        widget.cpp

                        #include <QWidget>
                        
                        #include "ui_widget.h"
                        
                        
                        class Widget : public QWidget
                        {
                            Q_OBJECT
                            Q_DISABLE_COPY(Widget)
                        public:
                            explicit Widget::Widget(QWidget *parent=Q_NULLPTR) :
                                QWidget(parent)
                                ,ui(new Ui::Widget)
                              ,labelPix(200,200)
                            {
                                ui->setupUi(this);
                                labelPix.fill(Qt::blue);
                                ui->label->setPixmap(labelPix);
                                QObject::connect(ui->bigButton,&QPushButton::clicked,[this]()->void{
                                    ui->label->setPixmap(labelPix.scaled(labelPix.size()*2));
                                    ui->label->resize(ui->label->pixmap()->size());
                                });
                                QObject::connect(ui->smallButton,&QPushButton::clicked,[this]()->void{
                                    ui->label->setPixmap(labelPix.scaled(labelPix.size()/2));
                                    ui->label->resize(ui->label->pixmap()->size());
                                });
                                QObject::connect(ui->normalButton,&QPushButton::clicked,[this]()->void{
                                    ui->label->setPixmap(labelPix);
                                    ui->label->resize(ui->label->pixmap()->size());
                                });
                            }
                        
                            Widget::~Widget()
                            {
                                delete ui;
                            }
                        
                        private:
                            Ui::Widget *ui;
                            QPixmap labelPix;
                        };
                        

                        widget.ui

                        <?xml version="1.0" encoding="UTF-8"?>
                        <ui version="4.0">
                         <class>Widget</class>
                         <widget class="QWidget" name="Widget">
                          <property name="geometry">
                           <rect>
                            <x>0</x>
                            <y>0</y>
                            <width>400</width>
                            <height>300</height>
                           </rect>
                          </property>
                          <property name="windowTitle">
                           <string>Widget</string>
                          </property>
                          <layout class="QGridLayout" name="gridLayout">
                           <item row="0" column="0">
                            <widget class="QPushButton" name="bigButton">
                             <property name="text">
                              <string>Big</string>
                             </property>
                            </widget>
                           </item>
                           <item row="0" column="1">
                            <widget class="QPushButton" name="normalButton">
                             <property name="text">
                              <string>Normal</string>
                             </property>
                            </widget>
                           </item>
                           <item row="0" column="2">
                            <widget class="QPushButton" name="smallButton">
                             <property name="text">
                              <string>Small</string>
                             </property>
                            </widget>
                           </item>
                           <item row="1" column="0" colspan="3">
                            <widget class="QScrollArea" name="scrollArea">
                             <property name="widgetResizable">
                              <bool>true</bool>
                             </property>
                             <widget class="QWidget" name="scrollAreaWidgetContents">
                              <property name="geometry">
                               <rect>
                                <x>0</x>
                                <y>0</y>
                                <width>380</width>
                                <height>251</height>
                               </rect>
                              </property>
                              <layout class="QHBoxLayout" name="horizontalLayout">
                               <property name="spacing">
                                <number>0</number>
                               </property>
                               <property name="leftMargin">
                                <number>0</number>
                               </property>
                               <property name="topMargin">
                                <number>0</number>
                               </property>
                               <property name="rightMargin">
                                <number>0</number>
                               </property>
                               <property name="bottomMargin">
                                <number>0</number>
                               </property>
                               <item>
                                <widget class="QLabel" name="label">
                                 <property name="text">
                                  <string/>
                                 </property>
                                 <property name="alignment">
                                  <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
                                 </property>
                                </widget>
                               </item>
                              </layout>
                             </widget>
                            </widget>
                           </item>
                          </layout>
                         </widget>
                         <layoutdefault spacing="6" margin="11"/>
                         <resources/>
                         <connections/>
                        </ui>
                        
                        M Offline
                        M Offline
                        magicstar
                        wrote on last edited by
                        #16

                        @VRonin ,
                        Thanks a lot. :-)

                        1 Reply Last reply
                        0

                        • Login

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