Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to transfer a QMap from one function to another.
Forum Updated to NodeBB v4.3 + New Features

How to transfer a QMap from one function to another.

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 472 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kipalex
    wrote on last edited by
    #1

    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);
                        }
    
        }
    }
    
    M 1 Reply Last reply
    0
    • K kipalex

      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);
                          }
      
          }
      }
      
      M Offline
      M Offline
      mpergand
      wrote on last edited by
      #2

      @kipalex
      Define your QMap as member variable of MainWindow.

      K 1 Reply Last reply
      1
      • M mpergand

        @kipalex
        Define your QMap as member variable of MainWindow.

        K Offline
        K Offline
        kipalex
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          K 1 Reply Last reply
          2
          • SGaistS SGaist

            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.

            K Offline
            K Offline
            kipalex
            wrote on last edited by Chris Kawa
            #5

            @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.

            SGaistS 1 Reply Last reply
            0
            • K kipalex

              @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.

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @kipalex the conflict is notified to you through the warning (ALWAYS FIX THE WARNINGS).

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved