Only when I start from QtCreator program works fine / read write to xml



  • Hi everbody,

    so I finished my program and when it starts within creator and the run function it
    works fine. But when I start it with a double click from the filesystem it don't read and write in the xml.

    0_1516297069050_Bildschirmfoto 2018-01-18 um 6.37.26 PM.png

    Do you have any ideas.

    THX
    Allman



  • @Allman-2 you should provide more information regarding your environment, i.e. different folder when you run from IDE than when you double click your executable? In any case, when opening the XML file for reading, you should check for any errors, which may give you a hint of why is not working.



  • @Pablo-J.-Rogina
    Hey Pablo,
    no it's anytime the same xml-file. and with qt got no error.


  • Qt Champions 2017

    Hi
    To run it outside Creator, it should have its needed SO files with it.
    try LDD on the ".exe" and see if any So files are missing.



  • @Allman-2 again, are you implementing some error checking when opening the file? QFile::errorString()

        ...
        QFile xmlFile("/path/to/XMLfile");
    
        if (!xmlFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
            qDebug() << xmlFile.errorString();
        } else {
            qDebug() << "It worked";
        }
        ...
    

    and given no file exists for instance, you'll have this handy error message:

    "No such file or directory"
    


  • Hey,
    so in my pic I attached, you can see that my time stamp of the main.xml is more newer than other filer, thats cause I wrote after creating to it.
    And Pablo yes I'm using that snippet code you pronounced from the qt-api.

    Well I' m thinking it is something with the read and write rights, I will check it out.
    THX



  • so to get the correct path ->
    QString current = QDir::currentPath();
    QStringList url = current.split(QFileInfo(QCoreApplication::applicationFilePath()).fileName());
    xmlFile = new QFile(url.at(0)+QString("main.xml"));

    you can find the entire code here:
    here: https://github.com/lustigepepe/TinWeb.git

    in the xmllibrary.cpp line 9 is the reading from the main.xml

    THX



  • @Allman-2 it looks like you solved your issue. If so, please don't forget to mark your post as such. Thank you.



  • @Pablo-J.-Rogina

    no, it still doesn't work the permission rights a correct and the code is still the same.



  • @Pablo-J.-Rogina

    here ::
    this code works fine within creator but not when I double-click it.
    QString current = QDir::currentPath();
    QStringList url = current.split(QFileInfo(QCoreApplication::applicationFilePath()).fileName());
    xmlFile = new QFile(url.at(0)+QString("main.xml"));

    do you have any idea why?


  • Lifetime Qt Champion

    Hi,

    Did you check the content of your QStringList before trying to build the file path ?

    In any case, expecting the file to be writable when it's located in the same folder as your application is not a good idea. On Windows for example, the Program Files folder is read only. The same goes for *nix system's /bin, /usr/bin and /usr/local/bin.

    You should rather have a default file embedded within your application that you copy over to best suited place given by QStandardPaths::writableLocation.

    By the way, you are using pointers way to much in your code and not in the best way. For example, there's no need to allocate QString on the heap. When you need to pass one around, use const references.



  • @SGaist said in Only when I start from QtCreator program works fine / read write to xml:

    Program Files
    mhhh
    yeahr everyone is scared about pointers but when this variable can be empty, I think
    pointer is good solution. Ok the heap is more slowly, your right.



  • @SGaist
    @Pablo-J-Rogina
    Well a Big big thanks to all, the solution is :: QCoreApplication::applicationDirPath()
    instead using QDir::currentPath() !!!
    AND SGaist thx for the tip with the write location, now it's changed!
    Big Cheers.!!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.