How to transfer a QMap from one function to another.
-
Good afternoon! Please tell the newcomer how to do it. How to transfer a QMap array from one function to another. There is a function for reading XML, I read the file I send the necessary data to Qlineedit in graphical form, and then I take this data in the function. But I decided to move away from transferring data to Qlineedit and transfer data directly. The choice fell on QMap, but I don't think of anything. Thank you. I'm completely new.
My function from where you need to transfer QMap to another.
void MainWindow::ReadXML() { { QString ReadfileName = "authentication.xml"; QString ReadfilePath = QDir::currentPath(); QFile Readfile(ReadfilePath+ '/' + ReadfileName); QMap <QString, QString> map; /* Open file */ if (!Readfile.open(QFile::ReadOnly | QFile::Text)) { QMessageBox::warning(this, "Error opening", "Error open file" "\n" "File not found?", QMessageBox::Ok); } else { QXmlStreamReader xmlReader; xmlReader.setDevice(&Readfile); //Reading while (!xmlReader.isEndDocument()) { xmlReader.readNext(); if (xmlReader.isStartElement()) { QString serv_ip = xmlReader.name().toString(); QString user = xmlReader.name().toString(); QString pass = xmlReader.name().toString(); QString port = xmlReader.name().toString(); if (serv_ip == "serverURL") { map.insert(serv_ip, xmlReader.readElementText()); ui->serverURL->setText(map.value(serv_ip)); } if (user == "username") { map.insert(user, xmlReader.readElementText()); ui->userName->setText(map.value(user)); } if (pass == "password") { map.insert(pass, xmlReader.readElementText()); ui->passWord->setText(map.value(pass)); } if (port == "dbport") { map.insert(port, xmlReader.readElementText()); ui->port_set->setText(map.value(port)); } } else if (xmlReader.isEndElement()) { xmlReader.readNext(); } } if (xmlReader.hasError()) { qDebug() << "XML error: " << xmlReader.errorString().data() << "\n"; } xmlReader.clear(); } Readfile.close(); // File close // We check and extract data from QMap. foreach (QString data, map.keys()) { qDebug() << data << ":" << map.value(data); } } } -
Good afternoon! Please tell the newcomer how to do it. How to transfer a QMap array from one function to another. There is a function for reading XML, I read the file I send the necessary data to Qlineedit in graphical form, and then I take this data in the function. But I decided to move away from transferring data to Qlineedit and transfer data directly. The choice fell on QMap, but I don't think of anything. Thank you. I'm completely new.
My function from where you need to transfer QMap to another.
void MainWindow::ReadXML() { { QString ReadfileName = "authentication.xml"; QString ReadfilePath = QDir::currentPath(); QFile Readfile(ReadfilePath+ '/' + ReadfileName); QMap <QString, QString> map; /* Open file */ if (!Readfile.open(QFile::ReadOnly | QFile::Text)) { QMessageBox::warning(this, "Error opening", "Error open file" "\n" "File not found?", QMessageBox::Ok); } else { QXmlStreamReader xmlReader; xmlReader.setDevice(&Readfile); //Reading while (!xmlReader.isEndDocument()) { xmlReader.readNext(); if (xmlReader.isStartElement()) { QString serv_ip = xmlReader.name().toString(); QString user = xmlReader.name().toString(); QString pass = xmlReader.name().toString(); QString port = xmlReader.name().toString(); if (serv_ip == "serverURL") { map.insert(serv_ip, xmlReader.readElementText()); ui->serverURL->setText(map.value(serv_ip)); } if (user == "username") { map.insert(user, xmlReader.readElementText()); ui->userName->setText(map.value(user)); } if (pass == "password") { map.insert(pass, xmlReader.readElementText()); ui->passWord->setText(map.value(pass)); } if (port == "dbport") { map.insert(port, xmlReader.readElementText()); ui->port_set->setText(map.value(port)); } } else if (xmlReader.isEndElement()) { xmlReader.readNext(); } } if (xmlReader.hasError()) { qDebug() << "XML error: " << xmlReader.errorString().data() << "\n"; } xmlReader.clear(); } Readfile.close(); // File close // We check and extract data from QMap. foreach (QString data, map.keys()) { qDebug() << data << ":" << map.value(data); } } } -
@mpergand
Thanks for your help. In public, added QMap < QString, QString > map;
and added to the main function
ui->setupUi(this);
ReadXML();
foreach (QString data, map.keys())
{
qDebug() << data << ":" << map.value(data);
}There are no errors, but there is nothing in Debug either.
-
Hi and welcome to devnet,
If you kept the implementation of ReadXml as is, you are using a local variable and thus not the member variable. You also likely have a warning about said local variable shadowing your member variable.
-
Hi and welcome to devnet,
If you kept the implementation of ReadXml as is, you are using a local variable and thus not the member variable. You also likely have a warning about said local variable shadowing your member variable.
@SGaist
Thanks for your help. I achieved thanks to your hint result)In public, added
QMap < QString, QString > map;In the function, ReadXML removed its declaration (strange why there was no conflict) and already called Qdebug in the main one.
It probably didn't work out very nicely, but I'm just learning so far.
-
@SGaist
Thanks for your help. I achieved thanks to your hint result)In public, added
QMap < QString, QString > map;In the function, ReadXML removed its declaration (strange why there was no conflict) and already called Qdebug in the main one.
It probably didn't work out very nicely, but I'm just learning so far.