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. how to make a function that takes pushbutton as argument
Forum Update on Monday, May 27th 2025

how to make a function that takes pushbutton as argument

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 434 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.
  • M Offline
    M Offline
    mgdfp
    wrote on last edited by
    #1

    hi all,
    I am very new to Qt creator and also to C++ so I cannot figure out how to do the following:
    I have a number of pushbuttons that are animated based on some external signals. I am animating these based on a TimerSlot, like this:

    void keypad::TimerSlot()
    {
    
         if (button1_inhibit) == true)
        {
            ui->button1->setEnabled(false);
        }
    
        else
        {
            ui->button1->setEnabled(true);
        }
    
        if (button1_feedback)== true)
        {
            ui->button1->setStyleSheet("background-color: white");
            ui->button1->update();
        }
    
        else
        {
            ui->button1->setStyleSheet("background-color: grey");
            ui->button1->update();
        }
    
        // same thing for button_2, 3, 4 and so on. 
    
    }
    

    I have many buttons and I wanted to create a function that takes arguments bool inhibit, bool feedback and button and that I then can call repeatedly for each button. Instead of copy and paste the if..else for each button, something like this:

    void animateButton (bool inhibit, bool feedback, keypad::ui::button)
    {
        if (inhibit == true)
        {
            ui->button->setEnabled(false);
        }
    
        else
        {
            ui->button->setEnabled(true);
        }
    }
    //...
    

    however I try I cannot get this to work and I have not been able to find any information when searching, probably because I don't know how to search for it.. I get errors that ui is not declared in this scope and the button is not a member of keypad and various other errors depeding on what I try. Can someone please point me in the right direction. Thanks a lot.

    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Can you explain how you manage these external signals ?

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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mgdfp
        wrote on last edited by
        #3

        Hi and thanks. Yes, I can try. They are external signals, sniffed from the network. They can change value at any time so I think I have to check them periodically based on a timer. if they change value they should animate the button. I have not implemented the actual fetching of the signals so I don't know exactly how it works but I hope that explanation is enough.

        1 Reply Last reply
        0
        • M mgdfp

          hi all,
          I am very new to Qt creator and also to C++ so I cannot figure out how to do the following:
          I have a number of pushbuttons that are animated based on some external signals. I am animating these based on a TimerSlot, like this:

          void keypad::TimerSlot()
          {
          
               if (button1_inhibit) == true)
              {
                  ui->button1->setEnabled(false);
              }
          
              else
              {
                  ui->button1->setEnabled(true);
              }
          
              if (button1_feedback)== true)
              {
                  ui->button1->setStyleSheet("background-color: white");
                  ui->button1->update();
              }
          
              else
              {
                  ui->button1->setStyleSheet("background-color: grey");
                  ui->button1->update();
              }
          
              // same thing for button_2, 3, 4 and so on. 
          
          }
          

          I have many buttons and I wanted to create a function that takes arguments bool inhibit, bool feedback and button and that I then can call repeatedly for each button. Instead of copy and paste the if..else for each button, something like this:

          void animateButton (bool inhibit, bool feedback, keypad::ui::button)
          {
              if (inhibit == true)
              {
                  ui->button->setEnabled(false);
              }
          
              else
              {
                  ui->button->setEnabled(true);
              }
          }
          //...
          

          however I try I cannot get this to work and I have not been able to find any information when searching, probably because I don't know how to search for it.. I get errors that ui is not declared in this scope and the button is not a member of keypad and various other errors depeding on what I try. Can someone please point me in the right direction. Thanks a lot.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @mgdfp
          I don't mean to sound rude because you have asked your question nicely, but you need to understand C++ and particularly classes before you will get far with Qt. You would benefit from taking a pause and reading up on those before continuing.

          void keypad::TimerSlot()
          

          This is a method defined in class keypad. The ui variable is a member of that class, and that's why you can access it inside this function.

          void animateButton (bool inhibit, bool feedback, keypad::ui::button)
          

          This method is not defined in class keypad. Unless it is supposed to be, you won't be able to access variable ui, hence your "I get errors that ui is not declared in this scope and the button is not a member of keypad".

          In fact, this function is not defined in any class. It is a "global" function, like in C. In C++, at least as a rule of thumb, just about all of your code should be written inside classes.

          keypad::ui::button in your argument list is wrong.

          What you probably intend is something like:

          void SomeClassEitherKeypadOrSomethingElse::animateButton (bool inhibit, bool feedback, QPushbutton *button)
          {
              if (inhibit == true)
              {
                  button->setEnabled(false);
              }
              else
              {
                  button->setEnabled(true);
              }
          }
          

          To write code for doing this sort of thing on your multiple buttons, you want put all your buttons into an array/list, such as

          QList<QPushButton*> buttons = { ui->button1, ui->button2, ui->button3, ... };
          

          Then instead of having to write code for each button explicitly, you can perform operations on all the buttons by looping over the items in the list:

          for (QPushButton *button : buttons)
          {
              button->setStyleSheet("...");
              ...
          }
          
          1 Reply Last reply
          5
          • M Offline
            M Offline
            mgdfp
            wrote on last edited by
            #5

            Hi and thanks,
            Yes, you are right, I need to get up to speed on C++ classes, I will do that next.
            Thanks for the help, putting things in arrays does help, I am playing around with this as well.

            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