Search code examples
c++qtresourcesqpixmap

How to use resource properly


When i use resorce it crash my program have no idea how to fix it. Name of resource Resource.grc, name of prefix /imag, name of picture srce_sedam_one.jpg. Everything else work fine.

QPixmap pixmap1 = QPixmap (":/imag/srce_sedam_one.jpg");
QMap<int, QPixmap> map;
map.insert(4, pixmap1);
ui->label_no->setPixmap(map.value(4));

Solution

  • Add in your pro-file and rebuild all.

    RESOURCES += \
        icons.qrc
    

    icons.qrc is in my case, you should use your filename. Resource.qrc

    Then call after ui->setupUi(this);

    ui->setupUi(this); 
    
    QPixmap pixmap1 = QPixmap (":/imag/srce_sedam_one.jpg");
    QMap<int, QPixmap> map;
    map.insert(4, pixmap1);
    ui->label_no->setPixmap(map.value(4));
    

    You app crashes because: ui->setupUi(this) create all your widgets which you put in Qt Designer. And when you call ui->label_no before setupUi, you use null pointer and get crash.