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. Can't see .xml file from build directory
QtWS25 Last Chance

Can't see .xml file from build directory

Scheduled Pinned Locked Moved Solved General and Desktop
qfilexmlconsole appxml parsing
6 Posts 3 Posters 966 Views
  • 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.
  • B Offline
    B Offline
    BeaverShallBurn
    wrote on 16 Sept 2021, 08:13 last edited by BeaverShallBurn
    #1

    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

    S 1 Reply Last reply 16 Sept 2021, 09:16
    0
    • B BeaverShallBurn
      16 Sept 2021, 08:13

      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.

      S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 16 Sept 2021, 09:16 last edited by
      #2

      @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(:^

      B 1 Reply Last reply 17 Sept 2021, 07:39
      3
      • S sierdzio
        16 Sept 2021, 09:16

        @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";
        
        B Offline
        B Offline
        BeaverShallBurn
        wrote on 17 Sept 2021, 07:39 last edited by
        #3

        @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

        S 1 Reply Last reply 17 Sept 2021, 07:55
        0
        • C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 17 Sept 2021, 07:43 last edited by
          #4

          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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • B BeaverShallBurn
            17 Sept 2021, 07:39

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

            S Offline
            S Offline
            sierdzio
            Moderators
            wrote on 17 Sept 2021, 07:55 last edited by
            #5

            @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(:^

            B 1 Reply Last reply 17 Sept 2021, 08:04
            0
            • S sierdzio
              17 Sept 2021, 07:55

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

              B Offline
              B Offline
              BeaverShallBurn
              wrote on 17 Sept 2021, 08:04 last edited by
              #6

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

              3/6

              17 Sept 2021, 07:39

              • Login

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