Results 1 to 13 of 13

Thread: Having one code for every pushButton

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2020
    Posts
    11
    Qt products
    Qt4 Qt5

    Default Having one code for every pushButton

    Hey, I have a 10x10 field of pushButtons and I have a Code for what happens, when one is clicked. How can I rewrite the Code, so that the Code applies for every pushButton and I dont have to rewrite my Code for every one of them? With my Code I would have to create 100 variable and have to Change every one in the Code for each pushButton plus I would rewrite the Name of the pushButton in every one. I would like to have a Code which I can copy and paste in every pushButton. I searched a lot but with my Knowledge a lot is hard to understand. Maybe you have any idea?
    My Code so far is:

    int i=1;

    void MainWindow:n_pushButton_clicked()
    {
    if((i==1))
    {
    ui->pushButton->setStyleSheet("QPushButton { background-color: grey; }\n"
    "QPushButton:enabled { background-color: rgb(200,0,0,100); }\n");
    i=i+1;

    }
    else
    {
    if((i==2))
    {
    ui->pushButton->setStyleSheet("QPushButton { background-color: grey; }\n"
    "QPushButton:enabled { background-color: rgb(0,200,0,100); }\n");
    i=i+1;
    }
    else
    {
    ui->pushButton-> setStyleSheet("QPushButton { }\n"
    "QPushButton:enabled { }\n");
    i=i-2;
    }
    }

    }

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Having one code for every pushButton

    If you want to handle the event with the same code for each QPushButton, use the QObject::sender method in the slot function. Your method should look something like this :
    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. QPushButton *ptr = dynamic_cast<QPushButton*>(sender());
    4. if(ptr == nullptr)
    5. return;
    6. if((i==1))
    7. {
    8. ptr->setStyleSheet("QPushButton { background-color: grey; }\n"
    9. "QPushButton:enabled { background-color: rgb(200,0,0,100); }\n");
    10. i=i+1;
    11. }
    12. else
    13. {
    14. if((i==2))
    15. {
    16. ptr->setStyleSheet("QPushButton { background-color: grey; }\n"
    17. "QPushButton:enabled { background-color: rgb(0,200,0,100); }\n");
    18. i=i+1;
    19. }
    20. else
    21. {
    22. ptr-> setStyleSheet("QPushButton { }\n"
    23. "QPushButton:enabled { }\n");
    24. i=i-2;
    25. }
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2020
    Posts
    11
    Qt products
    Qt4 Qt5

    Default Re: Having one code for every pushButton

    Yeah that helps a lot. Little Problem now is that when since int i=1 is out of the pushbutton function, when I now click on a button and then on another i does not equal to 1 anymore so the order of what happens is not the same. When I include int i=1 in the pushbutton function it Always starts at 1 and only the first if-cause is active. Do you have a solution to that as well?
    Last edited by ejoty; 17th June 2020 at 11:39.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Having one code for every pushButton

    No because I don't know what it is variable i. Does it apply to all buttons together or separately, where it is modified.

  5. #5
    Join Date
    Jun 2020
    Posts
    11
    Qt products
    Qt4 Qt5

    Default Re: Having one code for every pushButton

    Well I want the pushButton to Change Color when clicked on. So "i" is just a avariable I used to help me assaign a number to a Color, regarding how many times the Buttons is already clicked on, so that I get different Colors when the pushbutton is clicked multiple times. I would like to have the 10x10 field of pushbuttons in which no metter which pushButton gets clicked its Color changes from red to green to blue and then again red green blue and so on. If I use my Code up I would to have 100 variables one for each push button. The way I did it "i" can not apply to all Buttons because then the order is disturbed. But I would like to find a way to have "i" apply to everyone so that I dont have to use a different variable for every pushbutton.

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Having one code for every pushButton

    So the counter for each buttton separately. I suggest using QMap <QPushButtton *, int>. The service would look something like this.
    Qt Code:
    1. QMap<QPushButton*,int> counter_list;//defined as MainWindow variable
    2. void MainWindow::on_pushButton_clicked()
    3. {
    4. QPushButton *ptr = dynamic_cast<QPushButton*>(sender());
    5. if(ptr == nullptr)
    6. return;
    7.  
    8. int counter = 1;
    9. if(!counter_list.contains(ptr))
    10. counter = counter_list.value(ptr);
    11.  
    12. if(counter==1)
    13. {
    14. ptr->setStyleSheet("QPushButton { background-color: grey; }\n"
    15. "QPushButton:enabled { background-color: rgb(200,0,0,100); }\n");
    16. counter=counter+1;
    17. }
    18. else
    19. {
    20. if(counter==2)
    21. {
    22. ptr->setStyleSheet("QPushButton { background-color: grey; }\n"
    23. "QPushButton:enabled { background-color: rgb(0,200,0,100); }\n");
    24. counter=counter+1;
    25. }
    26. else
    27. {
    28. ptr-> setStyleSheet("QPushButton { }\n"
    29. "QPushButton:enabled { }\n");
    30. counter=counter-2;
    31. }
    32. }
    33. counter_list.insert(ptr,counter);
    34. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 2
    Last Post: 20th January 2017, 02:14
  2. PushButton
    By acuce in forum Qt Programming
    Replies: 2
    Last Post: 13th July 2012, 20:06
  3. pyQT how do I enable or disable a pushbutton from the main code?
    By Michael Druckenmiller Sr in forum Newbie
    Replies: 1
    Last Post: 12th January 2012, 18:44
  4. Replies: 3
    Last Post: 29th April 2011, 08:54
  5. Pasting code from code tag in emacs
    By Gopala Krishna in forum General Discussion
    Replies: 0
    Last Post: 16th February 2007, 05:47

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.