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. Generated buttons, Hiding button text upon a button press

Generated buttons, Hiding button text upon a button press

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 2.2k 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.
  • K Offline
    K Offline
    Kris Revi
    wrote on 6 Dec 2020, 13:27 last edited by Kris Revi 12 Jun 2020, 13:29
    #1

    I generate buttons for each entry in a json file like this

            QJsonDocument document = QJsonDocument::fromJson(jsonData);
            QJsonObject object = document.object();
    
            QJsonValue value = object.value("Device");
            QJsonArray array = value.toArray();
            foreach (const QJsonValue & v, array)
            {
                QPushButton *button = new QPushButton(this);
    
                button->setObjectName("DeviceButton0");
                button->setMinimumHeight(46);
                button->setMaximumHeight(46);
                button->setText(v.toObject().value("Name").toString());
    
                ui->VB_LedStrip_Container->addWidget(button);
            }
    

    So when i press a certain button i set the width of this menu so that ONLY the icon of the buttons shows but i can see the first character in the button text! and i can't set the width lower (for design reasons) so my question is.

    • How would you go about hiding the text of every button in this menu when the button is pressed and when the button is pressed again to show the menu, show the text in the button again.
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 6 Dec 2020, 13:46 last edited by mrjj 12 Jun 2020, 13:49
      #2

      Hi
      You could store the buttons in a QList so you can loop over them to set/reset text as needed.
      When you assign the text the first time
      button->setText(v.toObject().value("Name").toString());
      you also store the text in a dynamic property
      button->setProperty("orgtext", button->text() );

      that way you can hide it by setting it to empty text and then you have the text in the property to restore it when needed.

      Alternative 2 is to make a custom QPushButton with custom paintEvent where you can tell it to draw text or not.

      K 2 Replies Last reply 6 Dec 2020, 14:10
      2
      • M mrjj
        6 Dec 2020, 13:46

        Hi
        You could store the buttons in a QList so you can loop over them to set/reset text as needed.
        When you assign the text the first time
        button->setText(v.toObject().value("Name").toString());
        you also store the text in a dynamic property
        button->setProperty("orgtext", button->text() );

        that way you can hide it by setting it to empty text and then you have the text in the property to restore it when needed.

        Alternative 2 is to make a custom QPushButton with custom paintEvent where you can tell it to draw text or not.

        K Offline
        K Offline
        Kris Revi
        wrote on 6 Dec 2020, 14:10 last edited by
        #3

        @mrjj said in Generated buttons, Hiding button text upon a button press:

        Hi
        You could store the buttons in a QList so you can loop over them to set/reset text as needed.
        When you assign the text the first time
        button->setText(v.toObject().value("Name").toString());
        you also store the text in a dynamic property
        button->setProperty("orgtext", button->text() );

        that way you can hide it by setting it to empty text and then you have the text in the property to restore it when needed.

        Alternative 2 is to make a custom QPushButton with custom paintEvent where you can tell it to draw text or not.

        that is cool, so let me get this right if i would do

        button->setText("");
        

        i can do

        button->setProperty("orgtext", button->text());
        

        to get the original text back? or did i get that wrong?

        1 Reply Last reply
        0
        • M mrjj
          6 Dec 2020, 13:46

          Hi
          You could store the buttons in a QList so you can loop over them to set/reset text as needed.
          When you assign the text the first time
          button->setText(v.toObject().value("Name").toString());
          you also store the text in a dynamic property
          button->setProperty("orgtext", button->text() );

          that way you can hide it by setting it to empty text and then you have the text in the property to restore it when needed.

          Alternative 2 is to make a custom QPushButton with custom paintEvent where you can tell it to draw text or not.

          K Offline
          K Offline
          Kris Revi
          wrote on 6 Dec 2020, 15:19 last edited by
          #4

          @mrjj i did this

          mainwindow.h

          List<QPushButton*> m_DeviceButtonList;
          

          mainwindow.cpp

                  QJsonDocument document = QJsonDocument::fromJson(jsonData);
                  QJsonObject object = document.object();
          
                  QJsonValue value = object.value("Device");
                  QJsonArray array = value.toArray();
                  foreach (const QJsonValue & v, array)
                  {
                      QPushButton *button = new QPushButton(this);
          
                      button->setObjectName("DeviceButton0");
                      button->setMinimumHeight(46);
                      button->setMaximumHeight(46);
                      button->setText(v.toObject().value("Name").toString());
          
                      ui->VB_LedStrip_Container->addWidget(button);
          
                      m_DeviceButtonList.append(button);
                  }
          
                  for (QPushButton * dButtons : m_DeviceButtonList)
                  {
                      qDebug() << "Found : " << dButtons;
                  }
          

          and i get this

          Found :  QPushButton(0x9ec5c8, name="DeviceButton0")
          Found :  QPushButton(0x978840, name="DeviceButton0")
          Found :  QPushButton(0x598d030, name="DeviceButton0")
          Found :  QPushButton(0x598ccd0, name="DeviceButton0")
          Found :  QPushButton(0x598d7c8, name="DeviceButton0")
          

          i thought each button would get a unique object name? like

          DeviceButton0
          DeviceButton1
          DeviceButton2
          DeviceButton3
          ....

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 6 Dec 2020, 15:26 last edited by mrjj 12 Jun 2020, 15:28
            #5

            Hi
            button->setProperty("orgtext", button->text());
            stores the text into a dynamic property,. ( the orgtext name)

            button->setText( button->property("orgtext").toString()   );
            

            would set the text back.

            Regarding the naming
            you set it to
            button->setObjectName("DeviceButton0");
            so all will get same name.

            You will need to add the number manually.
            something like

            int cc=0;
            foreach (const QJsonValue & v, array)
                    {
                        QPushButton *button = new QPushButton(this);
                       button->setObjectName("DeviceButton"+ QString::number(cc++));
            ...
            

            But else you seem on the right track. :)

            K 1 Reply Last reply 6 Dec 2020, 15:33
            0
            • M mrjj
              6 Dec 2020, 15:26

              Hi
              button->setProperty("orgtext", button->text());
              stores the text into a dynamic property,. ( the orgtext name)

              button->setText( button->property("orgtext").toString()   );
              

              would set the text back.

              Regarding the naming
              you set it to
              button->setObjectName("DeviceButton0");
              so all will get same name.

              You will need to add the number manually.
              something like

              int cc=0;
              foreach (const QJsonValue & v, array)
                      {
                          QPushButton *button = new QPushButton(this);
                         button->setObjectName("DeviceButton"+ QString::number(cc++));
              ...
              

              But else you seem on the right track. :)

              K Offline
              K Offline
              Kris Revi
              wrote on 6 Dec 2020, 15:33 last edited by
              #6

              @mrjj said in Generated buttons, Hiding button text upon a button press:

              button->property("orgtext").toString()

              this didn't work for some reason :S

                      for (QPushButton * dButtons : m_DeviceButtonList)
                      {
                          dButtons->setText( dButtons->property("orgtext").toString() );
                      }
              
              M 1 Reply Last reply 6 Dec 2020, 15:36
              0
              • K Kris Revi
                6 Dec 2020, 15:33

                @mrjj said in Generated buttons, Hiding button text upon a button press:

                button->property("orgtext").toString()

                this didn't work for some reason :S

                        for (QPushButton * dButtons : m_DeviceButtonList)
                        {
                            dButtons->setText( dButtons->property("orgtext").toString() );
                        }
                
                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 6 Dec 2020, 15:36 last edited by mrjj 12 Jun 2020, 15:36
                #7

                @Kris-Revi
                In what way didn't work?

                K 1 Reply Last reply 6 Dec 2020, 15:37
                0
                • M mrjj
                  6 Dec 2020, 15:36

                  @Kris-Revi
                  In what way didn't work?

                  K Offline
                  K Offline
                  Kris Revi
                  wrote on 6 Dec 2020, 15:37 last edited by Kris Revi 12 Jun 2020, 15:38
                  #8

                  @mrjj

                  So on button press i remove the text

                  void MainWindow::on_B_ToggleMenu_clicked()
                  {   
                      if (ui->B_ToggleMenu->isChecked() == true)
                      {
                          for (QPushButton * dButtons : m_DeviceButtonList)
                          {
                              dButtons->setText("");
                          }
                      }
                      else
                      {
                          for (QPushButton * dButtons : m_DeviceButtonList)
                          {
                              dButtons->setText( dButtons->property("orgtext").toString() );
                          }
                      }
                  }
                  

                  but the text never shows :S

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 6 Dec 2020, 15:38 last edited by
                    #9

                    @Kris-Revi said in Generated buttons, Hiding button text upon a button press:

                    but the text never shows

                    When you never set the property how do you expect that there is text in it?

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    K 1 Reply Last reply 6 Dec 2020, 15:39
                    2
                    • C Christian Ehrlicher
                      6 Dec 2020, 15:38

                      @Kris-Revi said in Generated buttons, Hiding button text upon a button press:

                      but the text never shows

                      When you never set the property how do you expect that there is text in it?

                      K Offline
                      K Offline
                      Kris Revi
                      wrote on 6 Dec 2020, 15:39 last edited by Kris Revi 12 Jun 2020, 15:40
                      #10

                      @Christian-Ehrlicher said in Generated buttons, Hiding button text upon a button press:

                      @Kris-Revi said in Generated buttons, Hiding button text upon a button press:

                      but the text never shows

                      When you never set the property how do you expect that there is text in it?

                      @mrjj said in Generated buttons, Hiding button text upon a button press:

                      Hi
                      You could store the buttons in a QList so you can loop over them to set/reset text as needed.
                      When you assign the text the first time
                      button->setText(v.toObject().value("Name").toString());
                      you also store the text in a dynamic property
                      button->setProperty("orgtext", button->text() );

                      that way you can hide it by setting it to empty text and then you have the text in the property to restore it when needed.

                      Alternative 2 is to make a custom QPushButton with custom paintEvent where you can tell it to draw text or not.

                      When you assign the text the first time
                      button->setText(v.toObject().value("Name").toString());
                      you also store the text in a dynamic property
                      button->setProperty("orgtext", button->text() );

                      did i read this wrong? was it supposed to be

                      you should also store the text in a dynamic property ?

                      M 1 Reply Last reply 6 Dec 2020, 15:43
                      0
                      • K Kris Revi
                        6 Dec 2020, 15:39

                        @Christian-Ehrlicher said in Generated buttons, Hiding button text upon a button press:

                        @Kris-Revi said in Generated buttons, Hiding button text upon a button press:

                        but the text never shows

                        When you never set the property how do you expect that there is text in it?

                        @mrjj said in Generated buttons, Hiding button text upon a button press:

                        Hi
                        You could store the buttons in a QList so you can loop over them to set/reset text as needed.
                        When you assign the text the first time
                        button->setText(v.toObject().value("Name").toString());
                        you also store the text in a dynamic property
                        button->setProperty("orgtext", button->text() );

                        that way you can hide it by setting it to empty text and then you have the text in the property to restore it when needed.

                        Alternative 2 is to make a custom QPushButton with custom paintEvent where you can tell it to draw text or not.

                        When you assign the text the first time
                        button->setText(v.toObject().value("Name").toString());
                        you also store the text in a dynamic property
                        button->setProperty("orgtext", button->text() );

                        did i read this wrong? was it supposed to be

                        you should also store the text in a dynamic property ?

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 6 Dec 2020, 15:43 last edited by mrjj 12 Jun 2020, 15:44
                        #11

                        Hi
                        Yes. you should also store it in the property as else it will just be empty.
                        So when you create teh buttons, also do and have set the text.
                        button->setProperty("orgtext", button->text() );

                        foreach (const QJsonValue & v, array)
                                {
                                    QPushButton *button = new QPushButton(this);
                        
                                    button->setObjectName("DeviceButton0");
                                    button->setMinimumHeight(46);
                                    button->setMaximumHeight(46);
                                    button->setText(v.toObject().value("Name").toString());
                                    button->setProperty("orgtext", button->text() ); // store a copy
                        ......1
                        
                        1 Reply Last reply
                        2

                        5/11

                        6 Dec 2020, 15:26

                        • Login

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