Using the GroupBoxes in Qt

4.1k views Asked by At

I'm a student programmer and I am using Qt to build a GUI application. I'm trying to ensure that some check boxes are checked in order to proceed. These check boxes enable or disable the group box itself and are part of the QGroupBox class. Accepted combinations could be either or both. The problem I am running into is getting the boolean value(at least that's what I think it is) from the QGroupBox member function QGroupBox::setChecked(bool) and use it to determine whether or not to display an error message. I have tried several methods and reference Qts documentation hoping for a good example. Because the QGroupBox I'm trying to use is a member of my ui class I tried creating a new instance of QGroupBox and setting it to the values of the ui. Then; using an if statement, find out whether or not the boxes are check or not. Here's my code for this:

QGroupBox activeParticleInjection = ui->groupBoxParticleInjection;
    QGroupBox activeFluidInjection = ui->groupBoxFluidInjection;
    if (activeParticleInjection::setChecked(false) && activeFluidInjection(false));
    {
        QMessageBox noInjectionSelectedError;
        noInjectionSelectedError.setText("Error: No injection type selected");
        noInjectionSelectedError.exec();
    }
    else
    {
        transData.particleInjectionActive = activeParticleInjection::setChecked();
        transData.fluidInjectionActive = activeFluidInjection::setChecked();

Compile and code

This doesn't work; starting with the way I'm trying to pass the Ui properties to the new instance of QGroupBox. I know that is question is relatively generic question but I tried passing the ui checkbox directly and that caused even more issues. I looked through the documentation and that led me to the way im trying to do it know; with no luck. I was hoping for some feedback on a better method of handling QGroupBox. Being a student sometimes its hard to see the answer especially when dealing with such unique members as the ones put together in QT.

Prior to changes I was using this method to build this process; and I received errors for the way my if parameters were setup. Compile error was : no matching function for call to 'QGroupBox::isChecked(bool)'

if (ui->groupBoxFluidInjection->isChecked(false) && ui->groupBoxParticleInjection->isChecked(false));
    {
        QMessageBox noInjectionSelectedError;
        noInjectionSelectedError.setText("Error: No injection type selected");
        noInjectionSelectedError.exec();
    }
    else
    {
        transData.particleInjectionActive = ui->groupBoxParticleInjection->isChecked();
        transData.fluidInjectionActive = ui->groupBoxFluidInjection->isChecked();
    }

I have been using these sites for most help: QGroupBox QCheckBox

2

There are 2 answers

1
Frank Osterfeld On BEST ANSWER

You're mixing several things:

  • The QGroupBox instances in ui are pointers, so you must assign them to a QGroupBox* (pointer), not a QGroupBox (object on the stack).
  • :: is used with methods only if the method you call is static (class method), which is not the case here.
  • setChecked() is a setter setting the checked state of the group box. It doesn't return anything (void), so you cannot use them as conditions. What you want there is the getter bool QGroupBox::isChecked().

Your code snippet cleansed:

QGroupBox* activeParticleInjection = ui->groupBoxParticleInjection;
QGroupBox* activeFluidInjection = ui->groupBoxFluidInjection;
if (!activeParticleInjection->isChecked() && !activeFluidInjection->isChecked())
{
    QMessageBox::critical(this, tr("Error"), tr("No injection type selected"));
}
else 
{
    transData.particleInjectionActive = activeParticleInjection->isChecked();
    transData.fluidInjectionActive = activeFluidInjection->isChecked();
}
1
Mario On

Not really sure what you tried to do in your code, but that's actually a lot easier to achieve. Also don't try to create copies (which you do in your example). Work with references or pointers in that case!

QGroupBox group("my group box"); // of course this might be a class member, too
group.setCheckable(true);

// do other things...

if(group.isChecked())
{
    // do whatever if it's checked
}
else
{
    // do stuff if it isn't checked
}

To check for at least one of multiple groups (or check boxes) being checked:

if(group1.isChecked() || group2.isChecked())