Solved Creating Next/Previous Function using Vectors problem.
-
I'm currently creating a program with images and strings being stored in vectors. Now what I wanted to do is for the program to display the first 4 data from the vectors to the program, and if the user pressed next, then it'll display the next 4, press previous, then it'll go back to the previous 4. Usual rules in displaying data applies.
I already made most of the code, but it seems that I'm lost as to where I would put the code to perform such a function, for I have initally placed the entire function within the MainWindow, but when I execute the code, I would get the vector out of bound error, mainly because it's running under a while true condition since I want display the info then wait for input from the user if s/he wants to go next or not so that the display for the next batch of data would be done.
How in the world am I going to implement such a thing in a way that doesn't require me to perform a while(true) condition that would make the entire thing out of bounds?
Thanks in advance!
Here is the code in the mainwindow:
#include "opencv2\opencv.hpp" #include "mainwindow.h" #include "ui_mainwindow.h" #include <iostream> #include <string> #include <cstring> #include <opencv2/core/types_c.h> #include <QDebug> #include <QImage> #include <QPixmap> #include <QtGlobal> #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/imgproc/types_c.h" using namespace std; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); //for initialization purposes // get label dimensions int w = ui->mul1->width(); int h = ui->mul1->height(); //initializing all label images QLabel* pt[]={ui->p1,ui->p2,ui->p3,ui->p4}; QLabel* mim[]={ui->mul1,ui->mul2,ui->mul3,ui->mul4}; QLabel* cim[]={ui->cen1,ui->cen2,ui->cen3,ui->cen4}; QLabel* yr[]={ui->yel1,ui->yel2,ui->yel3,ui->yel4}; QLabel* tim[]={ui->ther1,ui->ther2,ui->ther3,ui->ther4}; QLabel* aim[]={ui->avg1,ui->avg2,ui->avg3,ui->avg4}; QLabel* tri[]={ui->tem1,ui->tem2,ui->tem3,ui->tem4}; QLabel* trt[]={ui->tres1,ui->tres2,ui->tres3,ui->tres4}; vector<QLabel> muli(mim, mim+sizeof(mim) / sizeof(mim[0]) ); QImage no_i = QImage("no-img.png"); QImage gr = QImage("gray.jpg"); no_i.scaled(w,h); gr.scaled(w,h); unsigned long fi; //unsigned long fil; unsigned long yi; unsigned long il=0; //for debug Databank datab = testpass(); //end fi = static_cast<unsigned long>(datab.mi.size()); //fi = 4; yi = static_cast<unsigned long>(datab.yela.size()); qDebug()<<"Number of Multispectral images in this vector(MW): "<<fi<<endl; qDebug()<<"Number of Yellow strings in this vector(MW): "<<yi<<endl; if(fi==0){ while(il<4){ QString plt = QString("Plot #%1").arg(il+1); QString ncen = QString("No Dominant color found."); QString ntem = QString("No Average temp found."); pt[il]->setText(plt); mim[il]->setPixmap(QPixmap::fromImage(no_i)); cim[il]->setPixmap(QPixmap::fromImage(gr)); yr[il]->setText(ncen); tim[il]->setPixmap(QPixmap::fromImage(no_i)); aim[il]->setPixmap(QPixmap::fromImage(gr)); tri[il]->setText(ntem); trt[il]->setText("No result can be derived."); il++; } ui->prev->setEnabled(false); ui->next->setEnabled(false); } else if(fi<=4){ unsigned long mmil=0.0000000; while(mmil<fi){ QString plt = QString("Plot #%1").arg(mmil+1); pt[mmil]->setText(plt); mim[mmil]->setPixmap(QPixmap::fromImage(datab.mi[mmil]).scaled(125, 125)); cim[mmil]->setPixmap(QPixmap::fromImage(datab.cenp[mmil]).scaled(200, 25)); yr[mmil]->setText(QString::fromStdString(datab.yela[mmil])); tim[mmil]->setPixmap(QPixmap::fromImage(datab.ti[mmil]).scaled(125, 125)); aim[mmil]->setPixmap(QPixmap::fromImage(datab.avtemp[mmil]).scaled(125, 125)); tri[mmil]->setText(QString::fromStdString(datab.lo[mmil])); trt[mmil]->setText(QString::fromStdString(datab.tr[mmil])); mmil++; } unsigned long r=4-fi; il=0; while(il<r){ QString plt = QString("Plot #%1").arg(mmil+1); QString ncen = QString("No Dominant color found."); QString ntem = QString("No Average temp found."); pt[mmil]->setText(plt); mim[mmil]->setPixmap(QPixmap::fromImage(no_i)); cim[mmil]->setPixmap(QPixmap::fromImage(gr)); yr[mmil]->setText(ncen); tim[mmil]->setPixmap(QPixmap::fromImage(no_i)); aim[mmil]->setPixmap(QPixmap::fromImage(gr)); tri[mmil]->setText(ntem); trt[mmil]->setText("No result can be derived."); il++; mmil++; } ui->prev->setEnabled(false); ui->next->setEnabled(false); } else if(fi>4){ unsigned long d = fi/4; int rm = fi%4; if(rm!=0) d++; int ni = 0; unsigned long da=0; while(da<4){ QString plt = QString("Plot #%1").arg(da+1); pt[da]->setText(plt); mim[da]->setPixmap(QPixmap::fromImage(datab.mi[da]).scaled(125, 125)); cim[da]->setPixmap(QPixmap::fromImage(datab.cenp[da]).scaled(200, 25)); yr[da]->setText(QString::fromStdString(datab.yela[da])); tim[da]->setPixmap(QPixmap::fromImage(datab.ti[da]).scaled(125, 125)); aim[da]->setPixmap(QPixmap::fromImage(datab.avtemp[da]).scaled(125, 125)); tri[da]->setText(QString::fromStdString(datab.lo[da])); trt[da]->setText(QString::fromStdString(datab.tr[da])); da++; } ui->prev->setEnabled(false); unsigned long dn = d; unsigned long adi=0; while(dn<=d){ da=0; if(dn==d){ ui->prev->setEnabled(false); }else if(dn==dn-1){ ui->next->setEnabled(false); } if(ui->next->isDown()==true){ adi=adi+4; dn--; }else if(ui->prev->isDown()==true){ adi=adi-4; dn++; } while(da<4){ QString plt = QString("Plot #%1").arg(da+1+adi); pt[da]->setText(plt); mim[da]->setPixmap(QPixmap::fromImage(datab.mi[da+adi]).scaled(125, 125)); cim[da]->setPixmap(QPixmap::fromImage(datab.cenp[da+adi]).scaled(200, 25)); yr[da]->setText(QString::fromStdString(datab.yela[da+adi])); tim[da]->setPixmap(QPixmap::fromImage(datab.ti[da+adi]).scaled(125, 125)); aim[da]->setPixmap(QPixmap::fromImage(datab.avtemp[da+adi]).scaled(125, 125)); tri[da]->setText(QString::fromStdString(datab.lo[da+adi])); trt[da]->setText(QString::fromStdString(datab.tr[da+adi])); da++; } } } //qDebug() << "App path : " << qApp->applicationDirPath();//for debug paths //for the restart of the GUI program } MainWindow::~MainWindow() { delete ui; }
-
Hi and welcome to devnet,
The loop is your error.
You should implement your navigation as a result of a couple of previous/next button click.
In the slots associated with these clicks, update the current index the way you want it and then trigger the update of your labels.
-
Thanks a bunch! Though I had some errors created from adjusting to what you have suggested, I finally got it working!