Jon Heron
11th October 2010, 03:16
Hello all,
I am a beginner learning Qt C++.
I am making an application for Symbian that will teach the user how to tie knots. I am intending to iterate through a list of images and text strings to display them to the user via 2 QLabels. I would like to give the user an option to "play" or iterate automatically with an adjustable delay.
The problem is whenever I stop play and then start again my counter (in changeImage() )gets screwed up and the the order of the images gets all messed up. :(
PushButton_3 is the play pause button that calls the timer which calls the change image routine.
I cant figure out where the counter is getting screwed up with this simple code... I am sure this is some basic mistake I am making like usual... :o
Header for the knot view dialog;
#ifndef KNOTVIEWDLG_H
#define KNOTVIEWDLG_H
#include <QDialog>
#include "whatknot.h"
#include <QObject>
#include <QTimer>
namespace Ui {
class knotViewDlg;
}
class knotViewDlg : public QDialog
{
Q_OBJECT
public:
explicit knotViewDlg(QWidget *parent = 0);
~knotViewDlg();
void keyPressEvent(QKeyEvent *);
//current image name vari
QString cntKnot;
//Current list of image names
QStringList strLst;
int counter; // image list counter
int delay; //image delay
bool play; // is playing?
// Timer
QTimer* timer;
private:
Ui::knotViewDlg *ui;
public slots:
void getIndex(QString knot);
void timing();
void changeImage();
private slots:
void on_pushButton_3_clicked();
};
#endif // KNOTVIEWDLG_H
And here is the cpp;
#include "knotviewdlg.h"
#include "ui_knotviewdlg.h"
#include "ui_whatknot.h"
#include "whatknot.h"
#include <QStringList>
#include <QDir>
#include <QDebug>
#include <QKeyEvent>
knotViewDlg::knotViewDlg(QWidget *parent) :
QDialog(parent),
ui(new Ui::knotViewDlg)
{
ui->setupUi(this);
//init timer
timer = new QTimer(this);
play = false;
delay = 1000; //image play delay default 1s
QStringList result;
//setup d with the path to the resource images
QDir d(":/knotImages/images");
//populate result with all image names
result = d.entryList();
//search result for key word
QString str1 = cntKnot; //set str1 with selected knot name
QString str;
strLst = result.filter(str1);
//set first image before calling timer
str = strLst.at(0);
ui->label->setPixmap(QPixmap(QString::fromUtf8(
":/knotImages/images/").append(str)));
}
knotViewDlg::~knotViewDlg()
{
delete ui;
}
void knotViewDlg::getIndex(QString knot)
{
const QString cntKnot = knot;
}
void knotViewDlg::changeImage()
{
QString str = strLst.at(counter);
ui->label->setPixmap(QPixmap(QString::fromUtf8(
":/knotImages/images/").append(str)));
counter++;
if(counter >= (strLst.count()))
{
counter = 0;
}
}
void knotViewDlg::timing()
{
//default delay between images 1s
timer->setInterval(delay);
connect(timer, SIGNAL(timeout()),SLOT(changeImage()));
timer->start();
}
void knotViewDlg::on_pushButton_3_clicked()
{
if(play == false)
{
play = true;
timing();
}
else
{
timer->stop();
play = false;
}
}
Any and all advice is greatly appreciated!
Cheers,
Jon
I am a beginner learning Qt C++.
I am making an application for Symbian that will teach the user how to tie knots. I am intending to iterate through a list of images and text strings to display them to the user via 2 QLabels. I would like to give the user an option to "play" or iterate automatically with an adjustable delay.
The problem is whenever I stop play and then start again my counter (in changeImage() )gets screwed up and the the order of the images gets all messed up. :(
PushButton_3 is the play pause button that calls the timer which calls the change image routine.
I cant figure out where the counter is getting screwed up with this simple code... I am sure this is some basic mistake I am making like usual... :o
Header for the knot view dialog;
#ifndef KNOTVIEWDLG_H
#define KNOTVIEWDLG_H
#include <QDialog>
#include "whatknot.h"
#include <QObject>
#include <QTimer>
namespace Ui {
class knotViewDlg;
}
class knotViewDlg : public QDialog
{
Q_OBJECT
public:
explicit knotViewDlg(QWidget *parent = 0);
~knotViewDlg();
void keyPressEvent(QKeyEvent *);
//current image name vari
QString cntKnot;
//Current list of image names
QStringList strLst;
int counter; // image list counter
int delay; //image delay
bool play; // is playing?
// Timer
QTimer* timer;
private:
Ui::knotViewDlg *ui;
public slots:
void getIndex(QString knot);
void timing();
void changeImage();
private slots:
void on_pushButton_3_clicked();
};
#endif // KNOTVIEWDLG_H
And here is the cpp;
#include "knotviewdlg.h"
#include "ui_knotviewdlg.h"
#include "ui_whatknot.h"
#include "whatknot.h"
#include <QStringList>
#include <QDir>
#include <QDebug>
#include <QKeyEvent>
knotViewDlg::knotViewDlg(QWidget *parent) :
QDialog(parent),
ui(new Ui::knotViewDlg)
{
ui->setupUi(this);
//init timer
timer = new QTimer(this);
play = false;
delay = 1000; //image play delay default 1s
QStringList result;
//setup d with the path to the resource images
QDir d(":/knotImages/images");
//populate result with all image names
result = d.entryList();
//search result for key word
QString str1 = cntKnot; //set str1 with selected knot name
QString str;
strLst = result.filter(str1);
//set first image before calling timer
str = strLst.at(0);
ui->label->setPixmap(QPixmap(QString::fromUtf8(
":/knotImages/images/").append(str)));
}
knotViewDlg::~knotViewDlg()
{
delete ui;
}
void knotViewDlg::getIndex(QString knot)
{
const QString cntKnot = knot;
}
void knotViewDlg::changeImage()
{
QString str = strLst.at(counter);
ui->label->setPixmap(QPixmap(QString::fromUtf8(
":/knotImages/images/").append(str)));
counter++;
if(counter >= (strLst.count()))
{
counter = 0;
}
}
void knotViewDlg::timing()
{
//default delay between images 1s
timer->setInterval(delay);
connect(timer, SIGNAL(timeout()),SLOT(changeImage()));
timer->start();
}
void knotViewDlg::on_pushButton_3_clicked()
{
if(play == false)
{
play = true;
timing();
}
else
{
timer->stop();
play = false;
}
}
Any and all advice is greatly appreciated!
Cheers,
Jon