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. Reclaming Space of Invisible QLabel
QtWS25 Last Chance

Reclaming Space of Invisible QLabel

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 3.6k 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.
  • ModelTechM Offline
    ModelTechM Offline
    ModelTech
    wrote on last edited by
    #1

    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
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      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
      0
      • ModelTechM Offline
        ModelTechM Offline
        ModelTech
        wrote on last edited by
        #3

        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
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          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
          0
          • ModelTechM Offline
            ModelTechM Offline
            ModelTech
            wrote on last edited by ModelTech
            #5

            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
            0
            • ModelTechM 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 Offline
              A Offline
              ambershark
              wrote on last edited by ambershark
              #6

              @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
              1
              • ModelTechM Offline
                ModelTechM Offline
                ModelTech
                wrote on last edited by
                #7

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

                A 1 Reply Last reply
                0
                • ModelTechM ModelTech

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

                  A Offline
                  A Offline
                  ambershark
                  wrote on last edited by
                  #8

                  @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
                  2
                  • ModelTechM Offline
                    ModelTechM Offline
                    ModelTech
                    wrote on last edited by
                    #9

                    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
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved