Config File Overwritten in Qt - qt

I am creating a config file which stores username, password and role of certain user. I am using the following code.
void MainWindow::OnAssignButtonClicked()
{
QSettings settings("/root/configFile.ini", QSettings::IniFormat);
QString userName = lineEditUsername.text();
QString password = lineEditPassword.text();
QString Role = comboBox.currentText();
QList<QString> listUsername;
QList<QString> listPassword;
QList<QString> listRole;
QVariantMap userPasswordMapping;
QVariantMap userRoleMapping;
listUsername << userName;
listPassWord << Password;
listRole << Role;
for(int i = 0; i < listUsername.size() ; i++)
{
QString user = listUsername.at(i);
QString pass = listPassword.at(i);
QString role = listRole.at(i);
userPasswordMapping[user] = pass;
userRoleMapping[user] = role;
}
// Store the mapping.
settings.setValue("Password", userPasswordMapping);
settings.setValue("Role",userRoleMapping);
}
While Reading the Values
QVariant variantPassword = settings.value("Password");
QVariant variantRole = settings.value("Password");
QVariantMap mapPassword = variantPassword.value<QVariantMap>();
QVariantMap mapRole = variantRole.value<QVariantMap>();
QMapIterator<QString, QVariant> iteratorPassword(mapPassword);
QMapIterator<QString, QVariant>iteratorRole(mapRole);
while (iteratorPassword.hasNext())
{
iteratorPassword.next();
QString user = iteratorPassword.key();
QString pass = iteratorPassword.value().toString();
iteratorRole.next();
QString role = iteratorRole.value().toString();
}
The first time the value gets written correctly. However if I run the program again the value over writes the old values. Can some one please suggest me a solution here?
Thank You

Every time you click your 'assign' button, you create new mappings, store the user, password and role in there and save that list to the config file.
What is missing is that you read the existing mappings before modifying them.
Instead of
QVariantMap userPasswordMapping;
QVariantMap userRoleMapping;
do:
QVariantMap userPasswordMapping = settings.value("Password").value<QVariantMap>();
QVariantMap userRoleMapping = settings.value("Role").value<QVariantMap>();
Additional hint about the code you posted about reading the values:
QVariant variantRole = settings.value("Password");
this should be
.value("Role");

Related

Converting QMap<QString, QString> to Json string results empty

I have a function defined and used as this:
// usage:
QMap<QString, QString> map = ...;
foo(map);
// defination:
QString stringMapToJson(const QMap<QString, QString>& arg) {
QVariant v = QVariant::fromValue(arg);
JsonDocument doc = QJsonDocument::fromVariant(v);
...
}
Then I realized v is empty.
Is there a method to convert QMap<String, QString> to QMap<String, QVariant>, so above v could be valid?
Why above v is empty? I read people were saying QVariant and qMetaData, I don't understand given the following valid, why QString have a qMetaData problem:
QString s = "";
QVariant v = s;
(A Java programmer starts her pleasant C++ journey.)
Thanks.
There are 2 ways to do this. The first is to convert your map to a QMap<QString, QVariant> like you mentioned:
QByteArray stringMapToJson1(const QMap<QString, QString>& arg)
{
QVariantMap vmap;
for(auto it = arg.cbegin(); it != arg.cend(); ++it)
{
vmap.insert(it.key(), it.value());
}
const QVariant v = QVariant::fromValue(vmap);
const QJsonDocument doc = QJsonDocument::fromVariant(v);
return doc.toJson();
}
Alternatively, you can build the json object directly from the map. In this case it's the same amount of code:
QByteArray stringMapToJson2(const QMap<QString, QString>& arg)
{
QJsonObject jObj;
for(auto it = arg.cbegin(); it != arg.cend(); ++it)
{
jObj.insert(it.key(), it.value());
}
QJsonDocument doc;
doc.setObject(jObj);
return doc.toJson();
}
This seems like a stylistic choice and I am unsure which would be faster. Both produce the same output.
One thing to note: The conversion from QString to QVariant is predefined in Qt, so the first method works fine. For objects of your own classes you would have to register that type and provide a suitable conversion which can be a bit tough to get right. In the second method you could do this conversion inline in the loop.

Qt how to extract custom data pointer from QVariant without knowing the internal type

