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.
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 -
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@AI_Messiah Hi,
what ismyimg = new QImage[fcount];?
what isrelop = 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 thatQString QImageReader::fileName() constand being bold enough to read deeper you'll notice that you're looking forvoid QImageReader::setFileName(const QString &fileName)method.Having more or less fixed that:
- you can skip
this->uiand keep justui.
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.
- you can skip
-
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@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*>
QImageis implicitly shared, no reason to keep it in the heap. -
@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*>
QImageis implicitly shared, no reason to keep it in the heap.@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.