QImage loadFromData solid red



  • Hi,

    i want to open a image file and if it cannot be open a solid 1x1 pixel red image should used instead.
    Here is my code, but my loadFromData does not work. Can someone explain to me how it works??

    QImage img;
    if (!img.load(path))
        img.loadFromData(QByteArray(1, 255));
    

  • Moderators

    That's not correct. loadFromData does not create images from raw bytes. All it does is it takes a stream of data from memory instead of from file. It still needs to be in proper format e.g. jpeg, so have a header, data blocks etc.
    If all you want is a red pixel you can do it like this:

    QImage img;
    if (!img.load(path)) {
        img = QImage(1, 1, QImage::Format_ARGB32);
        img.fill(Qt::red);
    }
    

Log in to reply
 

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