Image viewer - moving to previous image in directory
-
@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.
-
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.
-
@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.
-
@ambershark
open(newFileName) uses the same code as open(), only without the part getting the fileName, so it starts with the if. -
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); } }
-
@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.
-
@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...
-
@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 doprev()
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--;
-
@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 :)