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,
_
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:
Code:
ProfileName = ui->CreateProfile->text(); // QLineEdit
ProfileList << "Mark" << "Bruce" << "William"; // I created 3 values in the list
if(ProfileList.contains(ui->CreateProfile->text()))
{
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?
Re: QList, duplicate problem
Quote:
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.
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:
Code:
QSettings.
setValue("MyProfileList", ProfileList
);
and in LoadProfile() function it is:
Code:
QSetting.Value("MyProfileList");
I try to achieve something like this:
Code:
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! :)
Re: QList, duplicate problem
Code:
s.setValue("MyProfileList", profileList);
// then
profileList
= s.
value("MyProfileList",
QStringList()).
toStringList();
qDebug() << profileList;
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
SaveProfile() Function:
Code:
ProfileList.append(ui->CreateProfile->text()); //my lineEdit
settings.setValue("ProfileList", ProfileList);
qDebug() << "Settings Saved:" << ProfileList;
LoadProfile() Function:
Code:
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?
Re: QList, duplicate problem
Quote:
Originally Posted by
MichaU1337
SaveProfile() Function:
Code:
ProfileList.append(ui->CreateProfile->text()); //my lineEdit
settings.setValue("ProfileList", ProfileList);
qDebug() << "Settings Saved:" << ProfileList;
LoadProfile() Function:
Code:
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:
Code:
ProfileList = settings.value("ProfileList").toStringList();
Re: QList, duplicate problem
Allright, we are home! :D Thanks for the help!