Results 1 to 1 of 1

Thread: Dynamically Add QCheckBox

  1. #1
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Cool Dynamically Add QCheckBox

    In my application I have a group box that shows how many objects are loaded into a table. When an object is loaded into the table and it belongs to a new group I want to add a QCheckbox and labels into my group box to show there is a new grouping inside the table. A label next to the check box shows how many of that category are in the table. Then if I uncheck the box that category disappears from the table and then rechecking the box makes the items again appear in the table. For some reason the only thing that is appearing inside my group box is the total labels which are created back in a function call made by the constructor. My parent widget is just a QWidget* with the parent being the QVGroupBox and the QGridLayout's parent is the QWidget* mpCollectorWidget. Here is my code to dynamically add a check box.

    Qt Code:
    1. bool MainWindow::addCollector( QString collector )
    2. {
    3. int result = -1;
    4. QCheckBox *button;
    5. QLabel *total;
    6.  
    7. // Remove the bottom row from the grid layout in case I need to add a new checkbox
    8. if( mTotalCollectors > 0 )
    9. {
    10. mpCollectorLayout->remove( mpTotalTextLabel );
    11. mpCollectorLayout->remove( mpTotalNumberLabel );
    12. }
    13.  
    14. // Search the QStringList to see if collector is in it
    15. int found;
    16. for( int index = 0 ; index < mCollectors.size( ) ; index++ )
    17. {
    18. found = ( *( mCollectors.at( index ) ) ).find( collector );
    19. if( found != -1 )
    20. {
    21. result = index;
    22. break;
    23. }
    24. }
    25.  
    26. // The item was not found so create the checkbox and add it to the layout
    27. if( result == -1 )
    28. {
    29. button = new QCheckBox( collector, mpCollectorWidget );
    30. total = new QLabel( QString::number( 1 ), mpCollectorWidget );
    31. button->setChecked( true );
    32.  
    33. mpCollectorLayout->addWidget( button, mCollectors.size( ), 0 );
    34. mpCollectorLayout->addWidget( total, mCollectors.size( ), 1 );
    35.  
    36. // Add the button and label to a QValueVector of buttons and labels for this group box
    37. mCCheckBoxes.push_back( button );
    38. mCTotalLabels.push_back( total );
    39. // Add the collector name to the QString list of already seen choices
    40. mCollectors.push_back( collector );
    41. }
    42. else
    43. {
    44. QLabel *amt = *( mCTotalLabels.at( result ) );
    45. int count = ( amt->text( ) ).toInt( );
    46. // Increment count so there is 1 more of that type loaded into the table
    47. count++;
    48. amt->setText( QString::number( count ) );
    49. }
    50.  
    51. // Increment the total number of open items
    52. mTotalCollectors++;
    53. mpTotalNumberLabel->setText( QString::number( mTotalCollectors ) );
    54.  
    55. // Add the previously removed items to the end of the QGridLayout
    56. mpCollectorLayout->addWidget( mpTotalTextLabel );
    57. mpCollectorLayout->addWidget( mpTotalNumberLabel );
    58.  
    59. return true;
    60. }
    To copy to clipboard, switch view to plain text mode 

    When I first load the program the box should be empty. Then when I add 1 item a new checkbox followed by its label and then the number 1 should appear. Under it a total should appear preceded by the word "Total: ". For example if the first data I load is in the group "baseball" the groupbox should look like:

    (CheckBox) Baseball: 1
    Total: 1

    Then if I load in two more items, one being baseball and the other being soccer I should see:

    (CheckBox) Baseball: 2
    (CheckBox Soccer: 1
    Total: 3

    Unfortunately, after loading one item I see nothing in the box, and after loading 2+ items I see:

    Total: 2+

    Can you see from my code why my check boxes are not appearing in the QVGroupBox correctly? Sorry if there is an error in the code, my development machine is not on-line so I have to retype all my code by hand to post on here. Thanks for your help!

    Edit: It appears I have to call a show( ) command for my newly created QCheckBox and QLabel to get them to appear. This leads me to a new question however. Why do I have to call show( ) on these new widgets when I didn't have to call show on my widgets I created at the same time as the layouts? Thanks again!
    Last edited by ToddAtWSU; 22nd January 2007 at 20:00.

Similar Threads

  1. Dynamically changing QFrame color
    By Doug Broadwell in forum Newbie
    Replies: 6
    Last Post: 16th October 2008, 08:22
  2. Dynamically adding tabs
    By larry104 in forum Qt Programming
    Replies: 7
    Last Post: 26th July 2006, 20:27
  3. Dynamically Loading a QMainWindow?
    By natron in forum Newbie
    Replies: 10
    Last Post: 21st July 2006, 01:15
  4. Resizing the dialog box dynamically
    By vvbkumar in forum Newbie
    Replies: 5
    Last Post: 20th June 2006, 08:54
  5. Adding/removing widgets dynamically...
    By ReilenBlaeyze in forum Newbie
    Replies: 2
    Last Post: 16th February 2006, 10:55

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.