我正在使用QT在Android平台上开发一个图像查看器应用程序。图像查看器具有移动到下一图像并通过单击按钮移动到上一图像的功能。
我正在使用QStringList::const_iterator
来控制移动方法。但是,只有移动到下一个图像的功能才能成功实现,而移动到前一个图像的功能无法工作。代码addPixmap(*m_imageIt);
无法添加到
相对按钮的功能。否则,应用程序将停止。请帮帮我。
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);
}
}
}
我使用整数方式更改了代码。最后,它现在有效。
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)));
}
}
}
答案 0 :(得分:0)
只需将其与您的计时器连接:
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;
};