Cannot open file in resources directory - c++

I've tested every natural iteration that I can imagine right now but still can not successfully open a file with a QFile object. I've also tested placing the file in the ./resources directory and in the directory with the source file.
I've cleaned my project in between each test and restarted Qt Creator as well. I've also deleted my *.pro.user project configuration file and tested with new versions of that.
#include <QFile>
#include <QDebug>
...
//QFile input_file("./resources/testfile.txt");
//QFile input_file("qrc:/10_graph.txt");
//QFile input_file("testfile.txt");
QFile input_file(":/testfile.txt");
if (!input_file.exists()) {
qDebug() << "File does NOT exist";
exit(11);
}
Qt Creator >> About
Qt Creator 3.1.1 (opensource)
Based on Qt 5.2.1 (Clang 5.0 (Apple), 64 bit)

You should add a resource file to your project and add the testfile.txt to the resources. After that you can access the file from resources by :
QFile input_file(":/testfile.txt");
If you add a prefix to the resources it can be accessed by :
QFile file(":/somePrefix/new.txt");
If you put the file in a folder alongside the source directory and add it to the resources, you can access it by :
QFile file(":/folderName/new.txt");

Related

How to open a text file in QT creator using either QFile or fstream

QFile file("D:/QT/employee manager ui/EmployeeManager/‪employeeLogIn.txt");
if(!file.open(QIODevice::ReadOnly))
{
QMessageBox::information(0, "info", file.errorString());
}
I tried to use fstream, and ifstream to open the the file in QT creator, but I could not open the file. I used both back slash and double forward slash while specifying the file location. Last night, my code ran, but doesn't open the file.
This project has qmake build system, and QT widget application
thank you!

QT5: Unable to set stylesheet properties using qss file

