⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

If statement to identify an image exists in a pixmap

  • I want to identify if a QLabel->pixmap is empty or not.

    This is because I want a button to open up an imageVewer (based on Qt example) to display a fullscreen view of the image in the pixmap but only if an image exists in the pixmap, otherwise don’t do anything.

    I have tried all sorts of things but cannot get this concept to work

    @ if(!ui->image_PreProcessed->pixmap()->isNull())
    viewImage = new ImageViewer(this);

    This is the only thing so far I have been able to compile but if there is no image in the pixmap it just crashes the application. If an image exists it works. I have declared viewImage in the header and all that works, so it’s just how to write the conditional part that I’m interested in.

    Any thoughts on where I’m going wrong?

  • Lifetime Qt Champion


    You should first check that ui->image_PreProcessed->pixmap() is not null

  • Isn't that what I'm doing with !ui->... ?

    Do I also require an else statement to stop it crashing when it is null?

  • Lifetime Qt Champion

    No, your line checks whether the content of the QPixmap is null, not whether the QPixmap itself if null

    Depends on when you need imageView. Beware of one thing, you might be creating a memory leak with your current code.

  • Thanks SGaist

    I will have to dig deeper then.

    Only need the imageView for zooming in to check focus as part of an initial config. Otherwise the application is being designed to run headless.

    I thought the QPixmap itself would never be null because it has height and width but I know very little about this stuff.

  • Lifetime Qt Champion

    The width/height doesn't depend on the pixmap set, it's the QLabel size.

Log in to reply