PNG Image not showing in QLabel



  • Hi guys,

    So I have an issue with a QLabel and a png image.

    When using Qt Creators form, I can get my resource png image to show properly.

    However, when I try to do the same programatically, it doesn't show. This is what I have:

        QPointer<QLabel> label_qt_logo = new QLabel(mapWidget.value("widget_top"));
        label_qt_logo->setObjectName(QStringLiteral("label_qt_logo"));
        QSizePolicy policy1(QSizePolicy::Fixed, QSizePolicy::Fixed);
        policy1.setHorizontalStretch(0);
        policy1.setVerticalStretch(0);
        policy1.setHeightForWidth(label_qt_logo->sizePolicy().hasHeightForWidth());
        label_qt_logo->setSizePolicy(policy1);
        label_qt_logo->setMinimumSize(QSize(50, 50));
        label_qt_logo->setMaximumSize(QSize(100, 100));
        label_qt_logo->setPixmap(QPixmap(QString::fromUtf8(":/logos/qt_logo.png")));
        label_qt_logo->setScaledContents(true);
        mapLabel.insert(label_qt_logo->objectName(), label_qt_logo);
    

    This is identical to the one Creator produced bar the QPointer bit and the mapLabel bit.

    Now, creator is aware of the logo as it shows up in the resource editor but when I launch the app it doesn't actually show.

    What gives?

    Cheers guys!


  • Moderators

    Hi Jamie welcome to the Qt Forum,

    Did you add the label to the layout of your form?

    If you use Qt Designer to design your form, Qt will generate a file with the code how it's build up (named something like ui_dialog.h) . You can look in there to learn and detect the missing pieces. So you have the best from both worlds.



  • Yes I have, I even tested the label by placing a "H" in it and it showed up.


  • Moderators

    Do you use a resource file?
    maybe your program couldn't find your image.



  • Yes, it would seem this must be the case but Qt Creator knows it exists because it even has the icon to show the image (if that makes sense).


  • Moderators

    did you have a look in the ui_dialog.h file as suggested above?



  • Yes, I have copied the code pretty much exactly, the only thing I changed was adding the insertion of an object into my hash map


  • Moderators

    Hi Jamie,

    Could you put the relevant parts in a minimal compilable example?

    otherwise we only can guess...



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