md612
md612

Reputation: 330

How to handle the reverse order using QStringList::const_iterator in QT

I am developing an image viewer app on android platform using QT. The image viewer has the functions of moving to the next image and moving to the previous image by clicking buttons.

I am using the QStringList::const_iterator to control the method of moving. However, only the function of moving to the next image can be successfully implemented, while the function of moving to the previous image fails to work. The code addPixmap(*m_imageIt); cannot be added into the relative button's function. Otherwise, the app will get stopped. Please help me.

showpic.h

#ifndef SHOWPIC_H
#define SHOWPIC_H

#include <QWidget>
#include <QTimer>


namespace Ui {
class ShowPic;
}

class ShowPic : public QWidget
{
    Q_OBJECT

public:
    explicit ShowPic(QWidget *parent = 0);
    ~ShowPic();

public:
    void addPixmap(const QPixmap &pixmap);
    void startPlay(QStringList infilenames); 

private slots:
    void on_pushButton_3_clicked();
    void tick();

    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_4_clicked();

private:
    Ui::ShowPic *ui;
    QStringList::const_iterator m_imageIt;
    QTimer m_timer;
    QStringList filenames;
    bool flag = false;
};

#endif // SHOWPIC_H

showpic.cpp

#include "showpic.h"
#include "ui_showpic.h"

ShowPic::ShowPic(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ShowPic)
{
    ui->setupUi(this);
    m_timer.setInterval(1000);
    connect(&m_timer, SIGNAL(timeout()), this, SLOT(tick()));
}

ShowPic::~ShowPic()
{
    delete ui;
}

void ShowPic::tick(){
    addPixmap(*m_imageIt);
    m_imageIt ++;
    if(m_imageIt == filenames.end()){        
        m_imageIt = filenames.begin();
    }
}

void ShowPic::addPixmap(const QPixmap &pixmap){
    ui->graphicsView->setScene(new QGraphicsScene);   
    ui->graphicsView->scene()->addPixmap(pixmap);
    ui->graphicsView->fitInView(ui->graphicsView->scene()->itemsBoundingRect() ,Qt::KeepAspectRatio);
}

void ShowPic::startPlay(QStringList infilenames){
    filenames = infilenames;
    m_imageIt = filenames.begin();
    m_timer.start();
    flag == false;
    QString d("Stop");
    ui->pushButton_3->setText(d);
}

void ShowPic::on_pushButton_3_clicked() //play
{
    if(flag == false){
        m_timer.stop();
        QString d("Play");
        ui->pushButton_3->setText(d);
        flag = true;
    }else{
        m_timer.start();
        QString d("Stop");
        ui->pushButton_3->setText(d);
        flag = false;
    }
}

void ShowPic::on_pushButton_clicked() //move to the prev image
{

    if(!filenames.isEmpty()){
        m_timer.stop();
        m_imageIt--;
        if(m_imageIt != filenames.begin()){
            addPixmap(*m_imageIt);
        }else{

            m_imageIt = filenames.end();
            //addPixmap(*m_imageIt);  //????

        }

    }
}

void ShowPic::on_pushButton_2_clicked()  //move to the next image
{
    if(!filenames.isEmpty()){
        m_timer.stop();
        m_imageIt++;
        if(m_imageIt != filenames.end()){
            addPixmap(*m_imageIt);
        }else{
            m_imageIt = filenames.begin();
            addPixmap(*m_imageIt);
        }
    }
}

I have changed the code using the integer way. Finally, it works now.

showpic.h

#ifndef SHOWPIC_H
#define SHOWPIC_H

#include <QWidget>
#include <QTimer>

namespace Ui {
class ShowPic;
}

class ShowPic : public QWidget
{
    Q_OBJECT

public:
    explicit ShowPic(QWidget *parent = 0);
    ~ShowPic();

public:
    void addPixmap(const QPixmap &pixmap);
    void startPlay(QStringList infilenames);

private slots:
    void on_pushButton_3_clicked();
    void tick();

    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_4_clicked();

private:
    Ui::ShowPic *ui;
    //QStringList::const_iterator m_imageIt;
    int index;
    QTimer m_timer;
    QStringList filenames;
    bool flag = false;
};

#endif // SHOWPIC_H

showpic.cpp

#include "showpic.h"
#include "ui_showpic.h"

ShowPic::ShowPic(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ShowPic)
{
    ui->setupUi(this);
    m_timer.setInterval(1000);
    connect(&m_timer, SIGNAL(timeout()), this, SLOT(tick()));
    index = 0;
}

ShowPic::~ShowPic()
{
    delete ui;
}

void ShowPic::tick(){
    addPixmap(QPixmap(filenames.at(index)));
    index++;
    if(index == filenames.size()){
        index = 0;
    }   
}

void ShowPic::addPixmap(const QPixmap &pixmap){    

    ui->graphicsView->setScene(new QGraphicsScene);
    //ui->graphicsView->setScene(ui->graphicsView->scene());
    ui->graphicsView->scene()->addPixmap(pixmap);
    ui->graphicsView->fitInView(ui->graphicsView->scene()->itemsBoundingRect() ,Qt::KeepAspectRatio);
}

void ShowPic::startPlay(QStringList infilenames){
    filenames = infilenames;
    //m_imageIt = filenames.begin();
    index = 0;
    m_timer.start();
    flag == false;
    QString d("Stop");
    ui->pushButton_3->setText(d);
}

void ShowPic::on_pushButton_3_clicked()
{
    if(flag == false){
        m_timer.stop();
        QString d("Play");
        ui->pushButton_3->setText(d);
        flag = true;
    }else{
        m_timer.start();
        QString d("Stop");
        ui->pushButton_3->setText(d);
        flag = false;
    }
}

void ShowPic::on_pushButton_clicked() //prev
{
    if(!filenames.isEmpty()){
        m_timer.stop();
        index--;
        if(index < 0){
            index = filenames.size()-1;
            addPixmap(QPixmap(filenames.at(index)));

        }else{
            //
            addPixmap(QPixmap(filenames.at(index)));
        }
    }
}

void ShowPic::on_pushButton_2_clicked()  //next
{
    if(!filenames.isEmpty()){
        m_timer.stop();
        index++;
        if(index != filenames.size()){
            addPixmap(QPixmap(filenames.at(index)));
        }else{
            //
            index = 0;
            addPixmap(QPixmap(filenames.at(index)));
        }
    }    
}

Upvotes: 1

Views: 640

Answers (1)

Dmitry Sazonov
Dmitry Sazonov

Reputation: 8994

Just connect it with your timers:

enum Direction
{
    Forward = 1,
    Backward = -1,
};

class ImageProvider
{
public:
    ImageProvider( const QStringList& data )
        : _data( data )
    { Q_ASSERT( !data.IsEmpty(); ) }

    void Next()
    {
        _current += _direction;
        Loop();
    }

    void Prev()
    {
        _current -= _direction;
        Loop();
    }

    const QString& Current() const
    {
        return _data[_current];
    }

    void SetDirection( Direction direction )
    {
        _direction = direction;
    }

    void Reverse()
    {
        SetDirection( _direction != Forward ? Forward : Backward );
    }

private:
    void Loop()
    {
        if ( _current < 0 )
            _current = _data.size() - 1;
        if ( _current >= _data.size() )
            _current = 0;
    }

private:
    Direction _direction = Forward;
    int _current = 0;
    const QStringList& _data;
};

Upvotes: 1

Related Questions