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. Could someone tell me why this image loader will not work?
Forum Updated to NodeBB v4.3 + New Features

Could someone tell me why this image loader will not work?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 325 Views 2 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.
  • A Offline
    A Offline
    AI_Messiah
    wrote on last edited by
    #1

    I am trying to load images that are in a directory from a resource file.

    This is my mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        int fcount = 0;
        int fdist = 0;
        QString numstr;
        QString fname;
        QImageReader reader;
        for (int i=0;i<65536;i++){
            if (fileExists(i)) fcount++;
        }
        myimg = new QImage[fcount];
        relop = new int[fcount];
        for (int i=0;i<65536;i++){
            if (fileExists(i)){
                numstr = QString::number(i,16);
                while (numstr.length()<4){
                    numstr = "0" + numstr;
                }
                fname = ":/images/images/bloc" + numstr + ".png";
                reader.fileName() = fname;
                myimg[fdist] = reader.read();
                relop[fdist] = i;
                fdist++;
                this->ui->widget->images = myimg;
                this->ui->widget->relind = relop;
                this->ui->widget_2->images = myimg;
                this->ui->widget_2->relind = relop;
            }
        }
        this->ui->widget->loaded = true;
        this->ui->widget->scrmax = fcount;
        this->ui->widget->repaint();
        ui->verticalScrollBar->setMinimum(0);
        ui->verticalScrollBar->setMaximum(fcount - 7);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    bool MainWindow::fileExists(int inv)
    {
        QString numstr = QString::number(inv,16);
        while (numstr.length()<4){
            numstr = "0" + numstr;
        }
        QString fname = ":/images/images/bloc" + numstr + ".png";
        QFile fil(fname);
        if (fil.exists()) return true;
        return false;
    }
    
    
    

    and this is my mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QFile>
    #include <QImage>
    #include <QImageReader>
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
        QImage *myimg;
        int *relop;
    
        bool fileExists(int inv);
    };
    #endif // MAINWINDOW_H
    
    
    artwawA kshegunovK 2 Replies Last reply
    0
    • A AI_Messiah

      I am trying to load images that are in a directory from a resource file.

      This is my mainwindow.cpp

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          int fcount = 0;
          int fdist = 0;
          QString numstr;
          QString fname;
          QImageReader reader;
          for (int i=0;i<65536;i++){
              if (fileExists(i)) fcount++;
          }
          myimg = new QImage[fcount];
          relop = new int[fcount];
          for (int i=0;i<65536;i++){
              if (fileExists(i)){
                  numstr = QString::number(i,16);
                  while (numstr.length()<4){
                      numstr = "0" + numstr;
                  }
                  fname = ":/images/images/bloc" + numstr + ".png";
                  reader.fileName() = fname;
                  myimg[fdist] = reader.read();
                  relop[fdist] = i;
                  fdist++;
                  this->ui->widget->images = myimg;
                  this->ui->widget->relind = relop;
                  this->ui->widget_2->images = myimg;
                  this->ui->widget_2->relind = relop;
              }
          }
          this->ui->widget->loaded = true;
          this->ui->widget->scrmax = fcount;
          this->ui->widget->repaint();
          ui->verticalScrollBar->setMinimum(0);
          ui->verticalScrollBar->setMaximum(fcount - 7);
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      bool MainWindow::fileExists(int inv)
      {
          QString numstr = QString::number(inv,16);
          while (numstr.length()<4){
              numstr = "0" + numstr;
          }
          QString fname = ":/images/images/bloc" + numstr + ".png";
          QFile fil(fname);
          if (fil.exists()) return true;
          return false;
      }
      
      
      

      and this is my mainwindow.h

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      #include <QFile>
      #include <QImage>
      #include <QImageReader>
      QT_BEGIN_NAMESPACE
      namespace Ui { class MainWindow; }
      QT_END_NAMESPACE
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          MainWindow(QWidget *parent = nullptr);
          ~MainWindow();
      
      private:
          Ui::MainWindow *ui;
          QImage *myimg;
          int *relop;
      
          bool fileExists(int inv);
      };
      #endif // MAINWINDOW_H
      
      
      artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #2

      @AI_Messiah Hi,
      what is myimg = new QImage[fcount];?
      what is relop = new int[fcount];?

      QImage is not a container, https://doc.qt.io/qt-5/qimage.html
      What you are looking for is rather QVector<QImage*>

      int, then again, is a simple type. Again, what you are looking for is QVector<int>.

      Then we have reader.fileName() = fname; - when you look into the documentation you'll surely notice that QString QImageReader::fileName() const and being bold enough to read deeper you'll notice that you're looking for void QImageReader::setFileName(const QString &fileName) method.

      Having more or less fixed that:

      • you can skip this->ui and keep just ui.

      I don't know what the widgets in the UI are but I can bet that you set them wrong, they most probably have their own setter methods.

      For more information please re-read.

      Kind Regards,
      Artur

      1 Reply Last reply
      1
      • A AI_Messiah

        I am trying to load images that are in a directory from a resource file.

        This is my mainwindow.cpp

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        
        MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent)
            , ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        
            int fcount = 0;
            int fdist = 0;
            QString numstr;
            QString fname;
            QImageReader reader;
            for (int i=0;i<65536;i++){
                if (fileExists(i)) fcount++;
            }
            myimg = new QImage[fcount];
            relop = new int[fcount];
            for (int i=0;i<65536;i++){
                if (fileExists(i)){
                    numstr = QString::number(i,16);
                    while (numstr.length()<4){
                        numstr = "0" + numstr;
                    }
                    fname = ":/images/images/bloc" + numstr + ".png";
                    reader.fileName() = fname;
                    myimg[fdist] = reader.read();
                    relop[fdist] = i;
                    fdist++;
                    this->ui->widget->images = myimg;
                    this->ui->widget->relind = relop;
                    this->ui->widget_2->images = myimg;
                    this->ui->widget_2->relind = relop;
                }
            }
            this->ui->widget->loaded = true;
            this->ui->widget->scrmax = fcount;
            this->ui->widget->repaint();
            ui->verticalScrollBar->setMinimum(0);
            ui->verticalScrollBar->setMaximum(fcount - 7);
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        bool MainWindow::fileExists(int inv)
        {
            QString numstr = QString::number(inv,16);
            while (numstr.length()<4){
                numstr = "0" + numstr;
            }
            QString fname = ":/images/images/bloc" + numstr + ".png";
            QFile fil(fname);
            if (fil.exists()) return true;
            return false;
        }
        
        
        

        and this is my mainwindow.h

        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H
        
        #include <QMainWindow>
        #include <QFile>
        #include <QImage>
        #include <QImageReader>
        QT_BEGIN_NAMESPACE
        namespace Ui { class MainWindow; }
        QT_END_NAMESPACE
        
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            MainWindow(QWidget *parent = nullptr);
            ~MainWindow();
        
        private:
            Ui::MainWindow *ui;
            QImage *myimg;
            int *relop;
        
            bool fileExists(int inv);
        };
        #endif // MAINWINDOW_H
        
        
        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        @AI_Messiah said in Could someone tell me why this image loader will not work?:

        I am trying to load images that are in a directory from a resource file.

        https://doc.qt.io/qt-5/qdiriterator.html

        There are examples as well.
        Then keeping the images in a vector (which you should)

        QVector<QImage> images;
        // ...
        QImage image(pathToImage);
        if (image.isNull()) // etc ...
            ;
        
        images.append(image);
        

        @artwaw said in Could someone tell me why this image loader will not work?:

        What you are looking for is rather QVector<QImage*>

        QImage is implicitly shared, no reason to keep it in the heap.

        Read and abide by the Qt Code of Conduct

        artwawA 1 Reply Last reply
        1
        • kshegunovK kshegunov

          @AI_Messiah said in Could someone tell me why this image loader will not work?:

          I am trying to load images that are in a directory from a resource file.

          https://doc.qt.io/qt-5/qdiriterator.html

          There are examples as well.
          Then keeping the images in a vector (which you should)

          QVector<QImage> images;
          // ...
          QImage image(pathToImage);
          if (image.isNull()) // etc ...
              ;
          
          images.append(image);
          

          @artwaw said in Could someone tell me why this image loader will not work?:

          What you are looking for is rather QVector<QImage*>

          QImage is implicitly shared, no reason to keep it in the heap.

          artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #4

          @kshegunov said in Could someone tell me why this image loader will not work?:

          QImage is implicitly shared, no reason to keep it in the heap.

          Dully noted, thank you.

          For more information please re-read.

          Kind Regards,
          Artur

          1 Reply Last reply
          1

          • Login

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