Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Where to put images (.png) to work wit Qt project
QtWS25 Last Chance

Where to put images (.png) to work wit Qt project

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 36.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mbourguel
    wrote on 13 Mar 2012, 13:50 last edited by
    #1

    Hello,
    From
    http://www.siteduzero.com/tutoriel-3-11316-les-principaux-widgets.html
    The methode :

    @image->setPixmap(QPixmap("icone.png"));
    layout->addWidget(image);@

    don't work. The second window is empty whitout "icone.png" image.
    icone.png is in the default place for the project
    C:\Users\Administrateur\Projets\qt\layout3
    and the executable place for the project:
    C:\Users\Administrateur\Projets\qt\layout3-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug\debug

    Where I have to put the "icone.png" file?
    Thanks.

    @
    /* Code du projet en C++ */
    #include <QApplication>
    #include <QtGui>

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    QWidget fenetre;
        QPushButton *bouton = new QPushButton("Ouvrir la fenĂȘtre", &fenetre);
    
    
    QDialog secondeFenetre (&fenetre);
        QVBoxLayout *layout = new QVBoxLayout;
        QLabel *image = new QLabel(&secondeFenetre);
        QPixmap pixmap;
    
    
        image->setPixmap(QPixmap("icone.png"));
        layout->addWidget(image);
        secondeFenetre.setLayout(layout);
    
    
    QWidget::connect(bouton, SIGNAL(clicked()), &secondeFenetre, SLOT(exec&#40;&#41;&#41;);
    fenetre.show();
    
    return app.exec&#40;&#41;;
    

    @

    [edit: @-code tags added, please use them for your code, Eddy]

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jake007
      wrote on 13 Mar 2012, 14:12 last edited by
      #2

      In your case, you need to put you png image in the same directory as .exe. If you are running your app from Qt Creator, exe is ran from \layout3-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug\ directoy, so you have to put your image there.
      Or you can also add it to the resource file.

      Regards,
      Jake


      Code is poetry

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mbourguel
        wrote on 13 Mar 2012, 17:13 last edited by
        #3

        Hello,
        Thanks Jake007. As you notify me I've put icone.png in:

        \Projets\qt\layout3-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug

        Now this code runs fine.
        Regards,
        Maurice

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tobias.hunger
          wrote on 14 Mar 2012, 07:51 last edited by
          #4

          Have you considered using the "resource system":http://qt-project.org/doc/qt-4.8/resources.html instead of having the file in the file system? That way it gets built into the binary and thus there can be no path issues.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mbourguel
            wrote on 14 Mar 2012, 13:06 last edited by
            #5

            Hello Tobias,
            I try the "resource system" method with a "application.qrc" file in Qt environment.
            That don't works. The main and image windows are empty.

            The icones directory is below the
            C:\Users\Administrateur\Projets\qt\image directory:

            C:\Users\Administrateur\Projets\qt\image\icones

            I've try also to put directly image.png file into the parent image directory. Same result..
            I don't undestand what's wrong.
            Thanks

            @
            /* --application.qrc file --*/
            <RCC>
            <qresource prefix="/icones">
            <file>image.png</file>
            </qresource>
            </RCC>

            /* -- main.cpp file */
            #include <QtGui/QApplication>
            #include "mainwindow.h"
            #include <QPixmap>
            #include <QLabel>

            int main(int argc, char *argv[])
            {
            QApplication a(argc, argv);
            MainWindow w;
            QWidget fenetre;

            // QLabel *label = new QLabel(&fenetre);

            // label->setPixmap(QPixmap("icone.png"));

            QLabel label;
            QPixmap p(":/icones/image.png");
            label.setPixmap(p);
            
            fenetre.show();
            w.show();
            
            return a.exec(&#41;;
            

            }
            @

            [EDIT: code formatting, please wrap in @-tags, Volker]

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mbourguel
              wrote on 14 Mar 2012, 16:04 last edited by
              #6

              Hello Tobias,
              Thanks for your help and sorry for my last message and
              to bother you for nothing .

              After several trials I have had success to visualize
              icone.png in the image window.
              I've use the Qt "resource system". The difficulty, for me, is
              to understand the correct syntax and path.
              Your QRC Editor sources permit me to correct my code.
              regards.
              Maurice

              The following code is working:

              @
              /* Qt source project for Qt ressource system*/
              #include <QtGui/QApplication>
              #include "mainwindow.h"
              #include <QPixmap>
              #include <QLabel>

              QApplication a(argc, argv);
              MainWindow w;
              QWidget fenetre;
              
              
                 QLabel *label = new QLabel(&fenetre);
                 label.setPixmap(p);
              label->setPixmap( QPixmap(":/icones/toto"));
              
              fenetre.show();
              w.show();
              
              return a.exec(&#41;;
              

              }
              @

              with the essai.qrc file:

              @
              <!DOCTYPE RCC>
              <RCC>
              <qresource prefix="/icones" lang="fr">
              <file alias="toto">ressources/essai.png</file>
              </qresource>
              </RCC>
              @

              [EDIT: code formatting, please wrap in @-tags, Volker]

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on 14 Mar 2012, 18:17 last edited by
                #7

                mbourguel, please wrap code in @-tags. this way you make sure that the code is formatted nicely. I've don it four you in your previous posts, but please keep in mind for further posts. Thank you.

                Is your problem finally solved or do you have an further questions on the resource system? Seems like you've already discovered how the aliasing facilities in the .qrc files work.

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0

                1/7

                13 Mar 2012, 13:50

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved