Create slideshow in qt creator
-
f1_image.cpp
#include "f1_image.h" #include "ui_f1_image.h" #include<QGraphicsView> #include<QStringList> #include<QList> #include<QDir> #include<iostream> #include<QWidget> #include <QKeyEvent> #include <QStringList> class f1_image_private { public: f1_image_private(); int CurrentSlide; }; F1_Image::F1_Image(QDialog *parent) : QDialog(parent), ui(new Ui::F1_Image) { ui->setupUi(this); filenames.append(":/f1/F1_IMG/city.jpg"); filenames.append(":/f1/F1_IMG/cycle.jpg"); filenames.append(":/f1/F1_IMG/design.jpg"); filenames.append(":/f1/F1_IMG/nature.jpg"); filenames.append(":/f1/F1_IMG/tree.jpg"); } F1_Image::~F1_Image() { delete ui; } void F1_Image::paintEvent(QPaintEvent *event) { QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing, false); if (filenames.size() > 0) { ui->image->setPixmap(filenames.at(i)); } } void F1_Image::keyPressEvent(QKeyEvent* event) { if(event->key()==Qt::Key_Left) { i++ ; } else if(event->key()==Qt::Key_Right) { i--; } }
f1_image.h
#ifndef F1_IMAGE_H #define F1_IMAGE_H #include <QDialog> #include <QStringList> #include <QGraphicsScene> namespace Ui { class F1_Image; } class F1_image_private; class F1_Image : public QDialog { Q_OBJECT public: explicit F1_Image(QDialog *parent = nullptr); ~F1_Image(); void addImage(QStringList filenames); QStringList filenames; int i=0; private: Ui::F1_Image *ui; Ui::F1_Image *d; QPixmap image; QImage *imageObject; QGraphicsScene *scene; signals: void inputReceived(); protected: void keyPressEvent(QKeyEvent *event); void paintEvent(QPaintEvent *event); // void showEvent(QShowEvent *event ); };
@SGaist @mrjj this is my code. i am not sure where excatly i am doing wrong. can you please help me??
-
Hi
It does not look very wrong.
At least not something app should crash from.
(not unless you press right from start and i becomes -1)You don't need the
void F1_Image::paintEvent(QPaintEvent *event)
as the QLabel can paint itself and since you are
not painting anything else it seems wasted.
However, in sample, i used to print i on same form.You can call setPixmap in keypressEvent
void F1_Image::keyPressEvent(QKeyEvent* event) { if(event->key()==Qt::Key_Left) { i++ ; } else if(event->key()==Qt::Key_Right) { i--; } int maxSize = filenames.size() - 1; // checks to make it round trip / avoid crashing if (i > maxSize) { i = 0; } if (i < 0 ) { i = maxSize; } if (i <= maxSize && i >= 0) { ui->label->setPixmap(filenames.at(i)); } }
also in .h
Ui::F1_Image *d;
That looks a little odd but just a pointer so should not do anything.Make sure that you check i against being negative also.
If user press right as first key, the i will be -1 and also crash the list.here is a working sample based on your code.
https://www.dropbox.com/s/ykfm3hqr1x3clby/slideshow_test.zip?dl=0 -
Well your code works fine in the sample
so i cannot guess why it does not for you.Use the debugger and check what happens when you press a key
and check the the value of i.Also , set an image to the QLabel in CTOR so it has something to show from start.
Make sure you check that i is valid.
The sample does the above if you have doubts.
-
What types of issues ?
It is extremely hard to help when you do not describe
what happens,
what is wrong,
What do you see on screen after key pressed.make sure you have valid index (i)
int maxSize = filenames.size() - 1; // checks to make it round trip / avoid crashing if (i > maxSize) { i = 0; } if (i < 0 ) { i = maxSize; } if (i <= maxSize && i >= 0) { ui->label->setPixmap(filenames.at(i)); }
-
@mrjj When I press key it change I value but it doesn't show picture that available in resource file. Problem is lable don't show picture from list. Can you just check what I write to add all links in filenames is right or not?
-
Hi, find the image file in the project tree to the left side
then right-click it and see the correct path there. then check
what you add to list. -
@RAJ-Dalsaniya
Super to hear \o/
Please mark as solved using the Topic Tool button at first post :)