Resizing qt widgets when their children are hidden

后端 未结 5 717
野趣味
野趣味 2020-12-31 05:13

How would I go about resizing the widget when the retry child is hidden so that it looks as in the first image? The main layout is a QVBoxLayout, the retry child is a widget

5条回答
  •  臣服心动
    2020-12-31 05:26

    Here is a basic example which does auto-resize upon widget hide/show.

    dialog.h file:

    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include 
    #include 
    
    class dialog : public QDialog
    {
        Q_OBJECT
    
    
    public:
        explicit dialog(QWidget *parent = 0)
        {
    
            vlayout.addWidget(&checkbox);
            vlayout.addWidget(&label);
            label.setText("Label");
    
            setLayout(&vlayout);
            this->layout()->setSizeConstraint(QLayout::SetFixedSize); // !!! This is the what makes it auto-resize
    
            checkbox.setChecked(true);
            connect(&checkbox,SIGNAL(toggled(bool)),&label,SLOT(setVisible(bool)));
        }
    
    private:
        QVBoxLayout vlayout;
        QCheckBox checkbox;
        QLabel label;
    
    };
    
    #endif // DIALOG_H
    

    and main.c

    #include 
    #include "dialog.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        dialog d;
        d.show();
        return a.exec();
    }
    

提交回复
热议问题