Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [solved]Unable to open resource file

    General and Desktop
    3
    4
    1290
    Loading More Posts
    • 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.
    • B
      BenBenJones last edited by

      Hi,

      I added a resource file "myresourcestuff.qrc" to my project "StringFinder"

      StringFinder.pro:
      @
      QT += core gui

      greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

      TARGET = StringFinder
      TEMPLATE = app
      INCLUDEPATH += .

      SOURCES += main.cpp
      stringfinder.cpp

      HEADERS += stringfinder.h

      FORMS += stringfinder.ui

      RESOURCES += myresourcestuff.qrc
      @

      and the "myresourcestuff.qrc" looks like this:
      @
      <RCC>
      <qresource prefix="/new/prefix1">
      <file>stuff.txt</file>
      </qresource>
      </RCC>
      @

      "stuff.txt" is located on the same path as the .ccp, .h, .qrc - files.

      Now I'm trying to access that file in "stringfinder.cpp"

      @
      // works
      //QFile myFile("C:/research/Qt/Qt5.2.0/Tools/QtCreator/bin/StringFinder/stuff.txt");
      // doesn't work
      QFile myFile(":/new/prefix1/stuff.txt");

      if(myFile.exists(&#41;){
          ui->textEdit->setText("opening file");
          myFile.open(QIODevice::ReadOnly);
          QTextStream textStream(&myFile);
          QString line = textStream.readAll();
          myFile.close();
      
          ui->textEdit->setPlainText(line);
      
          QTextCursor textCursor = ui->textEdit->textCursor();
          textCursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
      }
      else
      {
          ui->textEdit->setText("File not found");
      }
      

      @

      but it won't open, myFile.exists() returns "false" :/

      Can anyone help please? A minimal project is uploaded here: http://depot.tu-dortmund.de/get/ck3bx

      Best,
      Ben

      1 Reply Last reply Reply Quote 0
      • Q
        qxoz last edited by

        Hi BenBenJones. Welcome to devnet.
        The minimal project works ok. It is very strange that you get errors. Try "clean", "qmake", "rebuild all".

        1 Reply Last reply Reply Quote 0
        • dheerendra
          dheerendra Qt Champions 2022 last edited by

          This should work without any problem. Just check the file extn. Some times it will somestuff.txt.tx. It may not be visible in the windows explorer. This is the only issue I suspect now.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply Reply Quote 0
          • B
            BenBenJones last edited by

            Thanks, “clean”, “qmake”, “rebuild all” was the solution!!

            1 Reply Last reply Reply Quote 0
            • First post
              Last post