Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved Reclaming Space of Invisible QLabel

    General and Desktop
    3
    9
    2668
    Loading More Posts
    • 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.
    • ModelTech
      ModelTech last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • VRonin
        VRonin last edited by

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

        "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 Reply Quote 0
        • ModelTech
          ModelTech last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • VRonin
            VRonin last edited by

            Do you have spacers?

            Can you post the code?

            "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 Reply Quote 0
            • ModelTech
              ModelTech last edited by ModelTech

              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());
              }
              
              A 1 Reply Last reply Reply Quote 0
              • A
                ambershark @ModelTech last edited by ambershark

                @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.

                My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                1 Reply Last reply Reply Quote 1
                • ModelTech
                  ModelTech last edited by

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

                  A 1 Reply Last reply Reply Quote 0
                  • A
                    ambershark @ModelTech last edited by

                    @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. :)

                    My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                    1 Reply Last reply Reply Quote 2
                    • ModelTech
                      ModelTech last edited by

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

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post