Using a resource file in QMediaPlayer - c++

How do I load a .mp3 file to use in a QMediaPlayer from a .qrc resource file?
This is what I have so far
QMediaPlayer *player = new QMediaPlayer;
player->setMedia(QUrl::fromLocalFile(":/audio/theme.mp3"));
player->play();
resources.qrc:
<RCC>
<qresource prefix="/audio">
<file>theme.mp3</file>
</qresource>
</RCC>
theme.mp3 is located in the project directory.

Use m_player->setMedia(QUrl("qrc:/audio/theme.mp3"));
If you are using Qt Creator, you can copy this url to clipboard by pressing right button on the audio file in the side bar and choosing Copy url "...".

Related

Why are Qt resources being loaded in Linux but not Windows?

I'm trying to load some PNG icons and some text files into my Qt program created in Qt Creator via a qrc file, but while the respective images appear on the toolbar (as they should) in the UI editor and in Linux, the buttons are blank when compiled in Windows. I even tried
QImage img(":/icons/debug.png");
qDebug() << img.isNull(); // true
I have my source files and the .pro file in basedir/src/, and the resources and .qrc file in basedir/src/res/
Is there something I'm doing wrong? If so, why does this work in Linux but not Windows?
EDIT:
qDebug() << QImageReader::supportedImageFormats(); // ("bmp", "cur", "dds", "gif", "icns", "ico", "jpeg", "jpg", "pbm", "pgm", "png", "ppm", "svg", "svgz", "tga", "tif", "tiff", "wbmp", "webp", "xbm", "xpm")
Here is my qrc file.
<RCC>
<qresource prefix="/icons">
<file>sphere-icon.png</file>
<file>project-properties.png</file>
<file>debug.png</file>
<file>qsi-icon.png</file>
</qresource>
<qresource prefix="/dictionaries">
<file>Enums.txt</file>
<file>LegacyFunctions.txt</file>
<file>MiniSphereObjects.txt</file>
</qresource>
</RCC>
Alright, I don't know why this worked, but after cleaning and rebuilding a hundred more times, deleting the .pro.user file, deleting my temporary folders (which I set in the project file to keep things more organized), removing the .qrc file reference from the project file, then restarting, suddenly everything works

icon for QT app

I'm using QTCreator to build an application on Mac. At this time the application icon is the default one set by Qt Creator.
I would like to set one custom specific to my app.
What I have in in the .qrc file, I have added
<RCC>
<qresource prefix="/">
<file>main.qml</file>
<file>images/logo.icns</file>
</qresource>
</RCC>
I have made a try on my main.cpp by adding
QIcon icon(":images/logo.icns");
QApplication app(argc, argv);
app.setWindowIcon(icon);
it doesn't work... I have tried to do it in another class in which I define how the window of the app is done
QIcon icon(":images/logo.icns");
QMainWindow *window = new QMainWindow();
window->setWindowTitle(QString::fromUtf8("PULS"));
window->resize(600, 600);
QWidget *centralWidget = new QWidget(window);
centralWidget->setWindowIcon(icon);
It's also not working.
The icon's path may be incorrect.
try to change
QIcon icon(":images/logo.icns");
to
QIcon icon(":/images/logo.icns");
cause of your .qrc file define the prefix:
<qresource prefix="/">
To set the icon for the executable file you need:
ICON = logo.icns
In your .pro file.
To set the window icon you need to call
QIcon icon(":/images/logo.icns");
centralWidget->setWindowIcon(icon);
Or try using a .png

QT5.2 Resource files

