Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to load a shared library resource



  • I am struggling to load a pixmap from a library, i have added

    RESOURCES += ../Base/cards.qrc

    in the .pro file.

    I have added

    Q_INIT_RESOURCE(cards); in the main file

    int main(int argc, char *argv[])
    {
    
        QApplication a(argc, argv);
    
        MainWindow w;
        w.show();
    
        Q_INIT_RESOURCE(cards);
    
        return a.exec();
    }
    

    If the path it hard coded the images show up correctly.
    what am i missing?

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        // this works
        //QPixmap p("C:/Users/Home/Documents/PokerGame/Resources/2C.png");
    
        // this does not work
        //QPixmap p("qrc:/../Resources/2C.png");
    
        // this does not work
        //QPixmap p(":/../Resources/2C.png");
    
        ui->label->setPixmap(p.scaled(80,100));
    }
    ![0_1555364015547_structure.png](https://ddgobkiprc33d.cloudfront.net/93cbee33-492a-49fb-a238-0ea61084bb1f.png)

  • Lifetime Qt Champion

    Hi,

    You might want to load it before trying to use its content.



  • @SGaist

    i tried this

    QPixmap p;
    p.load(":/../Resources/2C.png");
    

    weirdly i did
    p.load(":Resources/2C.png"); and this one works. why is that?

    shouldn't the url path from the resource folder be the correct path.
    the first one i copied the path from the resource folder.


  • Lifetime Qt Champion

    I don't know the structure of your project nor the content of your .qrc file so I can't answer.


Log in to reply