简体   繁体   中英

How to show an image in a QLabel

I'm trying to show an image from a resource file in a QLabel . All this is happenning from a plugin that extends the main core application.

#include "sampleplugin.h"
#include <QtWidgets>

SamplePlugin :: SamplePlugin()
{

    Q_INIT_RESOURCE(pluginresources);

    oLContainerWidget = new QWidget();

    olWrapper = new QHBoxLayout();
    bagIcon(":/sample_plugin_resources/images/Dry-50.png");

    oLContainerWidget -> setLayout(olWrapper);
}

void SamplePlugin :: testIcon(const QString &imageUrl)
{
    QPixmap pix(imageUrl);

    QLabel *sampleIconLabel = new QLabel();
    sampleIconLabel -> setPixmap(pix);

    olWrapper -> addWidget(sampleIconLabel);
}

The project compiles without any errors, but the image doesn't show. What am I doing wrong?

Thank you all in advance.

The project structure:

**plugins_dir**  
    sampleplugin  
        pluginresources.qrc  
        sample_plugin_resources  
            - Dry-50.png  

Check that pix has actually loaded the image ( pix.isNull() ). You are loading the image from a resource path, is that the complete path on your QRC file?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM