添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Welcome to Qt Centre .

Qt Centre is a community site devoted to programming in C++ using the Qt framework . Over 90 percent of questions asked here gets answered. If you are looking for information about Qt related issue — register and post your question.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today !

If you have any problems with the registration process or your account login, please contact us .

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. 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,
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: Switch view
  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;
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??"));                 
        ProfileList.append(ProfileName);
    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? 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: Switch view
  1. QSettings.setValue("MyProfileList", ProfileList);
QSettings.setValue("MyProfileList", ProfileList);
To copy to clipboard, switch view to plain text mode
and in LoadProfile() function it is:
Qt Code: Switch view
  1. QSetting.Value("MyProfileList");
QSetting.Value("MyProfileList");
To copy to clipboard, switch view to plain text mode
I try to achieve something like this:
Qt Code: Switch view
  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. }
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
To copy to clipboard, switch view to plain text mode 
PS:Should I use settings.beginWriteArray??
Have a nice night everyone!
  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;
QStringList profileList = QStringList()   << "Mark" << "Bruce" << "William"; 
QSettings s;
s.setValue("MyProfileList", profileList);
// then
profileList = s.value("MyProfileList", QStringList()).toStringList();
qDebug() << profileList;
To copy to clipboard, switch view to plain text mode 
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: Switch view
  1. QSettings settings("MichaU", "BeingFit");
  2. QStringList ProfileList;
  3. LoadProfileList();
QSettings settings("MichaU", "BeingFit");
QStringList ProfileList;
LoadProfileList();
To copy to clipboard, switch view to plain text mode 
SaveProfile() Function:
Qt Code: Switch view
  1. QSettings settings("MichaU", "BeingFit");
  2. ProfileList.append(ui->CreateProfile->text()); //my lineEdit
  3. settings.setValue("ProfileList", ProfileList);
  4. qDebug() << "Settings Saved:" << ProfileList;
QSettings settings("MichaU", "BeingFit");
    ProfileList.append(ui->CreateProfile->text()); //my lineEdit
    settings.setValue("ProfileList", ProfileList);
    qDebug() << "Settings Saved:" << ProfileList;
To copy to clipboard, switch view to plain text mode 
LoadProfile() Function:
Qt Code: Switch view
  1. QSettings settings("MichaU", "BeingFit");
  2. settings.value("ProfileList").toStringList();
  3. qDebug() << "Settings Loaded:" << settings.value("ProfileList").toStringList();
QSettings settings("MichaU", "BeingFit");
settings.value("ProfileList").toStringList();
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?
  1. QSettings settings("MichaU", "BeingFit");
  2. QStringList ProfileList;
  3. LoadProfileList();
QSettings settings("MichaU", "BeingFit");
QStringList ProfileList;
LoadProfileList();
To copy to clipboard, switch view to plain text mode 
SaveProfile() Function:
Qt Code: Switch view
  1. QSettings settings("MichaU", "BeingFit");
  2. ProfileList.append(ui->CreateProfile->text()); //my lineEdit
  3. settings.setValue("ProfileList", ProfileList);
  4. qDebug() << "Settings Saved:" << ProfileList;
QSettings settings("MichaU", "BeingFit");
    ProfileList.append(ui->CreateProfile->text()); //my lineEdit
    settings.setValue("ProfileList", ProfileList);
    qDebug() << "Settings Saved:" << ProfileList;
To copy to clipboard, switch view to plain text mode 
LoadProfile() Function:
Qt Code: Switch view
  1. QSettings settings("MichaU", "BeingFit");
  2. settings.value("ProfileList").toStringList();
  3. qDebug() << "Settings Loaded:" << settings.value("ProfileList").toStringList();
QSettings settings("MichaU", "BeingFit");
settings.value("ProfileList").toStringList();
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: Switch view
  1. ProfileList = settings.value("ProfileList").toStringList();
ProfileList = settings.value("ProfileList").toStringList();
To copy to clipboard, switch view to plain text mode