(C++) QtExample16
January 17, 2018 · View on GitHub
(C++) QtExample16




This Qt example shows how to using resources, like this screenshot (png).
Using resources in Qt Creator 2.0.0
Here follows a step-by-step guide to add a bitmap as a resource to a Qt Creator project:
- View screenshot
- In the Project Manager, right-click on the project name on on the 'Sources' folder. Click 'Add New'
- View screenshot
- In the 'New File' dialog, select to create a new 'Qt Resource file'
- View screenshot
- In the 'New Qt Resource file -> Location' dialog, give the resource file a name. For example, 'MyResources'
- View screenshot
- In the 'New Qt Resource file -> Project management' dialog, click 'Finish'
- View screenshot
- As you can see, 'MyResources.qrc' has now been added to the Project Manager. Double-click on 'MyResources.qrt' and click 'Add -> Add Prefix' (which is all there is to choose yet)
- View screenshot
- Change the prefix to a more fitting name, for example 'MyImages'.
- View screenshot
- Click 'Add -> Add Files' and add an image. In this example, this is a file called 'BeerSmall.bmp' (a sprite from BeerWanter)
- View screenshot
- Now, the file has been added to your resources. The image can be accessed with the filename ':/MyImages/BeerSmall.bmp'
- View screenshot
- Copy-paste the example code below
- View final screenshot
Technical facts
Operating system(s) or programming environment(s)
Lubuntu 15.04 (vivid)
Qt Creator 3.1.1
- G++ 4.9.2
Libraries used:
Qt project file: ./CppQtExample16/CppQtExample16.pro
exists(../../DesktopApplication.pri) { include(../../DesktopApplication.pri) } !exists(../../DesktopApplication.pri) { QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TEMPLATE = app CONFIG(debug, debug|release) { message(Debug mode) } CONFIG(release, debug|release) { message(Release mode) DEFINES += NDEBUG NTRACE_BILDERBIKKEL } QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra unix { QMAKE_CXXFLAGS += -Werror } } exists(../../Libraries/Boost.pri) { include(../../Libraries/Boost.pri) } !exists(../../Libraries/Boost.pri) { win32 { INCLUDEPATH += \ ../../../Projects/Libraries/boost_1_55_0 } } SOURCES += main.cpp RESOURCES += MyResources.qrc
./CppQtExample16/main.cpp
#include <cassert> #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <boost/shared_ptr.hpp> #include <QApplication> #include <QDialog> #include <QLabel> #include <QVBoxLayout> #pragma GCC diagnostic pop int main(int argc, char *argv[]) { QApplication a(argc, argv); //Load the resource in a pixmap const QPixmap pixmap(":/MyImages/BeerSmall.bmp"); assert(!pixmap.isNull() && "Assume bitmap is found"); //Put the pixmap in a label boost::shared_ptr<QLabel> label(new QLabel); label->setPixmap(pixmap); //Add label in a layout boost::shared_ptr<QVBoxLayout> layout(new QVBoxLayout); layout->addWidget( label.get() ); //Create and show a window boost::shared_ptr<QDialog> dialog(new QDialog); dialog->setLayout(layout.get()); dialog->show(); return a.exec(); }