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. push buttons are in large numbers
Qt 6.11 is out! See what's new in the release blog

push buttons are in large numbers

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 891 Views 1 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.
  • R Offline
    R Offline
    russjohn834
    wrote on last edited by
    #1

    Hi,

    I have many push buttons (10 nos) and I need to change its style based various conditions, as follows.

    I'm wondering , Is there a easy way to do this?

       if(value == 0)
                {
                    ui->btn1->setStyleSheet(StyleSheetOn);
                    ui->btn2->setStyleSheet(StyleSheetOff);
    
                    .....
    
                    ui->btn10->setStyleSheet(StyleSheetOff);
                
                }
                else if(value == 1)
                {
                    ui->btn2->setStyleSheet(StyleSheetOn);
                    ui->btn1->setStyleSheet(StyleSheetOff);
    
                     .....
                   
                    ui->btn10->setStyleSheet(StyleSheetOff);   
            
                }
                else if(value == 2)
                {
                    ui->btn3->setStyleSheet(StyleSheetOn);
                    ui->btn2->setStyleSheet(StyleSheetOff);
    
                    .....
    
                    ui->btn10->setStyleSheet(StyleSheetOff);             
    
                 }
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      Well if the issue is to turn off all other than the On one, you could do

      QList<QPushButton *> List = findChildren<QPushButton *>();
       for (QPushButton * button : List) {
         button->setStyleSheet(StyleSheetOff);  
       }
      

      Then turn On the single one you want.

      If you make sure buttons comes in right order then you can also use this way to do the value part.

      List[value]->setStyleSheet(StyleSheetOn);

      But you have to make sure it matches up or it will crash :)
      so button 1 will be in index zero etc.

      If you have more Buttons on the form than you want in list then you can add a widget to hold the buttons you want to affect and use

      QList<QPushButton *> List = TheWidgetHolder->findChildren<QPushButton *>();

      So we only ask that widget about buttons and not the whole of mainwindow.

      1 Reply Last reply
      1
      • R Offline
        R Offline
        russjohn834
        wrote on last edited by
        #3

        @mrjj Thanks a lot for your suggestion.
        How do I make sure the button comes in the right order? what's the correct way to name the push button in this case?

        mrjjM 1 Reply Last reply
        0
        • R russjohn834

          @mrjj Thanks a lot for your suggestion.
          How do I make sure the button comes in the right order? what's the correct way to name the push button in this case?

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

          Hi
          That is the kinda tricky part as they seem to come in creation order.
          Meaning in that order that you dragged them to the form.

          The name is not important in this context.

          It might be ok already.

          just dump the list and see.

          If you use
          List[value]->

          then value must never be bigger than list.size()-1 ( -1 as it starts with zero)

          1 Reply Last reply
          0
          • R Offline
            R Offline
            russjohn834
            wrote on last edited by
            #5

            @mrjj said in push buttons are in large numbers:

            QList<QPushButton *> List = findChildren<QPushButton *>();

            Instead getting all the pushbuttons on the screen to a list , how do I add selected push buttons into the list, say push button names are btn_1, btn_2, btn_3 etc?

            mrjjM 1 Reply Last reply
            0
            • R russjohn834

              @mrjj said in push buttons are in large numbers:

              QList<QPushButton *> List = findChildren<QPushButton *>();

              Instead getting all the pushbuttons on the screen to a list , how do I add selected push buttons into the list, say push button names are btn_1, btn_2, btn_3 etc?

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

              @russjohn834
              Hi
              As i wrote, just put them in a extra QWidget and do
              QList<QPushButton *> List = TheWidgetHolder->findChildren<QPushButton *>();
              that way you get only the children of the TheWidgetHolder.
              If that is too annoying you could also just make the list manually

              QList<QPushButton *> list{ ui->bt1, ui->bt2 .... };

              1 Reply Last reply
              2

              • Login

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