Results 1 to 9 of 9

Thread: QList, duplicate problem

  1. #1
    Join Date
    Jun 2014
    Location
    Cracow
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default QList, duplicate problem

    Hi, I'm still newbie and I have small problem with QList. I created QLineEdit and QPushButton. When I enter ProfileName and click "AddProfile" it should show up in the QList (or QStringList) but ProfileName duplicates as many times as i click.

    I have tried loops with while and for(int i = 0, i < ProfileList.size(); i++) and checked inside for existing name but its not working properly...
    Now I'm checking out QListIterator to better move through a list...but problem still exists...

    My questions are:
    1) Is there any good way / fast way to create Profiles in applications?
    2) How to check if value exists in the list, maybe I have small error inside a code...
    3) What's better: to save whole List with QSettings, or to create a loop and save each line e.g. (#1, Mark; #2, Bruce);

    PS: I ran into QStringList::find() but I cannot find it. Maybe documentation is wrong and refers to previous Qt version. It is mentioned in QList info:

    "Qt includes a QStringList class that inherits QList<QString> and adds a few convenience functions, such as QStringList::join() and QStringList::find(). (QString::split() creates QStringLists from strings.)"

    Thank You for any help Dear Community.

    Regards, Michael M.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QList, duplicate problem

    QList::contains() checks if there is an item like that, QList::indexOf can be used to find where it is.

    Without seeing your code it is impossible to tell what is wrong with it.

    Cheers,
    _

  3. #3
    Join Date
    Jun 2014
    Location
    Cracow
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QList, duplicate problem

    Thank you! I must be blind : o Well...this contains() changes everything...Im not sure how I missed that...

    So I have something like:

    Qt Code:
    1. QStringList ProfileList;
    2. QString ProfileName;
    3. ProfileName = ui->CreateProfile->text(); // QLineEdit
    4.  
    5. ProfileList << "Mark" << "Bruce" << "William"; // I created 3 values in the list
    6.  
    7. if(ProfileList.contains(ui->CreateProfile->text()))
    8. {
    9. QMessageBox::warning(this, tr("Nick Already Exists"),
    10. tr("Choose another one"),
    11. tr("Got it??"));
    12. }
    13. else
    14. {
    15. ProfileList.append(ProfileName);
    16. }
    17.  
    18. qDebug() << ProfileList;
    To copy to clipboard, switch view to plain text mode 

    So I'll expand this else code to check which line in the list isEmpty...or is there any better way?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QList, duplicate problem

    So I'll expand this else code to check which line in the list isEmpty...or is there any better way?
    A better way to do what exactly? Your code will not add "Barney" twice (but it will add "barney" or "Barney " if asked to). The list will not contain empty strings unless your code is run with an empty string and one does not already exist.
    Last edited by ChrisW67; 5th August 2014 at 22:46. Reason: updated contents

  5. #5
    Join Date
    Jun 2014
    Location
    Cracow
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QList, duplicate problem

    After several tries I have noticed the same results as you wrote. The main problem is: I dont know how to restore saved list. Any tips how to get this working?

    SaveProfile() function:
    Qt Code:
    1. QSettings.setValue("MyProfileList", ProfileList);
    To copy to clipboard, switch view to plain text mode 

    and in LoadProfile() function it is:
    Qt Code:
    1. QSetting.Value("MyProfileList");
    To copy to clipboard, switch view to plain text mode 

    I try to achieve something like this:
    Qt Code:
    1. void on_AddProfileButton_clicked()
    2. {
    3. LoadProfiles(); //Read from QSettings, and fill the list with existing Profiles
    4. AddProfile(); //Add Profile and do duplicate check
    5. SaveProfiles(); //Save to QSettings new ProfileList
    6. }
    To copy to clipboard, switch view to plain text mode 

    PS:Should I use settings.beginWriteArray??

    Have a nice night everyone!

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QList, duplicate problem

    Qt Code:
    1. QStringList profileList = QStringList() << "Mark" << "Bruce" << "William";
    2. s.setValue("MyProfileList", profileList);
    3. // then
    4. profileList = s.value("MyProfileList", QStringList()).toStringList();
    5.  
    6. qDebug() << profileList;
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jun 2014
    Location
    Cracow
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QList, duplicate problem

    Buttons work well. I can create and fill my list - ProfileList e.g. ("1","2","3","4"). When I rerun the program there is LoadProfile function, which loads exactly the same list, but when I push AddProfileButton, it creates new List with newly added element at start, deleting previous records and values entirely. Can you explain me why?

    In MainWindow.cpp

    Qt Code:
    1. QSettings settings("MichaU", "BeingFit");
    2. QStringList ProfileList;
    3. LoadProfileList();
    To copy to clipboard, switch view to plain text mode 

    SaveProfile() Function:
    Qt Code:
    1. QSettings settings("MichaU", "BeingFit");
    2. ProfileList.append(ui->CreateProfile->text()); //my lineEdit
    3. settings.setValue("ProfileList", ProfileList);
    4. qDebug() << "Settings Saved:" << ProfileList;
    To copy to clipboard, switch view to plain text mode 

    LoadProfile() Function:
    Qt Code:
    1. QSettings settings("MichaU", "BeingFit");
    2. settings.value("ProfileList").toStringList();
    3. qDebug() << "Settings Loaded:" << settings.value("ProfileList").toStringList();
    To copy to clipboard, switch view to plain text mode 

    It should be working, I dont know why it clears the list every time. Any Ideas?

  8. #8
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QList, duplicate problem

    Quote Originally Posted by MichaU1337 View Post
    Qt Code:
    1. QSettings settings("MichaU", "BeingFit");
    2. QStringList ProfileList;
    3. LoadProfileList();
    To copy to clipboard, switch view to plain text mode 

    SaveProfile() Function:
    Qt Code:
    1. QSettings settings("MichaU", "BeingFit");
    2. ProfileList.append(ui->CreateProfile->text()); //my lineEdit
    3. settings.setValue("ProfileList", ProfileList);
    4. qDebug() << "Settings Saved:" << ProfileList;
    To copy to clipboard, switch view to plain text mode 

    LoadProfile() Function:
    Qt Code:
    1. QSettings settings("MichaU", "BeingFit");
    2. settings.value("ProfileList").toStringList();
    3. qDebug() << "Settings Loaded:" << settings.value("ProfileList").toStringList();
    To copy to clipboard, switch view to plain text mode 

    It should be working, I dont know why it clears the list every time. Any Ideas?
    You need an lvalue on your statement that reads the QStringList from the settings file in the LoadProfile() function or else the QStringList is read from the settings file and immediately discarded because you haven't assigned the results to anything. Try this:
    Qt Code:
    1. ProfileList = settings.value("ProfileList").toStringList();
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jun 2014
    Location
    Cracow
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QList, duplicate problem

    Allright, we are home! Thanks for the help!

Similar Threads

  1. removing duplicate itens from QList<int>
    By john_god in forum Qt Programming
    Replies: 6
    Last Post: 7th August 2014, 07:56
  2. Problem with QSqlQuery and duplicate entry
    By Pit in forum Qt Programming
    Replies: 5
    Last Post: 27th August 2012, 12:40
  3. Replies: 4
    Last Post: 20th August 2010, 14:54
  4. Replies: 1
    Last Post: 16th June 2010, 21:19

Tags for this Thread

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.