Short yet annoying problem; I can't access anything defined within qt resource file (aka .qrc). I've followed the qt utorial for creating a widget application called TextFinder.According to it I've created all the necessary files and completed all the instructions yet I can't access the qrc contents.
Inside the project folder I have files like:
TextFinder
resources
input.txt
main.cpp
textfinder.cpp
textfinder.h
TextFinder.pro
TextFinder.pro.user
TextFinder.qrc
textfinder.ui
The contents of the qrc file is as follows:
<RCC>
<qresource prefix="/res">
<file>resources/input.txt</file>
</qresource>
</RCC>
To access the file within I opened the qrc in Editor right clicked on the file and chose copy resource path to clipboard option. This produced ":/res/resources/input.txt". So I just entered this to my function to open the file. This function looks like the following:
void TextFinder::loadTextFile()
{
QFile inputFile(":/res/resources/input.txt");
inputFile.open(QIODevice::ReadOnly);
if (inputFile.isOpen())
{
QTextStream txtStream(&inputFile);
QString contents = txtStream.readAll();
inputFile.close();
ui->textEdit->setPlainText(contents);
QTextCursor cursor = ui->textEdit->textCursor();
cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
}
else
{
throw std::runtime_error("Resource file may be wrong?");
}
}
When I run the application the runtime_error is thrown that tells me that it couldn't open the file. In the project file I have the qrc file defined as follows:
RESOURCES += \
TextFinder.qrc
What is going wrong in here? Anyone could point out what i'm doing wrong?
Regards,
Joe
According to Qt Resource System documentation:
It is also possible to specify a path prefix for all files in the .qrc file using the qresource tag's prefix attribute:
<qresource prefix="/myresources">
<file alias="cut-img.png">images/cut.png</file>
</qresource>
In this case, the file is accessible as :/myresources/cut-img.png.
So subpath is cut off when prefix is present

Adding files to Qrc ressources

I have added a jpg file to the QRC ressources to not drag everytime the full path of the image file.
However, it's not displayed when I am starting the user interface where it's supposed to be loaded!!
I added to it an alias. It's not working. But when I write the name of the file included in the project solution, it's well displayed.
PS: I want to organize my project and put the file in QRC ressources
This is how did I do:
QPixmap img(":/projet/image.jpg"); // It's not loaded
QPixmap img("image.jpg"); // It's OK
Please check your .qrc file first.
You can check your file with another editor like notepad++, vim , joe etc.
Does it look similar to this example:
<RCC>
<qresource prefix="/project">
<file>FOLDER/image.jpg</file>
</qresource>
</RCC>
If it does you can access your image through:
QPixmap example(":/project/FOLDER/image.jpg");
Good luck!

Qt qrc resource file - can't load icon

I have a Qt5 desktop project and I added a "resource.qrc" file with the Qt Creator editor which created the following line into the project's .pro file:
RESOURCES = resource.qrc
I put a blank prefix and a png file (14x14) and I tried to use it like this:
QPixmap pixmap = QPixmap ("://my_image.png");
ui->combobox->addItem(QIcon(pixmap), "itemname");
The problem is: the icon won't show up!
The following works:
QPixmap pixmap(14,14);
pixmap.fill(QColor("red"));
ui->combobox->addItem(QIcon(pixmap), "itemname");
so the problem must be in the resource embedding process.. I noticed that the generated "exe" hasn't a resource section inside it... I don't have static linked external libraries, so I don't think I need the Q_INIT_RESOURCE(resource) macro (it gives me undefined external)
Update:
I'm posting here my qrc file:
<RCC>
<qresource prefix="/">
<file>my_image.png</file>
</qresource>
</RCC>
it's pretty simple and I don't understand why at runtime icons are not showing up
I had this same issue recently, where I malformed the resource string. If you are using a current version of Qt Creator, you can open your .qrc file for edit and then right-click the resource (in this case image) that you are trying to address, then click "Copy Resource Path to Clipboard". And voila, you have the correct resource string every time.
Qt Creator is awesome.
Hope this helps!
#Nikos C. gives you useful advice, but I think your main problem was that you didn't use the correct link to the resource.
In your code, you have:
QPixmap pixmap = QPixmap ("://my_image.png");
but, according to the documentation, it should be
QPixmap pixmap = QPixmap (":/my_image.png");
or you can give aliases to your resources, and use those instead.
Issue is solved is use rcc.exe
C:\root\QT>c:\root\QT\4.7.4\bin\rcc.exe Headless.qrc -o qtresources.cpp
During compilation you should have images in the path.
Create the qtresources.cpp file include this file in makefile or project. You should able to see the image.