Reclaming Space of Invisible QLabel



  • I have created a Widget that includes QLabel, which is initially not visible. However, depending on what the user does, this QLabel becomes visible. Further user actions may subsequently make the QLabel invisible again. The size of the containing Widget is adjusted at the first time that the QLabel becomes visible. Also, the space required to show the QLabel makes the Widget adjust in at the first time that the QLabel is shown. All this is according to my expectations. However, when the QLabel becomes invisible again, the size of the QWidget and the extra space is not reverted. Any suggestions on how to revert these changes to the containing QWidget when the QLabel becomes invisible?



  • Do you use a layout?
    How do you hide the label?



  • Yes, I do use a QVBoxLayout were the QLabel is one of the Widgets (the last one actually). The QLabel is hidden / shown by using the QWidget::setVisibility method.



  • Do you have spacers?

    Can you post the code?



  • Sure, here is the relevant part. The QLabel is named Warning.

    NameEntity::NameEntity(QWidget *Parent, QSet<QString> E, bool New, const QString &Type, const QString &N) : QDialog(Parent) {
    
        Excludes = E;
    
        Name = new QLineEdit(N, this);
        connect(Name, SIGNAL(textChanged(QString)), this, SLOT(onTextChanged(QString)));
    
        Buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
        connect(Buttons, SIGNAL(accepted()), this, SLOT(accept()));
        connect(Buttons, SIGNAL(rejected()), this, SLOT(reject()));
    
        QFormLayout *NameLayout = new QFormLayout;
        NameLayout->addRow(tr("%1 Name:").arg(Type), Name);
    
        Warning = new QLabel(tr("<font color='red' size='1'>Warning: Another equally named %1 exists</font>").arg(Type), this);
    
        QVBoxLayout *Layout = new QVBoxLayout;
        Layout->addLayout(NameLayout);
        Layout->addWidget(Warning);
        Layout->addWidget(Buttons);
        setLayout(Layout);
    
        onTextChanged(Name->text());
    }
    
    // Slots
    void NameEntity::onTextChanged(QString Text) {
    
        Warning->setVisible(Excludes.contains(Text));
        Buttons->button(QDialogButtonBox::Ok)->setEnabled(!Text.isEmpty());
    }
    

  • Moderators

    @ModelTech Have you tried the adjustSize() function after you hide the widget?

    When I've done things like this is the past I removed the widget from the layout (didn't delete it, just took it out after I hid it). Then you can add it back in when you want to show it again. You will probably need to deal with it not as a widget but as it's own layout since you can only take and put back QLayoutItem's.

    I'm not sure if adjustSize will work, but something worth trying.



  • Thatś interesting (for another problem that I am currently experiencing): How do you remove a Widget from a Layout?


  • Moderators

    @ModelTech said in Reclaming Space of Invisible QLabel:

    Thatś interesting (for another problem that I am currently experiencing): How do you remove a Widget from a Layout?

    http://doc.qt.io/qt-5/qlayout.html#takeAt

    If you're talking about your other thread on your bad QGridLayout, your question there was changing a widget in a layout which you would do with http://doc.qt.io/qt-5/qlayout.html#replaceWidget instead. :)



  • adjustSize didn't solve the problem of this thread, but your suggestion for removal of a Widget from a Layout has my other problem ;)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.