Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved Can't see .xml file from build directory

    General and Desktop
    qfile xml console app xml parsing
    3
    6
    215
    Loading More Posts
    • 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.
    • BeaverShallBurn
      BeaverShallBurn last edited by BeaverShallBurn

      Welcome to my problem, people!

      After exploring Qt docs about retrieving info from .xml files (QXmlStreamReader) i've decided to practice a bit before trying to implement this feature in my app. Clumsy, but fairly simple code has been written:

          QTextStream qtin(stdin);
          QTextStream qout(stdout);
          QString path = /*QDir::currentPath() + */"config.xml";
          QFile* file = new QFile(path);
          if (!file->exists())
          {
              std::cout << "File not found" << std::endl;
              qout << QDir::currentPath() << endl << file->errorString();
              return false;
          }
          if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
          {
              qout << file->errorString() << endl << file->error();
              return false;
          }
          QString reg = qtin.readLine();;
          uint16_t x;
          QXmlStreamReader xml(file);
          QXmlStreamAttributes attribute = xml.attributes();
          while (!xml.atEnd() && !xml.hasError())
          {
              QXmlStreamReader::TokenType token = xml.readNext();
              if (token == QXmlStreamReader::StartDocument)
                  continue;
              if (token == QXmlStreamReader::StartElement)
              {
                  if (xml.name() == "param")
                  {
                      if (attribute.hasAttribute(reg))
                          x = attribute.value("key").toInt();
                      std::cout << x << std::endl;
                      xml.readNext();
                  }
                  xml.readNext();
              }
          }
          file->close();
          system("pause");
      

      The thing is, it cannot detect my .xml file, responsibly placed in build directory (build-test-Desktop-...). After a little research, i've added file to .pro file via

      RESOURCE += config.xml
      

      This is a very basic console application, written without classes and header-files, so i felt no need to construct QCoreApplication function here.

      File is there, visible to everyone but my app. And i'm here, thinking about what could have gone wrong.

      Welcoming any suggestions and hints.

      -Knock knock
      -Who's there?
      -Compi
      -Compi who?
      -Compilation error

      sierdzio 1 Reply Last reply Reply Quote 0
      • sierdzio
        sierdzio Moderators @BeaverShallBurn last edited by

        @BeaverShallBurn said in Can't see .xml file from build directory:

        The thing is, it cannot detect my .xml file, responsibly placed in build directory (build-test-Desktop-...). After a little research, i've added file to .pro file via

        RESOURCE += config.xml
        

        You should probably use QRC files instead to hold your resources.

        This is a very basic console application, written without classes and header-files, so i felt no need to construct QCoreApplication function here.

        This is very wrong! QFile inherits from QObject and thus requires QCoreApplication instance to be present. If you don't have it, most Qt classes can misbehave.

        The thing is, it cannot detect my .xml file

        Are you sure you are looking in the right directory? Is working dir inside of your build dir? The usual practice is to use QCoreApplication here:

        QString path = QCoreApplication::instance()->applicationDirPath() + "/config.xml";
        

        (Z(:^

        BeaverShallBurn 1 Reply Last reply Reply Quote 3
        • sierdzio
          sierdzio Moderators @BeaverShallBurn last edited by

          @BeaverShallBurn said in Can't see .xml file from build directory:

          The thing is, it cannot detect my .xml file, responsibly placed in build directory (build-test-Desktop-...). After a little research, i've added file to .pro file via

          RESOURCE += config.xml
          

          You should probably use QRC files instead to hold your resources.

          This is a very basic console application, written without classes and header-files, so i felt no need to construct QCoreApplication function here.

          This is very wrong! QFile inherits from QObject and thus requires QCoreApplication instance to be present. If you don't have it, most Qt classes can misbehave.

          The thing is, it cannot detect my .xml file

          Are you sure you are looking in the right directory? Is working dir inside of your build dir? The usual practice is to use QCoreApplication here:

          QString path = QCoreApplication::instance()->applicationDirPath() + "/config.xml";
          

          (Z(:^

          BeaverShallBurn 1 Reply Last reply Reply Quote 3
          • BeaverShallBurn
            BeaverShallBurn @sierdzio last edited by

            @sierdzio thank you for your feedback!

            I've tried adding QCoreApplication the same way it is done in an empty Qt app:

            QCoreApplication a(argc, argv);
            //existing code
            return a.exec();
            

            Compilator protested about the lack of constructor, and i agree about it. But do i really have to create additional files and classes? Is there a small chance that some kind of a workaround exists?

            -Knock knock
            -Who's there?
            -Compi
            -Compi who?
            -Compilation error

            sierdzio 1 Reply Last reply Reply Quote 0
            • Christian Ehrlicher
              Christian Ehrlicher Lifetime Qt Champion last edited by

              Check with QFile::exists() if the file is really there. Also check if the current work dir is correct (which I doubt).
              And please do not create the QFile object on the heap - not needed and you leak it.

              Qt has to stay free or it will die.

              1 Reply Last reply Reply Quote 0
              • sierdzio
                sierdzio Moderators @BeaverShallBurn last edited by

                @BeaverShallBurn said in Can't see .xml file from build directory:

                Compilator protested about the lack of constructor, and i agree about it.

                Please post the error message, I don't know what compiler is complaining about.

                (Z(:^

                BeaverShallBurn 1 Reply Last reply Reply Quote 0
                • BeaverShallBurn
                  BeaverShallBurn @sierdzio last edited by

                  @sierdzio, false alarm!

                  After reopening my project everything got built and no file existance errors were present. Your hint about adding QCoreApplication really helped!

                  -Knock knock
                  -Who's there?
                  -Compi
                  -Compi who?
                  -Compilation error

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post