Qt-MultiSelectComboBox icon indicating copy to clipboard operation
Qt-MultiSelectComboBox copied to clipboard

Add QMap to hold aUserData

Open yurixahri opened this issue 2 months ago • 0 comments

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

yurixahri avatar Nov 24 '25 10:11 yurixahri