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



  • 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]



  • 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



  • 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


  • Moderators

    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.



  • 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]



  • 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]



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.