PDA

View Full Version : QList, duplicate problem



MichaU1337
5th August 2014, 12:34
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.

anda_skoa
5th August 2014, 12:42
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,
_

MichaU1337
5th August 2014, 14:11
Thank you! I must be blind : o Well...this contains() changes everything...Im not sure how I missed that...

So I have something like:


QStringList ProfileList;
QString ProfileName;
ProfileName = ui->CreateProfile->text(); // QLineEdit

ProfileList << "Mark" << "Bruce" << "William"; // I created 3 values in the list

if(ProfileList.contains(ui->CreateProfile->text()))
{
QMessageBox::warning(this, tr("Nick Already Exists"),
tr("Choose another one"),
tr("Got it??"));
}
else
{
ProfileList.append(ProfileName);
}

qDebug() << ProfileList;

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

ChrisW67
5th August 2014, 21:44
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.

MichaU1337
5th August 2014, 23:58
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:

QSettings.setValue("MyProfileList", ProfileList);

and in LoadProfile() function it is:

QSetting.Value("MyProfileList");

I try to achieve something like this:

void on_AddProfileButton_clicked()
{
LoadProfiles(); //Read from QSettings, and fill the list with existing Profiles
AddProfile(); //Add Profile and do duplicate check
SaveProfiles(); //Save to QSettings new ProfileList
}

PS:Should I use settings.beginWriteArray??

Have a nice night everyone! :)

ChrisW67
6th August 2014, 21:08
QStringList profileList = QStringList() << "Mark" << "Bruce" << "William";
QSettings s;
s.setValue("MyProfileList", profileList);
// then
profileList = s.value("MyProfileList", QStringList()).toStringList();

qDebug() << profileList;

MichaU1337
7th August 2014, 01:42
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


QSettings settings("MichaU", "BeingFit");
QStringList ProfileList;
LoadProfileList();

SaveProfile() Function:

QSettings settings("MichaU", "BeingFit");
ProfileList.append(ui->CreateProfile->text()); //my lineEdit
settings.setValue("ProfileList", ProfileList);
qDebug() << "Settings Saved:" << ProfileList;

LoadProfile() Function:

QSettings settings("MichaU", "BeingFit");
settings.value("ProfileList").toStringList();
qDebug() << "Settings Loaded:" << settings.value("ProfileList").toStringList();

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

jefftee
7th August 2014, 06:02
QSettings settings("MichaU", "BeingFit");
QStringList ProfileList;
LoadProfileList();

SaveProfile() Function:

QSettings settings("MichaU", "BeingFit");
ProfileList.append(ui->CreateProfile->text()); //my lineEdit
settings.setValue("ProfileList", ProfileList);
qDebug() << "Settings Saved:" << ProfileList;

LoadProfile() Function:

QSettings settings("MichaU", "BeingFit");
settings.value("ProfileList").toStringList();
qDebug() << "Settings Loaded:" << settings.value("ProfileList").toStringList();

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:


ProfileList = settings.value("ProfileList").toStringList();

MichaU1337
7th August 2014, 10:13
Allright, we are home! :D Thanks for the help!