[solved]Unable to open resource file



  • 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



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


  • Qt Champions 2017

    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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.