SFML "failed to create the font face" issue - c++

I'm using SFML in Visual Studio 2015 to make a game that requires me to print text. I try to load fonts and keep getting an error that says "failed to create the font face". I've tried loading several different fonts and none of them work, and they are in the correct directory, which is the folder where my project is located.
This is literally all it is, and it doesn't work:
sf::Font font;
if (!font.loadFromFile("arial.ttf"))
return EXIT_FAILURE;
The ttf file for arial is in the same folder as the project itself, which is what seems to solve the problem for everyone else I find online who has the same issue. Any idea why the font still won't load?

The simple answer is, the file is not in your current working directory when you run your executable.
Try to put in a fully qualified filename. It will work.
Using Visual Studio, most likely this is the directory where your .vcxproj file resides.
If this does not work, you can find out what your current working directory is by checking this post for a generic way how to find out your current directory.
As a quick hack, you could just create a file when your program starts. Start once and check where the file gets created. This is the current directory.

I fixed with this:
sf::Font font;
if (!font.loadFromFile("../arial.ttf")){
return EXIT_FAILURE;
}
I'm using CLion on Ubuntu using SMFL also.

To add to this, I was experiencing this same error when I thought I'd had everything typed properly. What ultimately ended up being the issue was that I had written .tff for the font file rather than .ttf, which, once corrected, fixed the problem.

My TTF file had no permissions once extracted from its archive. Executing chmod 644 font.ttf fixed the problem.

Related

How can I load an image to QPixmap without specifying the full path?

I'm using Qt Creator on Windows and trying to add an image to an icon.
The only way I can get it to work is by specifying the full path:
QPixmap newIcon("C:/Users/slipn/Documents/qt/projetos/exemplo/new.png");
newToolBarAction = toolbar->addAction(QIcon(newIcon), "New File");
I tried adding to the project as shown below:
The image and the code files are in the same directory. I have no clue of what path I should use to make it work. Any hints?
The problem you need to solve, is that your source path and the path where you build or maybe later even install your application are not the same. You need to bundle your image resource with your application, either by copying & installing it to your build/install, or by including it directly in your application binary.
The latter is your easiest bet with qmake. Installing files with qmake is (relatively) easy, but making your build result work without a separate make install step is a different thing.
Your easiest bet to solve both "running your build result directly" and "running the installed application" is to use the Qt Resource System.
Create a .qrc file with File > New File > Files and Classes > Qt > Qt Resource File, add a "prefix" (some name), add your image file there, and then use QPixmap newIcon(":/yourprefix/new.png") to access it in your application.

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.

Ofstream creates hidden(?) files (while .open) on Ubuntu

I have the following problem. I would like to open and save a string to file.
ofstream file("file");
file << "example" << endl;
file.close()
Rather simple stuff. The problem is... the code above doesn't seem to create any files in my project directory. I have tried to use "locate" in terminal to find my file - withoud any results. What is interesting - I can open the file using ifstream and read from it, without any particular problems.
What the heck is going on here? Where should I look for the file created?
P.S. On Windows everything works 100% fine.
in my project directory
Where did you read that this is where to look? We should collaborate and correct that resource.
I have tried to use locate in terminal
It probably hasn't been indexed yet.
You need to look in the current working directory of the terminal that started the program.

Trouble getting a Qt Reference Document program to work(Minehunt)... its just blank when run... no errors though?

http://docs.huihoo.com/qt/4.7/demos-declarative-minehunt.html
When I build and run... I just get a blank white rectangle... game is MineHunt.
The source code is in the URL. I created files and copied and pasted into Qt Creator 4.7.
Running in Linux.
No need to downvote. Deploying qml apps does not seem to be easy for beginners. jdl, I don't know if I can help you. Currently I am only on Linux. But what I did:
I copied the whole folder minehunt in my destination folder (/tmp/mine). When I ran qmake I got:
WARNING: Include file
/tmp/mine/helper/qmlapplicationviewer/qmlapplicationviewer.pri not
found
So I also copied the examples/declarative/helper folder into /tmp/mine.
Looked like this:
/tmp
/mine
/minehunt
/helper
Then I changed into minhunt, did 'qmake' and 'make'. A few seconds later I had a working minehunt binary in /tmp/mine/minehunt/.
I'll try tomorrow the Windows build.
Edit: Ok, tried now under Windows 7. Qt4.
QtCreator opened.
Project ...qt\demos\declarative\minehunt\minehunt.pro
loaded. Normally in release mode compiled. No problem. A file minehunt.exe was created in ....qt\demos\declarative\minehunt\release. Started: White screen. Exactly as you described it. In ...qt\demos\declarative\minehunt I found a folder 'qml'. I moved this folder and the minehunt.exe into another folder (myMineFolder). Simulating a simple deployment this way. Clicked on minehunt.exe -> worked like a charm.
So my folder layout:
myMineFolder
qml
minehunt.exe
But of course, I have set my PATH variable correctly to my Qt installation.

Can't open text file resource in Xcode 4 c++ tool?

I am working on figuring out how to use Xcode 4 to debug c++ projects.
I have basically copy pasted a working c++ executable that when compiled from the terminal ran fine.
However, i was thinking it might be nice to use Xcode for debugging. So I am trying to migrate the single .cpp file into Xcode as a command line tool.
I need to read in a file called numbers.txt (which I supply through a command line argument) which is located in my project directory, and then out put to a file (whose name I also specify as an argument.)
The problem I am running into is that the files that are supplied as command line arguments are failing to open.
ifstream in;
ofstream out;
in.open(argv[1]);
out.open(argv[2]);
I have checked to make sure that the arguments are being properly passed and are named correctly. The ifstream in is being supplied with `numbers.txt', which I want to open a text file that I already have.
However when I check to make sure the ifstream is open:
if(in.is_open() == false){
cerr << "Unable to open input file" << endl;
return 1;
}
I get the error.
I suspect this has something to do with how Xcode organizes the project.
my numbers.txt file is just sitting in the Xcode project folder, and I have only one .cpp class and one product, the executable.
anyone know what I am missing here?
The executable built by Xcode is in a different folder than the project. Passing in the name of the file without an absolute path before it will cause the executable to look for it in the wrong place, which is why it can't be found. Some of the possible solutions are to include the file as part of the build process (so it ends up in the same directory as the executable) or to pass the file to be opened by its absolute path. There are other ways to solve the problem, too, but hopefully that should be enough to get you started.
Old thread, but i have faced the same problem now, and it is easy to solve. Just copy the file in the build phase.
Here is an screenshot of the final result (note the destination, subpath and checkbox):