Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

Text file in android



  • Hi, I am trying to make a program which will read lines from file on start and write them in QListWidget. Then if we click one button it will add to file and QListWidget line from QLineEdit. After clicking other button it should remove everything from QListWidget and file.
    The problem is that I can't do this. With help of Internet I've found 2 ways to do but they don't work for me (maybe I do something wrong?)

    So...

    First way:
    Add a file as resorce. I made file resources.qrc and add there my file
    <RCC>
    <qresource >
    <file>list</file>
    </qresource>
    </RCC>

    Then I was trying to use it by QFile in my cpp file:

    On start:

    QFile plik("/:lista");
    if (plik.exists())
    {
    if(plik.open(QIODevice::ReadOnly | QIODevice::Text))
    {
    while(!plik.atEnd())
    {
    ui->LTCP->addItem(plik.readLine());
    }
    }
    plik.close();
    }

    Adding to list and file:

    ui->LTCP->addItem(ui->dodadr->text() + " " + ui->dodname->text());
    if(plik.open(QIODevice::WriteOnly | QIODevice::Text))
    {
    QTextStream out(&plik);
    out << ui->dodadr->text() << " " << ui->dodname->text() << "\n";
    plik.close();
    }

    Removing:

    if(plik.open(QIODevice::ReadWrite | QIODevice::Text))
    {
    QString s;
    QTextStream t(&plik);
    while(!t.atEnd())
    {
    QString l = t.readLine();
    if(l!=ui->LTCP->currentRow())
    {
    s.append(l + "\n");
    }
    }
    plik.resize(0);
    t << s;
    plik.close();
    }

    LTCP is my QListWidget
    dodadr and dodname are QLineEdit

    In this way nothing happended... Program compiled and started but I can't write or read anything.
    When I tried write manualy a line to file and then compile, the line was in my QListWidget so it was readed from file. But adding or removing don't work :(

    Second way I found is simmilar.
    My code still look like this (only change is QFile plik("assets:/assets/list"), but I didn't add resources file (.qrc) and add to my .pro file :

    android
    {
    my_files.path = /assets
    my_files.files = $$PWD/assets/list
    INSTALLS += my_files
    }

    Unfortunatelly by this way I have an error: [install_my_files] Error 3 (ignored) and it doesn't work.

    I would be grateful for any help.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    First thing: files in resources are built in the executable, you can't write to them.

    Then ensure that you don't have typos in your code. Currently the file name in your resource file doesn't match the one you use in your code that tries to read the file.

    One alternative you have is to copy the file in an accessible read/write folder and work with that file.



  • Sorry, the name of file is my mistake now (when I wrote the post), in code it's OK.

    How can I copy that file? I quiet don't understand. I could copy it from resources to phone memory?


  • Lifetime Qt Champion

    Take a look at QStandardPaths.

    Get a writable location, check if it exists, if not create it and then copy the file over there.



  • Ok, sorry but i am not sure if i well understand. I should find writable location and than copy my file from resources to this location. Then I will be able to edit it and save. All this I shold do on the start of my program?

    And what if I do that and then close app and start it again? It will copy the first version of file again from resources and i lost changes or I missunderstand something?


  • Lifetime Qt Champion

    Well... Check that the file exists and do not overwrite it if it's the case.



  • Ok, it's good, but I have problem with QStandardpath... I can't find any writable location on android :/ I tried
    QStandardLocation::WritableLocation(QStandardLocation::DocumentsLocation) and almost all locations (where is Documents) but when I tried to check if(plik.isWritable()) it always say that location isn't writable :( So when I tried to copy file it can't be done.


  • Lifetime Qt Champion

    As I already wrote in a previous answer, check whether the folder exists and if not create it. The value returned returned by QStandardPaths are correct, but it doesn't mean that they already exist. It would make no sense to "pre-create" all possible folders if they are never used.


Log in to reply