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. Image viewer - moving to previous image in directory
Forum Update on Monday, May 27th 2025

Image viewer - moving to previous image in directory

Scheduled Pinned Locked Moved Solved General and Desktop
imagedirectoryqstringlist
13 Posts 3 Posters 5.5k Views
  • 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
    Avec
    wrote on last edited by
    #1

    Hi everyone,
    I am trying to make image viewer in Qt and I am struggling with moving to the next/prev image in current directory

    I open new picture with

    //your code here
    ```void Viewer::open(){
    QString fileName = QFileDialog::getOpenFileName(this,
         tr("Select image"), QDir::homePath(),
         tr("Images (*.png *.xpm *.jpg *.jpeg);;All files (*)"));
    if(!fileName.isEmpty()){
        QImage image(fileName);
        lblImage->setPixmap(QPixmap::fromImage(image));
        lblImage->adjustSize();
        QDir fileDir = QFileInfo(fileName).absoluteDir();
        QStringList filters;
        filters << "*.png" << "*.xpm" << "*.jpg" << "*.jpeg";
        fileDir.setNameFilters(filters);
        strlDir = fileDir.entryList();
        iter = strlDir.indexOf(fileName);
    }
    }
    

    for moving to the previous I use

    void Viewer::prev(){
    iter = iter - 1;
    if(iter!=-1){
        QString newFileName = strlDir[iter];
        open(newFileName);
    }
    }
    

    where open(newFileName) is the same as open(), only without the getting the fileName. The program falls down, when I try to move to the previous. It says "ASSERT failure in QList::operator[]: "index out of range"" . When I fix iter in prev() it fails to open the file. I really cannot find out, whats wrong with that, I haven't worked with directories nor QStringList before. Could anybody help me? Thanks a lot

    A 1 Reply Last reply
    0
    • A Avec

      Hi everyone,
      I am trying to make image viewer in Qt and I am struggling with moving to the next/prev image in current directory

      I open new picture with

      //your code here
      ```void Viewer::open(){
      QString fileName = QFileDialog::getOpenFileName(this,
           tr("Select image"), QDir::homePath(),
           tr("Images (*.png *.xpm *.jpg *.jpeg);;All files (*)"));
      if(!fileName.isEmpty()){
          QImage image(fileName);
          lblImage->setPixmap(QPixmap::fromImage(image));
          lblImage->adjustSize();
          QDir fileDir = QFileInfo(fileName).absoluteDir();
          QStringList filters;
          filters << "*.png" << "*.xpm" << "*.jpg" << "*.jpeg";
          fileDir.setNameFilters(filters);
          strlDir = fileDir.entryList();
          iter = strlDir.indexOf(fileName);
      }
      }
      

      for moving to the previous I use

      void Viewer::prev(){
      iter = iter - 1;
      if(iter!=-1){
          QString newFileName = strlDir[iter];
          open(newFileName);
      }
      }
      

      where open(newFileName) is the same as open(), only without the getting the fileName. The program falls down, when I try to move to the previous. It says "ASSERT failure in QList::operator[]: "index out of range"" . When I fix iter in prev() it fails to open the file. I really cannot find out, whats wrong with that, I haven't worked with directories nor QStringList before. Could anybody help me? Thanks a lot

      A Offline
      A Offline
      ambershark
      wrote on last edited by ambershark
      #2

      @Avec If you are taking a "previous" of the first image then that is a -1 index. So that is why you got the assertion. To fix that you would just check that your iterator - 1 is within the valid range for that list, if not, don't decrement it.

      Same with next. Confirm that iter + 1 is not beyond the end of the list.

      In your function it would look like this:

      void Viewer::prev() 
      {
         if (iter -1 < 0)
            return;
      
         iter--;
         open(strlDir.at(iter));
      }
      

      Also side note, using the word iter is wrong since you are not using an iterator. You are user iter as an index so it should more aptly be named index or something like that. Using the word iter will confuse you later on and any other developer that works on your code.

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      A 1 Reply Last reply
      0
      • A Offline
        A Offline
        Avec
        wrote on last edited by
        #3

        thank you very much, it fixed the iter problem, but program still fails to open the previous picture...

        A 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi and welcome to devnet,

          What about using a QFileSystemModel ? That would likely simplify the browsing part. Then you can use a QListView for the file selection part. Finally you can use a QLabel to show the content of the image.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply
          1
          • A Avec

            thank you very much, it fixed the iter problem, but program still fails to open the previous picture...

            A Offline
            A Offline
            ambershark
            wrote on last edited by
            #5

            @Avec Glad the index is fixed. :)

            So for the next problem.. can we see the open(filename) code? All you showed was the open function that gets the names, not the one you call with the new name.

            Something easy to test with is just throw a QDebug() or 3 in there showing what the filename is and see if it is correct. Then do some quick debugging on if the image is loading properly.

            But I can tell you what the issue is or how to solve it if you post the code.

            My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

            A 1 Reply Last reply
            0
            • A ambershark

              @Avec Glad the index is fixed. :)

              So for the next problem.. can we see the open(filename) code? All you showed was the open function that gets the names, not the one you call with the new name.

              Something easy to test with is just throw a QDebug() or 3 in there showing what the filename is and see if it is correct. Then do some quick debugging on if the image is loading properly.

              But I can tell you what the issue is or how to solve it if you post the code.

              A Offline
              A Offline
              Avec
              wrote on last edited by
              #6

              @ambershark
              open(newFileName) uses the same code as open(), only without the part getting the fileName, so it starts with the if.

              1 Reply Last reply
              0
              • A ambershark

                @Avec If you are taking a "previous" of the first image then that is a -1 index. So that is why you got the assertion. To fix that you would just check that your iterator - 1 is within the valid range for that list, if not, don't decrement it.

                Same with next. Confirm that iter + 1 is not beyond the end of the list.

                In your function it would look like this:

                void Viewer::prev() 
                {
                   if (iter -1 < 0)
                      return;
                
                   iter--;
                   open(strlDir.at(iter));
                }
                

                Also side note, using the word iter is wrong since you are not using an iterator. You are user iter as an index so it should more aptly be named index or something like that. Using the word iter will confuse you later on and any other developer that works on your code.

                A Offline
                A Offline
                Avec
                wrote on last edited by
                #7

                @ambershark

                void Viewer::prev(){
                    if(iter -1 < 0){
                        QMessageBox::warning(this, tr("Error"), tr("No previous image"));
                        return;
                    }
                    iter--;
                    open(strlDir.at(iter));
                }
                

                ok, actually, the iter thing does not work, the must be something wrong with the way I am getting the list, I guess... It always gives me the "No previous image" here

                also I add the open(fileName) code

                void Viewer::open(QString fileName){
                    if(!fileName.isEmpty()){
                        QImage image(fileName);
                        if(image.isNull()){
                            QMessageBox::warning(this, tr("Error"),
                                                 tr("Failed to load selected image"));
                            return;
                        }
                        lblImage->setPixmap(QPixmap::fromImage(image));
                        lblImage->adjustSize();
                        toggleZoomActions(true);
                        actZoomFit->setEnabled(true);
                        statusBar()->showMessage(tr("Image %1 was loaded").arg(fileName),
                                                 3000);
                        zoomOrig();
                        QDir fileDir = QFileInfo(fileName).absoluteDir();
                        QStringList filters;
                        filters << "*.png" << "*.xpm" << "*.jpg" << "*.jpeg";
                        fileDir.setNameFilters(filters);
                        strlDir = fileDir.entryList();
                        iter = strlDir.indexOf(fileName);
                    }
                }
                
                1 Reply Last reply
                0
                • SGaistS SGaist

                  Hi and welcome to devnet,

                  What about using a QFileSystemModel ? That would likely simplify the browsing part. Then you can use a QListView for the file selection part. Finally you can use a QLabel to show the content of the image.

                  A Offline
                  A Offline
                  Avec
                  wrote on last edited by
                  #8

                  @SGaist
                  tank you for your advice, but I would like to understand what is wrong in this code anyway :)

                  A 1 Reply Last reply
                  0
                  • A Avec

                    @SGaist
                    tank you for your advice, but I would like to understand what is wrong in this code anyway :)

                    A Offline
                    A Offline
                    ambershark
                    wrote on last edited by
                    #9

                    @Avec You keep not showing enough code for me to figure it out. :( My guess is iter is being screwed up somewhere but I don't see it in the code you've shared so far.

                    I can write you a quick class that will get you the filenames if you want. It sounds like that is where your problem is. It's not in the display code but in the list handling for the file names.

                    My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                    A 1 Reply Last reply
                    0
                    • A ambershark

                      @Avec You keep not showing enough code for me to figure it out. :( My guess is iter is being screwed up somewhere but I don't see it in the code you've shared so far.

                      I can write you a quick class that will get you the filenames if you want. It sounds like that is where your problem is. It's not in the display code but in the list handling for the file names.

                      A Offline
                      A Offline
                      Avec
                      wrote on last edited by
                      #10

                      @ambershark
                      header

                      #ifndef VIEWER_H
                      #define VIEWER_H
                      
                      #include <QMainWindow>
                      
                      class QLabel;
                      class QScrollArea;
                      class QAction;
                      class QMenu;
                      class QToolBar;
                      class QStringList;
                      
                      class Viewer : public QMainWindow
                      {
                          Q_OBJECT
                          QLabel *lblImage;
                          QScrollArea *scrollArea;
                          QAction *actionQuit;
                          QAction *actOpen;
                          QMenu *mnuFile;
                          QToolBar *toolBar;
                          QAction *actZoomIn;
                          QAction *actZoomOut;
                          QAction *actOrigSize;
                          QAction *actZoomFit;
                          QAction *actPrev;
                          QAction *actNext;
                          QMenu *mnuZoom;
                          QMenu *mnuMove;
                          QStringList strlDir;
                          void zoom(double factor);
                          void toggleZoomActions(bool enable);
                          double zoomFactor;
                          int iter;
                      public:
                          explicit Viewer(QWidget *parent = 0);
                      
                      signals:
                      
                      public slots:
                          void open(QString fileName);
                          void open();
                          void zoomIn();
                          void zoomOut();
                          void zoomOrig();
                          void zoomFit();
                          void next();
                          void prev();
                      };
                      
                      #endif // VIEWER_H
                      

                      source

                      #include "viewer.h"
                      //#include everything needed
                      
                      Viewer::Viewer(QWidget *parent) : QMainWindow(parent)
                      {
                          lblImage = new QLabel;
                          lblImage->setSizePolicy(QSizePolicy::Ignored,
                                                  QSizePolicy::Ignored);
                          lblImage->setScaledContents(true);
                          scrollArea = new QScrollArea;
                          scrollArea->setWidget(lblImage);
                          this->setCentralWidget(scrollArea);
                          this->resize(1024, 768);
                          this->setWindowTitle(tr("Qt Image Viewer"));
                      
                          //akcequit
                          QIcon iconQuit = QIcon::fromTheme("quit-icon", QIcon("://icon/quit.jpg"));
                          actionQuit = new QAction(tr("&Quit"), this);
                          actionQuit->setShortcut(QKeySequence::Quit); //ne Win
                          actionQuit->setIcon(iconQuit);
                          actionQuit->setToolTip(tr("Exits the viewer"));
                          actionQuit->setStatusTip(tr("Closes the application"));
                      
                          //act Open
                          QIcon iconOpen = QIcon::fromTheme("open-icon", QIcon("://icon/open.png"));
                          actOpen = new QAction(tr("&Open"), this);
                          actOpen->setIcon(iconOpen);
                          actOpen->setToolTip(tr("Opens image"));
                          actOpen->setStatusTip(tr("Open image from file"));
                      
                          mnuFile = new QMenu(tr("&File"), this);
                          mnuFile->addAction(actionQuit);
                          mnuFile->addAction(actOpen);
                      
                          toolBar = this->addToolBar(tr("Main tool bar"));
                          toolBar->addAction(actionQuit);
                          toolBar->addAction(actOpen);
                      
                          //act Zoom In
                      
                          //act Zoom out
                      
                          //act Orig Size
                          QIcon iconOrigSize = QIcon::fromTheme("zoom-orig", QIcon("://icon/zoom.png"));
                          actOrigSize = new QAction(tr("Zoom original size"), this);
                          actOrigSize->setIcon(iconOrigSize);
                          actOrigSize->setToolTip(tr("Original size of the picture"));
                          actOrigSize->setStatusTip(tr("Zooms the original siye of the picture"));
                      
                          //act zoom fit
                          QIcon iconZoomFit = QIcon::fromTheme("zoom-fit", QIcon("://icon/zoom_to_fit.png"));
                          actZoomFit = new QAction(tr("Fit in window"), this);
                          actZoomFit->setIcon(iconZoomFit);
                          actZoomFit->setToolTip(tr("Fits the picture in the window"));
                          actZoomFit->setStatusTip(tr("Fits current picture in the window"));
                          actZoomFit->setCheckable(true);
                          actZoomFit->setChecked(false);
                      
                          //act prev
                          QIcon iconPrev = QIcon::fromTheme("prev", QIcon("://icon/prev.png"));
                          actPrev = new QAction(tr("Previous image"), this);
                          actPrev->setIcon(iconPrev);
                          actPrev->setToolTip(tr("Moves to the previous image"));
                          actPrev->setStatusTip(tr("Moves to the previous image"));
                      
                          //act next
                      
                          mnuZoom = new QMenu(tr("&Zoom"), this);
                          mnuZoom->addAction(actZoomFit);
                          mnuZoom->addAction(actOrigSize);
                      
                          mnuMove = new QMenu(tr("&Move"), this);
                          mnuMove->addAction(actPrev);
                      
                          toolBar->addAction(actOrigSize);
                          toolBar->addAction(actZoomFit);
                          toolBar->addAction(actPrev);
                      
                      
                          this->menuBar()->addMenu(mnuFile);
                          this->menuBar()->addMenu(mnuZoom);
                          this->menuBar()->addMenu(mnuMove);
                          connect(actOpen, SIGNAL(triggered()), this, SLOT(open()));
                          connect(actOrigSize, SIGNAL(triggered()), this, SLOT(zoomOrig()));
                          connect(actZoomFit, SIGNAL(triggered()), this, SLOT(zoomFit()));
                          connect(actPrev, SIGNAL(triggered()), this, SLOT(prev()));
                          toggleZoomActions(false);
                          actZoomFit->setEnabled(false);
                      }
                      void Viewer::open(){
                          QString fileName = QFileDialog::getOpenFileName(this,
                               tr("Select image"), QDir::homePath(),
                               tr("Images (*.png *.xpm *.jpg *.jpeg);;All files (*)"));
                          if(!fileName.isEmpty()){
                              QImage image(fileName);
                              if(image.isNull()){
                                  QMessageBox::warning(this, tr("Error"),
                                                       tr("Failed to load selected image"));
                                  return;
                              }
                              lblImage->setPixmap(QPixmap::fromImage(image));
                              lblImage->adjustSize();
                              toggleZoomActions(true);
                              actZoomFit->setEnabled(true);
                              statusBar()->showMessage(tr("Image %1 was loaded").arg(fileName),
                                                       3000);
                              zoomOrig();
                              QDir fileDir = QFileInfo(fileName).absoluteDir();
                              QStringList filters;
                              filters << "*.png" << "*.xpm" << "*.jpg" << "*.jpeg";
                              fileDir.setNameFilters(filters);
                              strlDir = fileDir.entryList();
                              iter = strlDir.indexOf(fileName);
                          }
                      }
                      
                      void Viewer::open(QString fileName){
                          if(!fileName.isEmpty()){
                              QImage image(fileName);
                              if(image.isNull()){
                                  QMessageBox::warning(this, tr("Error"),
                                                       tr("Failed to load selected image"));
                                  return;
                              }
                              lblImage->setPixmap(QPixmap::fromImage(image));
                              lblImage->adjustSize();
                              toggleZoomActions(true);
                              actZoomFit->setEnabled(true);
                              statusBar()->showMessage(tr("Image %1 was loaded").arg(fileName),
                                                       3000);
                              iter = strlDir.indexOf(fileName);
                          }
                      }
                      
                      void Viewer::toggleZoomActions(bool enable){
                          actZoomIn->setEnabled(enable);
                          actZoomOut->setEnabled(enable);
                          actOrigSize->setEnabled(enable);
                      }
                      
                      void Viewer::zoom(double factor){
                          zoomFactor = zoomFactor * factor;
                          lblImage->resize(lblImage->pixmap()->size()*zoomFactor);
                          actZoomIn->setEnabled(zoomFactor < 3.0);
                          actZoomOut->setEnabled(zoomFactor > 0.2);
                      }
                      
                      void Viewer::zoomOrig(){
                          zoomFactor = 1.0;
                          zoom(1.0);
                      }
                      
                      void Viewer::zoomFit(){
                          bool fitted = actZoomFit->isChecked();
                          scrollArea->setWidgetResizable(fitted);
                          if(!fitted){
                              lblImage->adjustSize();
                              zoomOrig();
                          }
                          toggleZoomActions(!fitted);
                      }
                      
                      void Viewer::prev(){
                          if(iter -1 < 0){
                              QMessageBox::warning(this, tr("Error"), tr("No previous image"));
                              return;
                          }
                          iter--;
                          open(strlDir.at(iter));
                      }
                      
                      void Viewer::next(){}
                      

                      the list isn't used anywhere else, except the header...

                      A 1 Reply Last reply
                      0
                      • A Avec

                        @ambershark
                        header

                        #ifndef VIEWER_H
                        #define VIEWER_H
                        
                        #include <QMainWindow>
                        
                        class QLabel;
                        class QScrollArea;
                        class QAction;
                        class QMenu;
                        class QToolBar;
                        class QStringList;
                        
                        class Viewer : public QMainWindow
                        {
                            Q_OBJECT
                            QLabel *lblImage;
                            QScrollArea *scrollArea;
                            QAction *actionQuit;
                            QAction *actOpen;
                            QMenu *mnuFile;
                            QToolBar *toolBar;
                            QAction *actZoomIn;
                            QAction *actZoomOut;
                            QAction *actOrigSize;
                            QAction *actZoomFit;
                            QAction *actPrev;
                            QAction *actNext;
                            QMenu *mnuZoom;
                            QMenu *mnuMove;
                            QStringList strlDir;
                            void zoom(double factor);
                            void toggleZoomActions(bool enable);
                            double zoomFactor;
                            int iter;
                        public:
                            explicit Viewer(QWidget *parent = 0);
                        
                        signals:
                        
                        public slots:
                            void open(QString fileName);
                            void open();
                            void zoomIn();
                            void zoomOut();
                            void zoomOrig();
                            void zoomFit();
                            void next();
                            void prev();
                        };
                        
                        #endif // VIEWER_H
                        

                        source

                        #include "viewer.h"
                        //#include everything needed
                        
                        Viewer::Viewer(QWidget *parent) : QMainWindow(parent)
                        {
                            lblImage = new QLabel;
                            lblImage->setSizePolicy(QSizePolicy::Ignored,
                                                    QSizePolicy::Ignored);
                            lblImage->setScaledContents(true);
                            scrollArea = new QScrollArea;
                            scrollArea->setWidget(lblImage);
                            this->setCentralWidget(scrollArea);
                            this->resize(1024, 768);
                            this->setWindowTitle(tr("Qt Image Viewer"));
                        
                            //akcequit
                            QIcon iconQuit = QIcon::fromTheme("quit-icon", QIcon("://icon/quit.jpg"));
                            actionQuit = new QAction(tr("&Quit"), this);
                            actionQuit->setShortcut(QKeySequence::Quit); //ne Win
                            actionQuit->setIcon(iconQuit);
                            actionQuit->setToolTip(tr("Exits the viewer"));
                            actionQuit->setStatusTip(tr("Closes the application"));
                        
                            //act Open
                            QIcon iconOpen = QIcon::fromTheme("open-icon", QIcon("://icon/open.png"));
                            actOpen = new QAction(tr("&Open"), this);
                            actOpen->setIcon(iconOpen);
                            actOpen->setToolTip(tr("Opens image"));
                            actOpen->setStatusTip(tr("Open image from file"));
                        
                            mnuFile = new QMenu(tr("&File"), this);
                            mnuFile->addAction(actionQuit);
                            mnuFile->addAction(actOpen);
                        
                            toolBar = this->addToolBar(tr("Main tool bar"));
                            toolBar->addAction(actionQuit);
                            toolBar->addAction(actOpen);
                        
                            //act Zoom In
                        
                            //act Zoom out
                        
                            //act Orig Size
                            QIcon iconOrigSize = QIcon::fromTheme("zoom-orig", QIcon("://icon/zoom.png"));
                            actOrigSize = new QAction(tr("Zoom original size"), this);
                            actOrigSize->setIcon(iconOrigSize);
                            actOrigSize->setToolTip(tr("Original size of the picture"));
                            actOrigSize->setStatusTip(tr("Zooms the original siye of the picture"));
                        
                            //act zoom fit
                            QIcon iconZoomFit = QIcon::fromTheme("zoom-fit", QIcon("://icon/zoom_to_fit.png"));
                            actZoomFit = new QAction(tr("Fit in window"), this);
                            actZoomFit->setIcon(iconZoomFit);
                            actZoomFit->setToolTip(tr("Fits the picture in the window"));
                            actZoomFit->setStatusTip(tr("Fits current picture in the window"));
                            actZoomFit->setCheckable(true);
                            actZoomFit->setChecked(false);
                        
                            //act prev
                            QIcon iconPrev = QIcon::fromTheme("prev", QIcon("://icon/prev.png"));
                            actPrev = new QAction(tr("Previous image"), this);
                            actPrev->setIcon(iconPrev);
                            actPrev->setToolTip(tr("Moves to the previous image"));
                            actPrev->setStatusTip(tr("Moves to the previous image"));
                        
                            //act next
                        
                            mnuZoom = new QMenu(tr("&Zoom"), this);
                            mnuZoom->addAction(actZoomFit);
                            mnuZoom->addAction(actOrigSize);
                        
                            mnuMove = new QMenu(tr("&Move"), this);
                            mnuMove->addAction(actPrev);
                        
                            toolBar->addAction(actOrigSize);
                            toolBar->addAction(actZoomFit);
                            toolBar->addAction(actPrev);
                        
                        
                            this->menuBar()->addMenu(mnuFile);
                            this->menuBar()->addMenu(mnuZoom);
                            this->menuBar()->addMenu(mnuMove);
                            connect(actOpen, SIGNAL(triggered()), this, SLOT(open()));
                            connect(actOrigSize, SIGNAL(triggered()), this, SLOT(zoomOrig()));
                            connect(actZoomFit, SIGNAL(triggered()), this, SLOT(zoomFit()));
                            connect(actPrev, SIGNAL(triggered()), this, SLOT(prev()));
                            toggleZoomActions(false);
                            actZoomFit->setEnabled(false);
                        }
                        void Viewer::open(){
                            QString fileName = QFileDialog::getOpenFileName(this,
                                 tr("Select image"), QDir::homePath(),
                                 tr("Images (*.png *.xpm *.jpg *.jpeg);;All files (*)"));
                            if(!fileName.isEmpty()){
                                QImage image(fileName);
                                if(image.isNull()){
                                    QMessageBox::warning(this, tr("Error"),
                                                         tr("Failed to load selected image"));
                                    return;
                                }
                                lblImage->setPixmap(QPixmap::fromImage(image));
                                lblImage->adjustSize();
                                toggleZoomActions(true);
                                actZoomFit->setEnabled(true);
                                statusBar()->showMessage(tr("Image %1 was loaded").arg(fileName),
                                                         3000);
                                zoomOrig();
                                QDir fileDir = QFileInfo(fileName).absoluteDir();
                                QStringList filters;
                                filters << "*.png" << "*.xpm" << "*.jpg" << "*.jpeg";
                                fileDir.setNameFilters(filters);
                                strlDir = fileDir.entryList();
                                iter = strlDir.indexOf(fileName);
                            }
                        }
                        
                        void Viewer::open(QString fileName){
                            if(!fileName.isEmpty()){
                                QImage image(fileName);
                                if(image.isNull()){
                                    QMessageBox::warning(this, tr("Error"),
                                                         tr("Failed to load selected image"));
                                    return;
                                }
                                lblImage->setPixmap(QPixmap::fromImage(image));
                                lblImage->adjustSize();
                                toggleZoomActions(true);
                                actZoomFit->setEnabled(true);
                                statusBar()->showMessage(tr("Image %1 was loaded").arg(fileName),
                                                         3000);
                                iter = strlDir.indexOf(fileName);
                            }
                        }
                        
                        void Viewer::toggleZoomActions(bool enable){
                            actZoomIn->setEnabled(enable);
                            actZoomOut->setEnabled(enable);
                            actOrigSize->setEnabled(enable);
                        }
                        
                        void Viewer::zoom(double factor){
                            zoomFactor = zoomFactor * factor;
                            lblImage->resize(lblImage->pixmap()->size()*zoomFactor);
                            actZoomIn->setEnabled(zoomFactor < 3.0);
                            actZoomOut->setEnabled(zoomFactor > 0.2);
                        }
                        
                        void Viewer::zoomOrig(){
                            zoomFactor = 1.0;
                            zoom(1.0);
                        }
                        
                        void Viewer::zoomFit(){
                            bool fitted = actZoomFit->isChecked();
                            scrollArea->setWidgetResizable(fitted);
                            if(!fitted){
                                lblImage->adjustSize();
                                zoomOrig();
                            }
                            toggleZoomActions(!fitted);
                        }
                        
                        void Viewer::prev(){
                            if(iter -1 < 0){
                                QMessageBox::warning(this, tr("Error"), tr("No previous image"));
                                return;
                            }
                            iter--;
                            open(strlDir.at(iter));
                        }
                        
                        void Viewer::next(){}
                        

                        the list isn't used anywhere else, except the header...

                        A Offline
                        A Offline
                        ambershark
                        wrote on last edited by
                        #11

                        @Avec Hi, sorry about the delay. Been really busy.

                        So the code actually looks correct. If you are getting the "no previous" message as you stated then that probably means your index (iter) was on the first entry in the file list. I.e. iter == 0 then when you do prev() it sets it to -1 which causes the failure.

                        Did you want it to wrap when you do prev() to the end of the list or something? If not, it sounds like it is working as intended. If so, just do:

                        if (iter == 0)
                           iter = strlDir.size()-1;
                        else
                           iter--;
                        

                        My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                        A 1 Reply Last reply
                        0
                        • A ambershark

                          @Avec Hi, sorry about the delay. Been really busy.

                          So the code actually looks correct. If you are getting the "no previous" message as you stated then that probably means your index (iter) was on the first entry in the file list. I.e. iter == 0 then when you do prev() it sets it to -1 which causes the failure.

                          Did you want it to wrap when you do prev() to the end of the list or something? If not, it sounds like it is working as intended. If so, just do:

                          if (iter == 0)
                             iter = strlDir.size()-1;
                          else
                             iter--;
                          
                          A Offline
                          A Offline
                          Avec
                          wrote on last edited by
                          #12

                          @ambershark
                          Hi :)
                          I have found the problem! entryList() gets only the names of files, but my fileName consist of the path, too, so when I tried to find fileName in strlDir, I failed, because it did not contain it.
                          thanks for your help :)

                          A 1 Reply Last reply
                          2
                          • A Avec

                            @ambershark
                            Hi :)
                            I have found the problem! entryList() gets only the names of files, but my fileName consist of the path, too, so when I tried to find fileName in strlDir, I failed, because it did not contain it.
                            thanks for your help :)

                            A Offline
                            A Offline
                            ambershark
                            wrote on last edited by
                            #13

                            @Avec Oh duh.. I should have thought of that as a potential reason. I just assumed that you were in the directory with the files as your current dir.

                            Glad it's all solved. :)

                            My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                            1 Reply Last reply
                            0

                            • Login

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