繁体   English   中英

Qt Label :: setPixmap不起作用

[英]Qt Label::setPixmap not working

很简单,我试图在一个简单的Qt GUI应用程序中显示图像。 我有以下代码:

ui->label_2->setVisible(true);
QPixmap supremect(":/images/supremecourt.jpg");
ui->label_2->setPixmap(supremect);
if(supremect.isNull())
{
    QMessageBox err(this);
    err.setText("File null");
    err.exec();
}
building=SPCT; // A flag
ui->label_2->show();

它可以完美编译,但是当我运行它时,没有任何显示。 我确信该图像存在于资源中,那么我在做什么错呢?

编辑:转换为PNG没有帮助

尝试

QPixmap p;
QString str = ":/images/supremecourt.jpg";
bool retVal = p.load(str);
if(retVal == false)
{
    qDebug() << "Failed to load image!";
    foreach(const QByteArray &fmt, QImageReader::supportedImageFormats())
    {
        qDebug() << QString(fmt);
        // if this doesn't list jpeg then you don't have 
        // the plugin in your imageformats folder.
    }

    if( !QFile::exists(str) )
    {
        qDebug() << "File" << str << "doesn't exist!";
    }
}

http://qt-project.org/doc/qt-4.8/qpixmap.html#load

http://qt-project.org/doc/qt-4.8/qfile.html#exists

如果未加载,则您的程序可能无法访问正确的插件,或者该文件在您认为合适的位置不存在。

jpeg插件的dll称为qjpeg4.dll,应位于./imageformats/

希望能有所帮助。

  1. 从某些IDE(Qt Creator)运行它并检查日志。 几乎可以肯定在那里显示了一些错误消息。

  2. 确保已加载jpg插件。

  3. 确保其他内容不会改变标签的内容(例如setText将清除pixmap)

您是否将图像(:/images/supremecourt.jpg)正确添加到资源文件中 如果你不是,看这个 您可以发布资源文件的结构吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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