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. Check if buttons hidden on the widget

Check if buttons hidden on the widget

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 3.1k Views 2 Watching
  • 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.
  • Cobra91151C Offline
    Cobra91151C Offline
    Cobra91151
    wrote on last edited by Cobra91151
    #1

    Hi! I want to create the search by button text and display the message when no button is found.

    My code:

    QList<QPushButton *> allButtons = testWidget->findChildren<QPushButton *>();
    
    connect(testLineEdit, &QLineEdit::textChanged, [this](const QString &text) {
            for (QPushButton *button : allButtons) {
                 if (button->text().contains(text, Qt::CaseInsensitive)) {
                     button->setVisible(true);
                 } else {
                     button->setVisible(false);
                 }
            }
    });
    

    This code works, but I can't detect when all buttons are hidden (not visible) on the widget.

    I have tried to check when editing is finished but it displays no found even when 1 item is found:

    connect(testLineEdit, &QLineEdit::editingFinished, [this]() {
            bool isHidden = false;
    
            for (QPushButton *button: allButtons) {
                 if (!button->isVisible() && !testLineEdit->text().isEmpty()) {
                     isHidden = true;
                 } else {
                     isHidden = false;
                 }
            }
    
            qDebug() << isHidden;
    
            if (isHidden) {
                qDebug() << "Hidden!";
            }
     });
    

    Any ideas how to check if all buttons are hidden? Thanks.

    1 Reply Last reply
    0
    • Cobra91151C Offline
      Cobra91151C Offline
      Cobra91151
      wrote on last edited by Cobra91151
      #2

      So, I removed the connect(testLineEdit, &QLineEdit::editingFinished... and fixed it like this:

      bool isButtonVisible = false;
      
      connect(testLineEdit, &QLineEdit::textChanged, [this, isButtonVisible](const QString &text) {
          for (QPushButton *button: allButtons) {
             if (!text.isEmpty()) {
                if (button->text().contains(data, Qt::CaseInsensitive)) {
                    button->setVisible(true);
                    isButtonVisible = true;
                    break;
                }
      
                button->setVisible(false);
                isButtonVisible = false;
             } else {
               button->setVisible(true);
             }
          }
      
        if (!isButtonVisible) {
              qDebug() << "No button is found!";
        }
      });
      

      After some checking, it still not working as expected. For example, when full search string provided, other buttons are visible, not hidden.

      1 Reply Last reply
      1
      • Cobra91151C Offline
        Cobra91151C Offline
        Cobra91151
        wrote on last edited by Cobra91151
        #3

        I tried to search by indexOf function but it returns -1 even when 1 item is found? The same with contains(), it returns false but some buttons are visible. Any ideas why? Thanks.

        Code:

        int testIndex = 0;
         connect(testLineEdit, &QLineEdit::textChanged, [this, testIndex ](const QString &text) mutable {
                for (QPushButton *button: allButtons) {
                    if (!text.isEmpty()) {
                        testIndex = button->text().indexOf(text, 0, Qt::CaseInsensitive);
        
                        qDebug() << testIndex;
        
                        if (testIndex != -1) {
                            button->setVisible(true);
                        } else if (testIndex == -1) {
                            button->setVisible(false);
                            qDebug() << "No item is found!";
                        }
                    } else {
                        button->setVisible(true);
                    }
                }
            });
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          You logic is a bit strange. Shouldn't you rather start by doing a two pass logic ?
          i.e.:

          1. Check all buttons for text and set visible property
          2. Check all buttons for visibility

          Once you have that running properly, you can refactor the code to do it in one pass. There are several techniques to know whether all buttons are hidden.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          Cobra91151C 1 Reply Last reply
          1
          • Cobra91151C Offline
            Cobra91151C Offline
            Cobra91151
            wrote on last edited by Cobra91151
            #5

            @SGaist

            I will check and fix it. Thanks.

            1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              You logic is a bit strange. Shouldn't you rather start by doing a two pass logic ?
              i.e.:

              1. Check all buttons for text and set visible property
              2. Check all buttons for visibility

              Once you have that running properly, you can refactor the code to do it in one pass. There are several techniques to know whether all buttons are hidden.

              Cobra91151C Offline
              Cobra91151C Offline
              Cobra91151
              wrote on last edited by Cobra91151
              #6

              @SGaist

              I have tried it:

              1. Loop to find the searched buttons text works well:
                     for (QPushButton *button : allButtons) {
                          if (!text.isEmpty()) {
                              if (!button->text().contains(text, Qt::CaseInsensitive)) {
                                  button->setVisible(false);
                              } else {
                                  button->setVisible(true);
                              }
                          } else {
                              button->setVisible(true);
                          }
                      }
              
              1. The problem is that label is shown when some buttons are visible, I want to make this label visible only when all buttons are hidden. Checking buttons visibility with boolean doesn't work in this case.

              So, I need to check the widget for the visible/hidden buttons. What are other techniques to know whether all buttons are hidden? Thanks.

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi
                A fast test using your code works as expected so I do wonder why it won't for you.

                alt text

                  ui->label->setVisible(false);
                
                    connect(ui->lineEdit, &QLineEdit::textChanged, [this](const QString & text) {
                        QList<QPushButton *> allButtons = ui->testwidget->findChildren<QPushButton *>();
                        for (QPushButton *button : allButtons) {
                            if (button->text().contains(text, Qt::CaseInsensitive)) {
                                button->setVisible(true);
                            } else {
                                button->setVisible(false);
                            }
                            bool allHidden = true;
                            for (QPushButton *button : allButtons) {
                                if ( button->isVisible()) {
                                    allHidden = false;
                                    continue;
                                }
                            }
                            ui->label->setVisible(allHidden);
                        }
                    });
                
                
                Cobra91151C 1 Reply Last reply
                2
                • mrjjM mrjj

                  Hi
                  A fast test using your code works as expected so I do wonder why it won't for you.

                  alt text

                    ui->label->setVisible(false);
                  
                      connect(ui->lineEdit, &QLineEdit::textChanged, [this](const QString & text) {
                          QList<QPushButton *> allButtons = ui->testwidget->findChildren<QPushButton *>();
                          for (QPushButton *button : allButtons) {
                              if (button->text().contains(text, Qt::CaseInsensitive)) {
                                  button->setVisible(true);
                              } else {
                                  button->setVisible(false);
                              }
                              bool allHidden = true;
                              for (QPushButton *button : allButtons) {
                                  if ( button->isVisible()) {
                                      allHidden = false;
                                      continue;
                                  }
                              }
                              ui->label->setVisible(allHidden);
                          }
                      });
                  
                  
                  Cobra91151C Offline
                  Cobra91151C Offline
                  Cobra91151
                  wrote on last edited by Cobra91151
                  #8

                  @mrjj

                  Hi! Yes, my code works except for this part:

                  bool allHidden = true;
                  
                  for (QPushButton *button : allButtons) {
                          if (button->isVisible()) {
                               allHidden = false;
                               continue;
                          }
                  }
                  
                  ui->label->setVisible(allHidden);
                  

                  Now it works as it should. The issue is resolved. Thank you.

                  mrjjM 1 Reply Last reply
                  1
                  • Cobra91151C Cobra91151

                    @mrjj

                    Hi! Yes, my code works except for this part:

                    bool allHidden = true;
                    
                    for (QPushButton *button : allButtons) {
                            if (button->isVisible()) {
                                 allHidden = false;
                                 continue;
                            }
                    }
                    
                    ui->label->setVisible(allHidden);
                    

                    Now it works as it should. The issue is resolved. Thank you.

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Cobra91151
                    Ehh, ok :)
                    Guess the continue might have made the difference.

                    Cobra91151C 1 Reply Last reply
                    1
                    • mrjjM mrjj

                      @Cobra91151
                      Ehh, ok :)
                      Guess the continue might have made the difference.

                      Cobra91151C Offline
                      Cobra91151C Offline
                      Cobra91151
                      wrote on last edited by
                      #10

                      @mrjj

                      I have checked it, and even without continue it works well. I think my logic was not correct.

                      mrjjM 1 Reply Last reply
                      0
                      • Cobra91151C Cobra91151

                        @mrjj

                        I have checked it, and even without continue it works well. I think my logic was not correct.

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Cobra91151
                        Well the continue is just optimization.
                        if one is visible, no reason to check the others.
                        Works the same checking all, though.
                        Yeah, i guess so. even i didn't notice what
                        but maybe both checking text and visibility in
                        the same loop had some side effect.

                        1 Reply Last reply
                        1

                        • Login

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