QLabel size won't expand even after setting sizepolicy to expanding
-
Hi,
I am using qLabel inside scrollarea to display image.
I am setting qLabel sizepolicy to fixedsize programmatically in one function of code. And again setting it back to expanding in other function like follow
m_scrollarea->setWidgetResizable(true);
viewlabel->setMaximumSize(10000,10000);
viewlabel->adjustSize();
viewlabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);However, qLabel won't expand if image size is bigger.
May I know how can I solve this issue? -
Hi,
Can you show the complete code where you change the size policy. Also why are you changing it ?
-
Hi,
Here is my dialog.ui<?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="widgetResizable"> <bool>true</bool> </property> <widget class="QWidget" name="scrollAreaWidgetContents"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>380</width> <height>249</height> </rect> </property> <layout class="QHBoxLayout" name="horizontalLayout_2"> <item> <widget class="QLabel" name="label"> <property name="text"> <string/> </property> </widget> </item> </layout> </widget> </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>
Here is my dialog.cpp file
#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() { int w = ui->scrollArea->width() -20; int h = ui->scrollArea->height() -20; ui->label->setFixedSize(w,h); } void Dialog::on_pushButton_expandsize_clicked() { ui->scrollArea->setWidgetResizable(true); ui->label->setMaximumSize(10000,10000); ui->label->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); } 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); }
Now as default, label size policy is expanding, full image with scrollbars would appear.
After that I clicked on Fix Size button. And image got cropped as expected
Now after that I clicked on Expand Size. But, it won't increase the size of label. How do I get previous behaviour like shown in 1st image. I am learning QT. And for some other project I want to switch between above two size policies
-
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 simplyui->label->setFixedSize(ui->label->sizeHint());
-
@magicstar
In your first post, in Topic Tools button, there is menu to say solved. -
@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:
- display as it is
- display to fit to content
-
Scroll area does not lay out the contents of the viewport. The sizing is left entirely to the user.
Make sure you haveui->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);
- resize label to the image size:
-
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.
-
@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());
-
@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>
-
@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.
-
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(); }
-
@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 -
@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>