(C++) QtExample16

January 17, 2018 · View on GitHub

 

 

 

 

 

(C++) QtExample16

 

QtQt
CreatorLubuntuUbuntu

 

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:

 

  1. View screenshot
  2. In the Project Manager, right-click on the project name on on the 'Sources' folder. Click 'Add New'
  3. View screenshot
  4. In the 'New File' dialog, select to create a new 'Qt Resource file'
  5. View screenshot
  6. In the 'New Qt Resource file -> Location' dialog, give the resource file a name. For example, 'MyResources'
  7. View screenshot
  8. In the 'New Qt Resource file -> Project management' dialog, click 'Finish'
  9. View screenshot
  10. 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)
  11. View screenshot
  12. Change the prefix to a more fitting name, for example 'MyImages'.
  13. View screenshot
  14. Click 'Add -> Add Files' and add an image. In this example, this is a file called 'BeerSmall.bmp' (a sprite from BeerWanter)
  15. View screenshot
  16. Now, the file has been added to your resources. The image can be accessed with the filename ':/MyImages/BeerSmall.bmp'
  17. View screenshot
  18. Copy-paste the example code below
  19. View final screenshot

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

  • Qt Qt: version 5.4.1 (32 bit)
  • STL STL: GNU ISO C++ Library, version 4.9.2

 

 

 

 

 

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(); }