PDA

View Full Version : Dynamically Add QCheckBox



ToddAtWSU
22nd January 2007, 19:00
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.



bool MainWindow::addCollector( QString collector )
{
int result = -1;
QCheckBox *button;
QLabel *total;

// Remove the bottom row from the grid layout in case I need to add a new checkbox
if( mTotalCollectors > 0 )
{
mpCollectorLayout->remove( mpTotalTextLabel );
mpCollectorLayout->remove( mpTotalNumberLabel );
}

// Search the QStringList to see if collector is in it
int found;
for( int index = 0 ; index < mCollectors.size( ) ; index++ )
{
found = ( *( mCollectors.at( index ) ) ).find( collector );
if( found != -1 )
{
result = index;
break;
}
}

// The item was not found so create the checkbox and add it to the layout
if( result == -1 )
{
button = new QCheckBox( collector, mpCollectorWidget );
total = new QLabel( QString::number( 1 ), mpCollectorWidget );
button->setChecked( true );

mpCollectorLayout->addWidget( button, mCollectors.size( ), 0 );
mpCollectorLayout->addWidget( total, mCollectors.size( ), 1 );

// Add the button and label to a QValueVector of buttons and labels for this group box
mCCheckBoxes.push_back( button );
mCTotalLabels.push_back( total );
// Add the collector name to the QString list of already seen choices
mCollectors.push_back( collector );
}
else
{
QLabel *amt = *( mCTotalLabels.at( result ) );
int count = ( amt->text( ) ).toInt( );
// Increment count so there is 1 more of that type loaded into the table
count++;
amt->setText( QString::number( count ) );
}

// Increment the total number of open items
mTotalCollectors++;
mpTotalNumberLabel->setText( QString::number( mTotalCollectors ) );

// Add the previously removed items to the end of the QGridLayout
mpCollectorLayout->addWidget( mpTotalTextLabel );
mpCollectorLayout->addWidget( mpTotalNumberLabel );

return true;
}


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!