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 QWidget during show event?
QtWS25 Last Chance

Resize QWidget during show event?

Scheduled Pinned Locked Moved Unsolved General and Desktop
showeventresize
3 Posts 2 Posters 968 Views
  • 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.
  • CJhaC Offline
    CJhaC Offline
    CJha
    wrote on last edited by
    #1

    Hi, I have a QWidget which has a QScrollArea, this scroll area contains a vertical layout (QVBoxLayout) in which I add QCheckBox during the showEvent(). Here is a test code:

    class ResizeTest : public QWidget
    {
    	Q_OBJECT
    
    public:
    	ResizeTest(QWidget* parent = Q_NULLPTR);
    	~ResizeTest();
    	int num{0};
    
    private:
    	Ui::ResizeTest ui;
    	QVector<QPointer<QCheckBox>> chks;
    
    protected:
    	void showEvent(QShowEvent* event) override;
    	void closeEvent(QCloseEvent* event) override;
    
    private slots:
    	void onCheckBoxClicked();
    };
    
    ResizeTest::ResizeTest(QWidget* parent)
    	: QWidget(parent)
    {
    	ui.setupUi(this);
    }
    
    ResizeTest::~ResizeTest()
    {}
    
    void ResizeTest::showEvent(QShowEvent* event)
    {
    	if(num > 0) {
    		for(int ii = 0; ii < num; ++ii) {
    			QString name = QString("CheckBox Number ").append(QString::number(ii + 1));
    			QPointer<QCheckBox> chkPtr = new QCheckBox;
    			chkPtr->setText(name);
    			connect(chkPtr, &QCheckBox::clicked, this, &ResizeTest::onCheckBoxClicked);
    			chks << chkPtr;
    			ui.verticalLayout->addWidget(chkPtr);
    		}
    	}
    
    	qDebug() << ui.verticalLayout->minimumSize(); // outputs QSize(0, 0) always
    
    	int widgetHeight = this->layout()->contentsMargins().top() + this->layout()->contentsMargins().bottom() +  + ui.verticalLayout->minimumSize().height();
    	int widgetWidth = this->layout()->contentsMargins().left() + this->layout()->contentsMargins().right() + ui.verticalLayout->minimumSize().width();
    	this->resize(widgetWidth, widgetHeight);
    }
    
    void ResizeTest::closeEvent(QCloseEvent* event)
    {
    	while(!chks.isEmpty()) {
    		auto lastItem = chks.last();
    		if(!lastItem.isNull())
    			delete lastItem;
    		chks.removeLast();
    	}
    	chks.clear();
    }
    
    void ResizeTest::onCheckBoxClicked()
    {
    	qDebug() << ui.verticalLayout->minimumSize(); // this gives me correct size
    }
    

    Is there any way I could get the correct ui.verticalLayout->minimumSize() during the show event so that my widget is resized properly when shown?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You should not modify the widgets during the showEvent - do it in an init function or in the ctor instead.
      If you use proper layouts and set proper min/max sizes for your widgets then the widget is resized without any interaction from your size.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      CJhaC 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        You should not modify the widgets during the showEvent - do it in an init function or in the ctor instead.
        If you use proper layouts and set proper min/max sizes for your widgets then the widget is resized without any interaction from your size.

        CJhaC Offline
        CJhaC Offline
        CJha
        wrote on last edited by
        #3

        @Christian-Ehrlicher Ok. I understand, I will add my check boxes in a different function and not during showEvent().

        I tried setting layoutSizeConstraint to different values for the vertical layout, the QScrollArea as well as for the widget itself, because of QScrollArea my widget is always resized to a small size when shown for the first time, this is why I tried to add and resize the widget during showEvent() myself. According to Qt's documentation:

        If a scroll area is used to display the contents of a widget that contains child widgets arranged in a layout, it is important to realize that the size policy of the layout will also determine the size of the widget. This is especially useful to know if you intend to dynamically change the contents of the layout. In such cases, setting the layout's size constraint property to one which provides constraints on the minimum and/or maximum size of the layout (e.g., QLayout::SetMinAndMaxSize) will cause the size of the scroll area to be updated whenever the contents of the layout changes.

        But this is not working for me, I am setting the size constraint to QLayout::SetMinAndMaxSize but the QScrollArea remains at the minimum size when shown for the first time.

        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