Results 1 to 2 of 2

Thread: Function to set bg-color & text of label and pass instance of an object

  1. #1
    Join Date
    Jan 2012
    Posts
    23
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Function to set bg-color & text of label and pass instance of an object

    Hi there,

    i program an Operationcenter for our local fire department (our younger members ( < 13 years old) use it on special training courses).
    To monitor all vehicles' state i designed a Status-Monitor. If a vehicle changes his state (e.g. has an accident and is not ready for any incidents) there are several things to do for the program:
    1.) Change color of a label
    2.) Change text of a label

    These tasks are very simple to program but now to my real problem.
    There are 9 possible states. Each vehicle has its own groupbox and combobox. With the combobox and a pushbutton the user can choose the current state and set it.

    Here is a screenshot:
    captureeta.png

    Now to my question:
    Is there a possible way to create a function that gets two arguments (id of state and instance of label to where colour and text shall be applied to) instead of creating a function like this (its just the beginning of a function not the complete one!):
    Qt Code:
    1. void ControlPanel::setStatus(int id, int boxId)
    2. {
    3. if(boxId == 1){
    4. switch(id){
    5. case 1:
    6. this->lblStatus1->setStyleSheet("background-color: rgb(181, 217, 164);");
    7. this->lblStatus1->setText("1");
    8. break;
    9. case 2:
    10. this->lblStatus1->setStyleSheet("background-color: rgb(121, 185, 92);");
    11. this->lblStatus1->setText("2");
    12. break;
    13. case 3:
    14. this->lblStatus1->setStyleSheet("background-color: rgb(244, 209, 81);");
    15. this->lblStatus1->setText("3");
    16. break;
    17. case 4:
    18. this->lblStatus1->setStyleSheet("background-color: rgb(239, 174, 77);");
    19. this->lblStatus1->setText("4");
    20. break;
    21. case 5:
    22. this->lblStatus1->setStyleSheet("background-color: rgb(239, 174, 77);");
    23. this->lblStatus1->setText("4");
    24. break;
    25. }
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

    I mean the main thing i want to know is how i can pass an instance of an object to a function so that i dont need the if(boxId == 1) thing?
    Are there maybe better and more efficient ways? Dont want to waste 100 lines of code if there is a better solution with less lines


    Greetz

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Function to set bg-color & text of label and pass instance of an object

    Try passing the pointer to the label, i.e.lblStatus1 ,lblStatus2 etc., rather than the index:
    Qt Code:
    1. void ControlPanel::setStatus(int id, QLabel *label) {
    2. Q_ASSERT(label); // to catch yourself if you pass a NULL during development
    3. switch(id) {
    4. case 1:
    5. label->setText(...);
    6. label->setStyleSheet(...);
    7. break;
    8. case 2:
    9. // stuff for id 2
    10. break;
    11. ....
    12. default:
    13. // Unexpected value?
    14. break;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 
    You might then ask yourself if there are more efficient ways to do this as well, e.g. is the label text is the same as the id number, can you derive or lookup the colour value?

Similar Threads

  1. Multiple instance of Plugin object
    By mcosta in forum Qt Programming
    Replies: 3
    Last Post: 15th June 2011, 16:11
  2. What determines color of text shown as axis label?
    By DrunkenUFOPilot in forum Qwt
    Replies: 1
    Last Post: 24th March 2011, 18:55
  3. Replies: 0
    Last Post: 2nd August 2010, 09:37
  4. Display Label Color by selecting Color Picker
    By sosanjay in forum Qt Programming
    Replies: 1
    Last Post: 25th September 2009, 06:11
  5. Label text color
    By maverick_pol in forum Qt Programming
    Replies: 3
    Last Post: 27th February 2008, 09:49

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.