PDA

View Full Version : Compare two QLists<STRUCT>



mikrocat
10th December 2015, 11:50
Hi,

I have 2 lists of the struct DataInfo.


struct AvailableData{
quint8 type;
quint8 resolution;
quint8 quantity;
QString unit;
QString name;
};

struct DataInfo{
AvailableData Info;
quint8 flag;
quint8 number;
};
...
QList<DataInfo> AllData;
QList<DataInfo> SelectedDataWithInfo;


I fill the first list like this:



for (int j = 1; j <= DataList.at(i).quantity; j++){
DataInfo tempInfo;
tempInfo.Info = DataList.at(i);
tempInfo.number = j;
tempInfo.flag = i;
AllData.append(tempInfo);
}

Now the user of my software is able to select via checkboxes certain members of the list. So i want to create another list "SelectedDataWithInfo" which contains the selected Data.



QList<QCheckBox *> CheckBoxes = ui->AvailableDataWidget->findChildren<QCheckBox *>();
for(int i = 0; i < CheckBoxes.size(); ++i){
if(CheckBoxes.at(i)->isChecked()){
ui->transducerTable->showColumn(i+2);

if (!SelectedDataWithInfo.contains(AllData.at(i))){
SelectedDataWithInfo.append(AllData.at(i));
}
}
else{
ui->transducerTable->hideColumn(i+2);
SelectedDataWithInfo.removeOne(AllData.at(i));
}
}

But it doesn't work, it says: "no match for 'operator==' (operand types are 'MonitorWindow::DataInfo' and 'const MonitorWindow::DataInfo')
if (i->t() == t)"

What exactly did I wrong?

Vikram.Saralaya
10th December 2015, 12:15
The QList documentation says:

QList's value type must be an assignable data type. This covers most data types that are commonly used, but the compiler won't let you, for example, store a QWidget as a value; instead, store a QWidget *. A few functions have additional requirements; for example, indexOf() and lastIndexOf() expect the value type to support operator==(). These requirements are documented on a per-function basis.

Basically if you are storing your custom data types by value in a QList (DataInfo and AvailableData in your case) you will most likely need to define a few additional operators depending on the QList methods that you will be accessing.

Since you are using QList::removeOne(DataInfoValue) QList needs to match all the values in the list and find the one you specified which it can remove. As the documentation for this function says:
This function requires the value type to have an implementation of operator==()

So something like this should fix the problem:

struct DataInfo{
AvailableData Info;
quint8 flag;
quint8 number;

bool operator==(const DataInfo& dInfo) const
{
if(number == dInfo.number)
return true;

return false;
}
};

mikrocat
10th December 2015, 13:32
Thank You. This is exactly what i need. I remember that I heard of this years ago but already forgot it. I'm getting old.

Vikram.Saralaya
10th December 2015, 13:44
haha! I encountered it off late.. few years down the line I might feel the same ;)

d_stranz
10th December 2015, 18:48
This is exactly what i need.

But you do realize that each of your two QLists contain *different* instances of DataInfo, don't you? By pushing instances by value as Vikram said, you are making copies of the struct as new instances on each list. Maybe that's what you want, but realize that if you change one of the instances on one of the lists, it has absolutely no effect on the "same" instance on the other list. They are duplicate copies and have no relationship to each other.