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. QIODevice::read: device not open, help
Forum Updated to NodeBB v4.3 + New Features

QIODevice::read: device not open, help

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 35.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.
  • O Offline
    O Offline
    okiewardoyo
    wrote on last edited by
    #1

    I tried using below code to load "myfile.txt" and insert it to "QTextEdit" textarea,
    @
    void MyTextEditor::loadTextFile()
    {
    QFile inputFile("myfile.txt");

    inputFile.open(QIODevice::ReadOnly);
    
    QTextStream in(&inputFile);
    QString line = in.readAll();
    inputFile.close();
    
    ui->myTextArea->setPlainText(line);
    

    }
    @

    But unfortunately, when i call this function, it return "QIODevice::read: device not open " on Application Output Window.
    Any suggestion??

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #2

      Hi,

      You should first check if "myfile.txt" "exists ":https://qt-project.org/doc/qt-5/qfile.html#exists

      If "exists" returns false, you need to add your/path/to/your/file.

      See the link to the docs for more info.

      Qt Certified Specialist
      www.edalsolutions.be

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

        Hi,

        To add to Eddy "myfile.txt" is a relative path, so your program will expect it in the same folder as the executable.

        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
        • O Offline
          O Offline
          okiewardoyo
          wrote on last edited by
          #4

          @Eddy and @SGaist , hi guys, my "myfile.txt" is on the same folder as my cpp files, i think it will work without adding the path :)

          1 Reply Last reply
          0
          • JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            [quote author="okiewardoyo" date="1397992026"]@Eddy and @SGaist , hi guys, my "myfile.txt" is on the same folder as my cpp files, i think it will work without adding the path :) [/quote]Your .txt file needs to be in your "working directory":https://en.wikipedia.org/wiki/Working_directory. Usually, this is the folder that contains your .exe file, not your .cpp files.

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            0
            • O Offline
              O Offline
              okiewardoyo
              wrote on last edited by
              #6

              @JKSH: hi, you're right, i will try it,, :)

              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