Can't see .xml file from build directory
-
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.
-
@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";
-
@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?
-
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. -
@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.