Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't read from file.
Forum Updated to NodeBB v4.3 + New Features

Can't read from file.

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.1k 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.
  • quentinthorntonQ Offline
    quentinthorntonQ Offline
    quentinthornton
    wrote on last edited by
    #1

    This is a really stupid question, but for the life of me, I can't figure out how to read one line of text from a .txt file. Everytime, it says in the console "QIODevice::read: device not open". This is my code.

    void Widget::printLine(){
        QFile myFile(":/conversations.txt");
    
        myFile.open(QIODevice::ReadOnly | QIODevice::Text);
        QTextStream conversations(&myFile);
        QString line = conversations.readLine();
        ui->textEdit->setText(line);
    
        myFile.close();
    }
    

    I've tried putting in the whole address (C:/users...) but that doesn't work. I've also tried adding a resource file and adding a prefix, /, then adding the text file to it. And I've also tried putting it in the same directory as the project (where all the .cpp's and stuff are). I'm almost certain that the problem is because it can't open the file, because I've done different tests to make sure of that. Any ideas what is wrong? Thanks in advance.

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

      hi

      If you use the ":/XX" syntax then the file must be in a qresource file
      included in the project.
      http://www.bogotobogo.com/Qt/Qt5_Resource_Files.php

      Have you check what open returns ?
      if(! myFile.open(QFile::ReadOnly | QFile::Text))
      {
      qDebug() << " Could not open the file for reading";
      return;
      }

      a full path to the file "c:/user/xx/conversations.txt" should have worked.

      If you try the qresource way again, then make sure to
      clean all-qmake-build all to make sure the res is compiled into
      exe.

      Here is small test project that does it (using your code)
      https://www.dropbox.com/s/q9nucvmxgqye7f4/readmyfile.zip?dl=0

      1 Reply Last reply
      2
      • quentinthorntonQ Offline
        quentinthorntonQ Offline
        quentinthornton
        wrote on last edited by
        #3

        Thanks you so much!! After trying your code and seeing that it was almost exactly the same as mine, I figured that it was probably something wrong with the actually project (I might have messed something up) so I made a new project and put all your and my code into it and it worked! I also tried my code and it worked too. So I must have messed something up that I wasn't supposed to. I wouldn't have ever figured that out if it wasn't for your code. Thanks a lot!

        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