Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QImage load images return 0 occasionlly
Forum Updated to NodeBB v4.3 + New Features

QImage load images return 0 occasionlly

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 815 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    huhansan
    wrote on last edited by
    #1

    I load image with QImage,then scaled,create a QPixmap with that QImage,finally set it to a QLabel to show.

    At the begin, it works well. But sometimes it failed at loading the same image that was successfully loaded before. After that, whatever image loaded, it always return 0.

    Has anyone come to this question before?

    Here is my code :

    void show_image(const QString& imagePath) {

    QImage image;
    int ret = image.load(imagePath);
    LOG(DEBUG) << "Load image" << imagePath.toStdString() << ": " << ret;
    if (ret) {
        QPixmap pixmap = QPixmap::fromImage(image.scaled(170, 170, Qt::IgnoreAspectRatio));
        if (!pixmap.isNull()) {
            imageLabel->setPixmap(pixmap);
        }
    } 
    

    }

    Consider I looped call show_image with a collection of images.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Did you check if you run out of memory ?
      It could sound like that.

      H 1 Reply Last reply
      1
      • mrjjM mrjj

        Hi
        Did you check if you run out of memory ?
        It could sound like that.

        H Offline
        H Offline
        huhansan
        wrote on last edited by
        #3

        @mrjj Thank you for replying. I checked days ago, it's very likely a memory leak problem. But the code i posted seemed has no relationship with the leak, i will try to find the leak somewhere else in my code.

        mrjjM 1 Reply Last reply
        0
        • H huhansan

          @mrjj Thank you for replying. I checked days ago, it's very likely a memory leak problem. But the code i posted seemed has no relationship with the leak, i will try to find the leak somewhere else in my code.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @huhansan
          Hi
          Ok. I would go to look for widgets that you new and give no parent.
          Or places where you give the images as pointers.

          H 1 Reply Last reply
          0
          • mrjjM mrjj

            @huhansan
            Hi
            Ok. I would go to look for widgets that you new and give no parent.
            Or places where you give the images as pointers.

            H Offline
            H Offline
            huhansan
            wrote on last edited by
            #5

            @mrjj

            I has one widget that does not has one parent. It is usually invisible。 When I need to show an image, i set this widget visible, after serval seconds, set it back to invisible again. After hundreds of this kind of procedures, QImage.load() return 0.

            I watched the memory's usage , it's always growing. Has the step i set the widget visible/invisible has some influence on the memory usage?

            jsulmJ 1 Reply Last reply
            0
            • H huhansan

              @mrjj

              I has one widget that does not has one parent. It is usually invisible。 When I need to show an image, i set this widget visible, after serval seconds, set it back to invisible again. After hundreds of this kind of procedures, QImage.load() return 0.

              I watched the memory's usage , it's always growing. Has the step i set the widget visible/invisible has some influence on the memory usage?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @huhansan said in QImage load images return 0 occasionlly:

              Has the step i set the widget visible/invisible has some influence on the memory usage?

              Not really, but how you're loading and keeping the image does. You will need to show more code (how exactly do you load the image in that widget?).

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              H 1 Reply Last reply
              0
              • jsulmJ jsulm

                @huhansan said in QImage load images return 0 occasionlly:

                Has the step i set the widget visible/invisible has some influence on the memory usage?

                Not really, but how you're loading and keeping the image does. You will need to show more code (how exactly do you load the image in that widget?).

                H Offline
                H Offline
                huhansan
                wrote on last edited by
                #7

                @jsulm

                Thank you for replying.

                I customered one window inherted from QMainWindow which also has a QLabel to show image i setted.

                This window is usually invisible, when i load an image and call the function QLabel.setPixmap, i set it visible.

                Servals seconds set the window to invisible, and go on.

                The code used to load image is

                
                QImage image;
                int ret = image.load(imagePath);
                // After hundreds of loading actions, image.load always return 0
                LOG(DEBUG) << "Load image" << imagePath.toStdString() << ": " << ret;
                if (ret) {
                    QPixmap pixmap = QPixmap::fromImage(image.scaled(170, 170, Qt::IgnoreAspectRatio));
                    if (!pixmap.isNull()) {
                        // If pimmap is not now, set to QLabel to show 
                        imageLabel->setPixmap(pixmap);
                    }
                } 
                // Set the window visible
                setVisible(true);
                }
                
                
                

                I just googled setVisible(bool) function. It says the window object was created when set the window to Visible. So i was thinking , is it the memory was leaked every time i set the window to visible, then to invisible because of the window has no parents?

                jsulmJ 1 Reply Last reply
                0
                • H huhansan

                  @jsulm

                  Thank you for replying.

                  I customered one window inherted from QMainWindow which also has a QLabel to show image i setted.

                  This window is usually invisible, when i load an image and call the function QLabel.setPixmap, i set it visible.

                  Servals seconds set the window to invisible, and go on.

                  The code used to load image is

                  
                  QImage image;
                  int ret = image.load(imagePath);
                  // After hundreds of loading actions, image.load always return 0
                  LOG(DEBUG) << "Load image" << imagePath.toStdString() << ": " << ret;
                  if (ret) {
                      QPixmap pixmap = QPixmap::fromImage(image.scaled(170, 170, Qt::IgnoreAspectRatio));
                      if (!pixmap.isNull()) {
                          // If pimmap is not now, set to QLabel to show 
                          imageLabel->setPixmap(pixmap);
                      }
                  } 
                  // Set the window visible
                  setVisible(true);
                  }
                  
                  
                  

                  I just googled setVisible(bool) function. It says the window object was created when set the window to Visible. So i was thinking , is it the memory was leaked every time i set the window to visible, then to invisible because of the window has no parents?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @huhansan This looks good. You could use Valgrind (Linux/UNIX tool) to check for memory leaks.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  H 1 Reply Last reply
                  1
                  • jsulmJ jsulm

                    @huhansan This looks good. You could use Valgrind (Linux/UNIX tool) to check for memory leaks.

                    H Offline
                    H Offline
                    huhansan
                    wrote on last edited by
                    #9

                    @mrjj @jsulm
                    It's a memory leak issue.

                    I found a memory leak in someone other's code, that why I load images failed, it eaten all the memory.

                    Thank you again for you concern.

                    1 Reply Last reply
                    4

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved