Results 1 to 5 of 5

Thread: Compare two QLists<STRUCT>

  1. #1
    Join Date
    Oct 2015
    Posts
    50
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Compare two QLists<STRUCT>

    Hi,

    I have 2 lists of the struct DataInfo.
    Qt Code:
    1. struct AvailableData{
    2. quint8 type;
    3. quint8 resolution;
    4. quint8 quantity;
    5. QString unit;
    6. QString name;
    7. };
    8.  
    9. struct DataInfo{
    10. AvailableData Info;
    11. quint8 flag;
    12. quint8 number;
    13. };
    14. ...
    15. QList<DataInfo> AllData;
    16. QList<DataInfo> SelectedDataWithInfo;
    To copy to clipboard, switch view to plain text mode 

    I fill the first list like this:

    Qt Code:
    1. for (int j = 1; j <= DataList.at(i).quantity; j++){
    2. DataInfo tempInfo;
    3. tempInfo.Info = DataList.at(i);
    4. tempInfo.number = j;
    5. tempInfo.flag = i;
    6. AllData.append(tempInfo);
    7. }
    To copy to clipboard, switch view to plain text mode 

    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.

    Qt Code:
    1. QList<QCheckBox *> CheckBoxes = ui->AvailableDataWidget->findChildren<QCheckBox *>();
    2. for(int i = 0; i < CheckBoxes.size(); ++i){
    3. if(CheckBoxes.at(i)->isChecked()){
    4. ui->transducerTable->showColumn(i+2);
    5.  
    6. if (!SelectedDataWithInfo.contains(AllData.at(i))){
    7. SelectedDataWithInfo.append(AllData.at(i));
    8. }
    9. }
    10. else{
    11. ui->transducerTable->hideColumn(i+2);
    12. SelectedDataWithInfo.removeOne(AllData.at(i));
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

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

    What exactly did I wrong?

  2. #2
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Compare two QLists<STRUCT>

    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:
    Qt Code:
    1. struct DataInfo{
    2. AvailableData Info;
    3. quint8 flag;
    4. quint8 number;
    5.  
    6. bool operator==(const DataInfo& dInfo) const
    7. {
    8. if(number == dInfo.number)
    9. return true;
    10.  
    11. return false;
    12. }
    13. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by Vikram.Saralaya; 10th December 2015 at 13:19.

  3. The following user says thank you to Vikram.Saralaya for this useful post:

    mikrocat (10th December 2015)

  4. #3
    Join Date
    Oct 2015
    Posts
    50
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Compare two QLists<STRUCT>

    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.

  5. #4
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Compare two QLists<STRUCT>

    haha! I encountered it off late.. few years down the line I might feel the same

  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Compare two QLists<STRUCT>

    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.

Similar Threads

  1. QList of QLists
    By m15ch4 in forum Newbie
    Replies: 12
    Last Post: 16th October 2013, 23:36
  2. Sorting a series of QLists into the same order
    By mobucl in forum Qt Programming
    Replies: 8
    Last Post: 25th February 2011, 18:15
  3. Renaming multidimensional QLists
    By mobucl in forum Newbie
    Replies: 3
    Last Post: 17th December 2010, 10:04
  4. help using QLists as data in QwtPlotCurve
    By esorensen in forum Qwt
    Replies: 1
    Last Post: 11th July 2008, 20:52
  5. QList of Qlists?
    By markcole in forum Qt Programming
    Replies: 6
    Last Post: 8th July 2008, 06:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.