Skip to content

Add QMap to hold aUserData #3

@yurixahri

Description

@yurixahri

So i noted that aUserData in addItem() is not used, but it would be very useful because in normal combobox i can retrieve the currentData() in item row. so i came up with the implement:

/* header
 add QMap to map each checkbox to its value
*/
#include <QCheckBox>

private:
  QMap<QCheckBox*, QVariant> values;
  QList<QVariant> selectedValues;

// also add return value function
public:
  QList<QVariant> currentValues();
/* source
    in setState() check and append selected value
*/
void MultiSelectComboBox::addItem(const QString& aText, const QVariant& aUserData)
{
    //Q_UNUSED(aUserData);
   /*
      existing code
   */

    values[checkBox] = aUserData; // map checkbox to value
}

void MultiSelectComboBox::stateChanged(int aState)
{
   /*
      existing code
   */
    selectedValues.clear();      // clear

    for (int i = 1; i < count; ++i){
         /*
          existing code
       */

        if (checkBox->isChecked(){
            // existing code
            selectedValues.append(values[checkBox]);  // get selected value
        }
    }
    /*
      existing code
   */
}

QList<QVariant> MultiSelectComboBox::currentValues(){
    return selectedValues;
}

i think it's more convenient this way

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions