The "for" loop you have iterates over all images thus you see the last eight. You should have a single iteration modifying eight images starting from the one after the last displayed one.
#include <QtGui>
public:
for(int i=0;i<9;i++){
l->addWidget(lab, i / 3, i % 3);
lab->setFixedSize(200,200);
lab->setAlignment(Qt::AlignCenter);
labels << lab;
}
imagePaths
= QDir(dirName
).
entryList(QStringList() <<
"*.jpg" <<
"*.png" <<
"*.bmp");
m_dirName = dirName;
m_nextImage = 0;
}
void start(int ms){
m_timer = startTimer(ms);
m_nextImage = 0;
updateLabels();
}
void stop(){
killTimer(m_timer);
}
protected:
updateLabels();
}
void updateLabels(){
for(int i=0;i<9;i++){
if(m_nextImage>=imagePaths.size()){
} else {
labels
[i
]->setPixmap
(QPixmap(m_dirName
+QDir::separator()+imagePaths.
at(m_nextImage
)).
scaled(QSize(200,
200), Qt
::KeepAspectRatio, Qt
::SmoothTransformation));
}
m_nextImage++;
}
if(m_nextImage>=imagePaths.size()) m_nextImage = 0;
}
private:
QList<QLabel*> labels;
int m_timer;
int m_nextImage;
};
int main(int argc, char **argv){
Gallery gal(argv[1]);
gal.show();
gal.start(5000);
return app.exec();
}
#include <QtGui>
class Gallery : public QWidget {
public:
Gallery(const QString &dirName) {
QGridLayout *l = new QGridLayout(this);
for(int i=0;i<9;i++){
QLabel *lab = new QLabel;
l->addWidget(lab, i / 3, i % 3);
lab->setFixedSize(200,200);
lab->setAlignment(Qt::AlignCenter);
labels << lab;
}
imagePaths = QDir(dirName).entryList(QStringList() << "*.jpg" << "*.png" << "*.bmp");
m_dirName = dirName;
m_nextImage = 0;
}
void start(int ms){
m_timer = startTimer(ms);
m_nextImage = 0;
updateLabels();
}
void stop(){
killTimer(m_timer);
}
protected:
void timerEvent(QTimerEvent *tev){
updateLabels();
}
void updateLabels(){
for(int i=0;i<9;i++){
if(m_nextImage>=imagePaths.size()){
labels[i]->setPixmap(QPixmap());
} else {
labels[i]->setPixmap(QPixmap(m_dirName+QDir::separator()+imagePaths.at(m_nextImage)).scaled(QSize(200,200), Qt::KeepAspectRatio, Qt::SmoothTransformation));
}
m_nextImage++;
}
if(m_nextImage>=imagePaths.size()) m_nextImage = 0;
}
private:
QList<QLabel*> labels;
int m_timer;
int m_nextImage;
QString m_dirName;
QStringList imagePaths;
};
int main(int argc, char **argv){
QApplication app(argc, argv);
Gallery gal(argv[1]);
gal.show();
gal.start(5000);
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks