Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Text file in android
Forum Updated to NodeBB v4.3 + New Features

Text file in android

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
8 Posts 2 Posters 784 Views 1 Watching
  • 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.
  • S Offline
    S Offline
    Sason
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sason
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Take a look at QStandardPaths.

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • S Offline
            S Offline
            Sason
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Sason
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1

                  • Login

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