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. How to use QFile on Android
Qt 6.11 is out! See what's new in the release blog

How to use QFile on Android

Scheduled Pinned Locked Moved Mobile and Embedded
8 Posts 2 Posters 5.8k 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.
  • F Offline
    F Offline
    freelife
    wrote on last edited by
    #1

    I need read .txt file on android using qt program. If build for windows, that file should be in build folder, but what about android? How to include it in apk?
    One person told me use:
    @QFile file( "assets:/qml/foo/main.qml" );@
    but It does not work, maybe I did something not right.
    Can you write step by step instruction?
    (for example: get strings from 1.txt and paste them to QTextEditor on Android)
    Thank you

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Another way is to add your text file into your program's "resource":http://qt-project.org/doc/qt-5.0/qtcore/resources.html file. And then you can access that file as the other normal resources (e.g images etc..) But it will compiled into the binary.
      E.g Resource file
      @
      <RCC>
      <qresource prefix="/">
      <file>read.txt</file>
      </qresource>
      </RCC>@

      Access as follows:
      @void MainWindow::loadFile()
      {
      QFile file(":/read.txt");
      file.open(QIODevice::ReadOnly);

      QTextStream stream(&file); 
      QString line = stream.readAll();
      file.close(); 
      
      ui->textEdit->setText(line);
      

      }@

      157

      1 Reply Last reply
      0
      • F Offline
        F Offline
        freelife
        wrote on last edited by
        #3

        Thank you.
        I use your code, running on widnows was good, but "installation on Android" crashed,
        Problems:
        @: -1: Error: Could not find the information ELF@
        @: -1: Error: Failed to install package.@
        I inatall on AVD, becouse qt does not see my phone(
        last android project runed fine, I think it is becouse resource have some problems with andoid.

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          Can you give more details ? Which Qt version you are using ? I think this problem is not related to Resource files.

          157

          1 Reply Last reply
          0
          • F Offline
            F Offline
            freelife
            wrote on last edited by
            #5

            @p3c0
            qt5.1.1
            I think that, becouse this problem did not, when I did not use Resource files.

            mainwindow.h
            @#ifndef MAINWINDOW_H
            #define MAINWINDOW_H

            #include <QMainWindow>

            namespace Ui {
            class MainWindow;
            }

            class MainWindow : public QMainWindow
            {
            Q_OBJECT

            public:
            explicit MainWindow(QWidget *parent = 0);
            ~MainWindow();
            public slots:
            void loadFile();

            private:
            Ui::MainWindow *ui;
            };

            #endif // MAINWINDOW_H@

            mainwindow.cpp
            @#include "mainwindow.h"
            #include "ui_mainwindow.h"
            #include <QFile>
            #include <QTextStream>

            MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
            ui(new Ui::MainWindow)
            {
            ui->setupUi(this);
            connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(loadFile()));
            }

            MainWindow::~MainWindow()
            {
            delete ui;
            }

            void MainWindow::loadFile()
            {
            QFile file(":/read.txt");
            file.open(QIODevice::ReadOnly);

            QTextStream stream(&file);
            QString line = stream.readAll();
            file.close();
            
            ui->textEdit->setText(line);
            

            }@

            app.qrc
            @<RCC>
            <qresource prefix="/">
            <file>read.txt</file>
            </qresource>
            </RCC>
            @

            1 Reply Last reply
            0
            • F Offline
              F Offline
              freelife
              wrote on last edited by
              #6

              Building log problem:
              @Unable to find '. "
              Make sure that the application is successfully gathered and selected in the Application tab ("start parameters").
              @
              (translate)

              1 Reply Last reply
              0
              • p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #7

                Please check with your configuration of NDK and SDK's. I would suggest you to create a new application and then deploy to device. The code you provided works on my device. No compilations issues either.

                157

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  freelife
                  wrote on last edited by
                  #8

                  Great Thanks=)

                  1 Reply Last reply
                  0

                  • Login

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