I have a QVariantMap with data. Inside the QVariant I can store custom class declared and registered in meta-system.
I know does exists QVariant::value<XXX>() but since i don't know a-priori what XXX is I cannot do that.
So I tried to convert to QObject* both with QVariant::value<QObject*> and qvariant_cast but it seems to hold a void pointer:
My attempt:
MyClass* obj = new MyClass();
QVariant variant = QVariant::fromValue<MyClass*>(obj); // it works;
qDebug() << variant; // Qvariant(MyClass*,)
QObject* obj2 = variant.value<QObject*>();
qDebug() << obj2; // QObject(0x0)
The OP mentions it works with:
QVariant variant(QVariant::fromValue(static_cast<void*>(obj)));
auto test = static_cast<QObject*>(variant.value<void*>());
qDebug() << obj2; // MyClass(0x8a99838
As specified in this answer

Reading from .can files

I want to read .can files in Qt, I found out it is similiar to ini files so i used QSettings::IniFormat, i look for 2 attributes( say "rate" and "name").
code:
for(int i=0; i<files.count();i++)
{
QSettings file(files[i], QSettings::IniFormat);
QStringList keys = file.allKeys();
foreach(const QString& key, keys)
{
if(key.endsWith("/rate"))
{
QString Rate = file.value(key).toString();
qDebug() << Rate;
}
if(key.endsWith("/name"))
{
QString name = file.value(key).toString();
qDebug()<<name;
}
Problem is my can files has lot of "name" attribute, so this method is returning all the "name" attributes. I want to store the "name" attribute which the program finds right after "rate", there can be "name" attribute before "rate", so i just want to store the attribute which the program finds immediately after it finds "rate".
I don't know about .can files, I searched a little about them but couldn't find anything about them related to ini format.
Anyways, I rewrote your code to output the very first name attribute it finds after each rate encountered.
bool rateAttrFound = false;
for(int i=0; i<files.count();i++)
{
QSettings file(files[i], QSettings::IniFormat);
QStringList keys = file.allKeys();
foreach(const QString& key, keys)
{
if(key.endsWith("/rate"))
rateAttrFound = true;
if(key.endsWith("/name"))
{
if(rateAttrFound){
qDebug() << file.value(key).toString();
rateAttrFound = false;
}
}
}
}

Saving a qlistwidget after closing application

I have a program that allows for a user to create a profile that saves values using qsettings, the user accesses their profile by clicking on the name in a qlistwidget. I am trying to save the names of the profiles by using a text file but I am having trouble saving more than one profile name at a time. thank you! here is the code:
for saving a profilename to the text document
void Profile::writeProfilenames()
{
QString profilename = ui->lineEdit_profilename->text();
profilename = profilename.simplified();
QFile pfile("profilenames.txt");
if (!pfile.open(QFile::WriteOnly | QIODevice::Text))
{
return;
}
QTextStream out(&pfile);
out << profilename;
pfile.flush();
pfile.close();
}
for retrieving the profile names from the document
void Profile::readProfilenames()
{
QFile pfile("profilenames.txt");
if (!pfile.open(QIODevice::ReadOnly |
QIODevice::Text))
{
return;
}
QString proname = pfile.readLine();
QListWidgetItem *itm = new QListWidgetItem;
itm->setText(proname);
ui->listWidget_profiles->insertItem(0,itm);
}
P.S. if you know of a better way to do this then feel free to share! (with example please)
I don't quite see why you're saving the list of names in a text file, while the settings themselves are saved in a platform-specific fashion using QSettings.
The code you show has several problems:
Presumably you don't want to "write" the name to the file, overwriting the existing contents at the beginning, but specifically to append to the file. You also must specify a writable path to the file, so far you're using the current working directory that is: variable, not under your control, and not necessarily writable. Your code also doesn't handle repeated names.
QFile is a proper C++ class, and embodies the RAII principles. You don't have to do anything to flush and close the file. The compiler takes care of generating the proper code for you. That's why you're using C++ and not C, after all. Yes, your code compiles, but it reads like C, and such verbosity is unnecessary and counterproductive.
You're only retrieving one name from the file. You want to retrieve all of them.
I'd say that you should dispense with the file access, set up your application's identification, a crucial prerequisite to using QSettings, and, finally, use them:
struct Profile {
QString name;
int age;
}
void saveProfiles(const QList<Profile> & profiles)
{
QSettings s;
s.beginWriteArray("profiles");
for (int i = 0; i < profiles.size(); ++i) {
s.setArrayIndex(i);
const Profile & p = profiles.at(i);
s.setValue("name", p.name);
s.setValue("age", p.age);
}
s.endArray(); //optional
}
QList<Profile> loadProfiles()
{
QList<Profile> profiles;
QSettings s;
int size = s.beginReadArray("profiles");
for (int i = 0; i < size; ++i) {
s.setArrayIndex(i);
Profile p;
p.name = s.value("name").toString();
p.age = s.value("age").toInt();
profiles << p;
}
s.endArray(); // optional
return profiles;
}
int main(int argc, char ** argv) {
QApplication app(argc, argv);
app.setOrganizationName("fluxD613"); // ideally use setOrganizationDomain instead
app.setApplicationName("fluxer");
...
return app.exec();
}
After a lot more research and trial and error I came up with the following code that does the trick:
this function is implemented when I close the profiles dialog window and return to the main window using QCloseEvent.
void Profile::writeProfilenames()
{
QFile pfile("profilenames.txt");
if (!pfile.open(QFile::WriteOnly | QIODevice::Text))
{
return;
}
for(int row = 0; row < ui->listWidget_profiles->count(); row++)
{
QListWidgetItem *item = ui->listWidget_profiles->item(row);
QTextStream out(&pfile);
out << item->text().simplified() << "\n";
}
pfile.close();
}
reading the list of profilenames is implemented when I open the dialog window just under ui->setup(this).
void Profile::readProfilenames()
{
QFile pfile("profilenames.txt");
if (!pfile.open(QIODevice::ReadOnly |
QIODevice::Text))
{
return;
}
QTextStream in(&pfile);
while (!in.atEnd())
{
QString line = in.readLine();
QListWidgetItem *item = new QListWidgetItem;
item->setText(line);
ui->listWidget_profiles->addItem(item);
}
pfile.close();
}
I am now working on making sure the user does not enter a profilename that already exists and deleting a profilename from the QListWidget.

change QCompleter result in QLineEdit

The QLineEdit is for entering post code. User may also input city name, while QCompleter will display a list of names for user to select. The problem is, on selecting the name in completer, how could the post code be put in the QLineEdit?
I tried to connect QCompleter::activated(QModelIndex) to slot that change the QLineEdit text to post code. But later the text was again set to city name by QLineEdit.
Sorry, my previous answer was not correct, so I've edited it.
As the documentation says:
QString QCompleter::pathFromIndex ( const QModelIndex & index ) const
[virtual]
Returns the path for the given index. The completer object
uses this to obtain the completion text from the underlying model. The
default implementation returns the edit role of the item for list
models. It returns the absolute file path if the model is a QDirModel.
I've got what you need by subclassing QCompleter and reimplementing pathFromIndex:
class CodeCompleter : public QCompleter
{
Q_OBJECT
public:
explicit CodeCompleter(QObject *parent = 0);
static const int CompleteRole;
QString pathFromIndex(const QModelIndex &index) const;
};
const int CodeCompleter::CompleteRole = Qt::UserRole + 1;
CodeCompleter::CodeCompleter(QObject *parent) :
QCompleter(parent)
{
}
QString
CodeCompleter::pathFromIndex(const QModelIndex &index) const
{
QMap<int, QVariant> data = model()->itemData(index);
QString code = data.value(CompleteRole).toString();
return code;
}
And you can use it like this:
QStringList cities;
cities << "Moscow" << "London" << "Las Vegas" << "New York";
QStandardItemModel *model = new QStandardItemModel;
for (int i = 0; i < cities.count(); ++i)
{
QString city = cities.at(i);
QString code = city.at(0) + QString::number(city.length());///< just an example
QStandardItem *item = new QStandardItem;
item->setText(city);
item->setData(code, CodeCompleter::CompleteRole);
model->appendRow(item);
}
QLineEdit *lineEdit = new QLineEdit(this);
CodeCompleter *completer = new CodeCompleter(this);
completer->setModel(model);
completer->setCaseSensitivity(Qt::CaseInsensitive);
lineEdit->setCompleter(completer);

Resources