I'm currently working on an app and I'm unable to set global stylesheet. Here's the code I've been trying:
QFile file(":/dark.qss");
file.open(QFile::ReadOnly | QFile::Text);
QTextStream stream(&file);
qApp->setStyleSheet(stream.readAll()); '''
and
QFile f(":/dark.qss");
if (!f.exists()) {
printf("Unable to set stylesheet, file not found\n");
}
else {
f.open(QFile::ReadOnly | QFile::Text);
QTextStream ts(&f);
qApp->setStyleSheet(ts.readAll());
}
For the first one I'm getting :
QIODevice::read \dark.qss device not open
and for the second one
Unable to set stylesheet, file not found
The second one used to work fine 2 weeks ago (build with msys2, mingw64, qt5.14.1.3), unfortunately, I had to reinstall mysys2 and since then this is not working anymore.
I tried to build using msys2 with all the packages I used 2 weeks ago (had to downgrade about 80 packages manually ) also straight from qt creator with no luck.
Any idea what might be the problem?
The issue has nothing to do with msys. The most obvious reason is an incorrect project setup. Please make sure dark.qss is added into the resource (qrc) file under "/" prefix and the qrc file is added into RESOURCES in the pro file.
Also, there is a bug in qmake not adding newly added resources into the app sometimes. A full rebuild might help in this situation.
It would be great if you paste both the qrc and pro file here. A full build log could be very helpful as well.
Maybe your .pro file is missing RESOURCES variable with .qrc file name.
You can also try to execute Clean and Run qmake on your project before building.

QFileDialog: How to open/select file/dir from package in osX

I have googled a lot but dint find any relative solution for my problem.
PROBLEM: I want to open .MTS file and its working find if its available in any directory. But if its in any package then my QFileDialog is not able to look into that package and select those .MTS files.
CODE:
auto filePaths = QFileDialog::getOpenFileNames(this, "Open Video File", lastOpenedPath, "*.MTS;*.mov");
qDebug() << "File Paths " << filePaths;
Now the .MTS files created under AVCHD(Advanced Video Coding High Definition) package default in Sony & Panasonic HD Camera, and I want to import/select that .MTS files.
HINT: QFileDialog is able to import/select those .MTS files in Windows machine, but fail to import/select in mac machine.
Highly appreciated any thoughts.
Thanks.
Well, if I understand what you want to do properly, I'm not sure that it's possible in Qt alone.
It did turn out to be easier than I expected to simply call in to Cocoa and NSOpenPanel to achieve what I think you're looking for.
Sample project is at: https://github.com/NSGod/widgetsOpenFileDialogCocoa
Basically, I renamed mainwindow.cpp to mainwindow.mm, then added an #import <Cocoa/Cocoa.h>:
#import <Cocoa/Cocoa.h>
void MainWindow::on_openFileButton_clicked()
{
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setAllowedFileTypes:[NSArray arrayWithObjects:#"mts", #"mov", nil]];
[openPanel setAllowsMultipleSelection:YES];
[openPanel setTreatsFilePackagesAsDirectories:YES];
[openPanel setTitle:#"Open Video File"];
NSInteger result = [openPanel runModal];
QStringList stringList;
if (result == NSFileHandlingPanelOKButton) {
NSArray *URLs = [openPanel URLs];
NSLog(#"URLs == %#", URLs);
for (NSURL *URL in URLs) {
stringList += QString::fromNSString(URL.path);
}
// do something with stringList
qDebug() << "filePaths == " << stringList;
}
}
Included in the project is a fakeBundle.component directory, which will be treated as a bundle (or "package") by OS X. But by setting treatsFilePackagesAsDirectories to YES, you can have the NSOpenPanel treat it as a directory (which it really is, of course).
Here is an image showing how the Finder treats this fakeBundle.component directory as if it were a single file:
And here in the NSOpenPanel, it's being treated as a directory:
An OSX package is a:
File system directory that is normally displayed to the user by the Finder as if it were a single file. Such a directory may be the top-level of a directory tree of objects stored as files, or it may be other archives of files or objects for various purposes, such as installer packages, or backup archives.
This is akin to an .mst or .msi file on Windows. Just as with OSX packages you would not be able to open your specified file within one of these packages. Your system open dialog is in fact doing you a disservice by allowing you to see into them, as you cannot open said files.
Your work around is to copy the file out of the package externally to the program then open the copy of the file.

QT 5.4, Unable to access Resource from code

I try to include style images of my app into a q-resource file.
When I include the file directly in the code, it work, but when i try to use QResource, it fail (do not load the file).
I have the resource file in the main directory:
AppFolder
|- main.cpp
|- darkstyle.qrc
|- darkstyle
|- WindowTitleBar.png
The following example print:
failed1
failed2
#include <QApplication>
#include <QResource>
#include <Qfile>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
bool ok= QResource::registerResource("darkstyle.qrc");
if (!ok) qDebug()<<"failed1";
QFile file(":/darkstyle/WindowTitleBar.png");
//QFile file("../AppFolder/darkstyle/WindowTitleBar.png"); //that work
if(!file.open(QFile::ReadOnly | QFile::Text)) qDebug()<<"failed2";
else file.close();
//return a.exec();
return 0;
}
Note: Qt creator by default create binaries (.exe) in a top folder: ../build-AppFolder_Qt_5_4_1_MSVC2013_64bit-Debug/debug/AppFolder.exe
The execution root path seem to be: ../build-AppFolder_Qt_5_4_1_MSVC2013_64bit-Debug
I tried most of possible combinations with execution paths.
Note2: Some examples use a .rcc file format, I have none of these, but that could be a clue.
Summary:
How to access a QResource file from inside a QT app?
EDIT 1: Content of qrc file:
<RCC>
<qresource prefix="/">
<file>darkstyle/WindowTitleBar.png</file>
<file>darkstyle/WindowTitleButton.png</file>
<file>darkstyle/WindowTitleButton1.png</file>
<file>darkstyle/WindowTitleButton2.png</file>
<file>darkstyle/WindowTitleButton3.png</file>
</qresource>
</RCC>
QResource::registerResource("darkstyle.qrc") registers the resource description. If you want to use resources dynamically like this you need to register the compiled resources themselves. Run rcc -binary darkstyle.qrc -o darkstyle.rcc and use QResource::registerResource("darkstyle.rcc")
Alternatively, compile the resources into your binary directly. Do do so, use RESOURCES += darkstyle.qrc in your .qrc, and leave out the QResource::registerResource.
The problem is related to an incompatibility of the given version of QT with MSVS2013.
The problem is solved by downloading another version of QT or visual studio.

C++ GUI Text Not Displaying

Basically I made a simple program to search a text file for a certain word. However my text file does not display in the textEdit Object it just displays it blank. I properly placed the text file into the resources and everything. I would really appreciate some input.
#include <QtCore/QFile>
#include <QtCore/QTextStream>
#include "find.h"
#include "ui_find.h"
Find::Find(QWidget *parent) : QWidget(parent), ui(new Ui::Find)
{
ui->setupUi(this); //sets up user interface
getTextFile();
}
Find::~Find()
{
delete ui;
}
void Find::on_goButton_clicked()
{
QString word = ui->lineEdit->text(); //gets text and stores in word
ui->textEdit->find(word, QTextDocument::FindWholeWords);
}
void Find::getTextFile()
{
QFile myFile(":/textfile.txt"); //what file
myFile.open(QIODevice::ReadOnly); //opens file
QTextStream textStream(&myFile); //convert to stream
QString line = textStream.readAll(); //store into variable
myFile.close(); //close file
ui->textEdit->setPlainText(line); //display text
QTextCursor textCursor = ui->textEdit->textCursor(); //moves cursor into position
textCursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1); //moves cursor into position
}
The application output also has this in red letters QIODevice::read: device not open
Qt Creator's clean rebuild are broken. Just permanently delete the kit specific folder where qmake has kept the project specific makefiles.
Attempt to do build again. rcc will be invoked specific to .qrc files added, that were skipped by older makefile files, as qmake has to give new makefile files now.
I have Qt 5.4 and my code gave the same error, when it was working with MSVC2010 OpenGL as a compiler. I manually added MinGW 32bit to use it as compiler and it worked.
P.S. I have not installed MSVC2013 for my Qt 5.4., and it works sometimes with MSVC2010 OpenGL without error, but not in this case.
Make sure that you added qrc file in project. If you use qrc file as external source, you should register it by call QResource::registerResource("path to file") in main function.
I just solved this with same error/symptoms. The issue was that I did not have a working compiler for my build kit. I ended up having to manually select my Microsoft Visual C++ Compiler 11.0 (x86). I guess it wasn't able to auto-detect it. This seems to be a common error message for new windows users beginning Qt5.