[SOLVED] changing image using keypress
-
wrote on 30 Sept 2014, 10:49 last edited by
@void Dialog::keyPressEvent(QKeyEvent *e)
{static int key_press; if(e->key() == Qt::Key_F2) { key_press++; } if(key_press == 1) { QMovie *movie = new QMovie("/home/dev6/Desktop/animation/spectrum.gif"); QLabel *processLabel = new QLabel(this); ui->processLabel->setMovie(movie); movie->start(); ui->label1->setText(QString::fromUtf8("chennai சென்னை")); ui->label1->show(); } if(key_press == 2) { QMovie *movie = new QMovie("/home/dev6/Desktop/animation/1.gif"); QLabel *processLabel = new QLabel(this); ui->processLabel->setMovie(movie); movie->start(); ui->label1->setText(QString::fromUtf8("சென்னை Namakkal")); ui->label1->show(); } if(key_press == 3) { QMovie *movie = new QMovie("/home/dev6/Desktop/animation/2.gif"); QLabel *processLabel = new QLabel(this); ui->processLabel->setMovie(movie); movie->start(); ui->label1->setText(QString::fromUtf8("coimbatore சென்னை")); ui->label1->show(); }
}@
hi..
this is not a right way.Because many of same instructions are repeating.you can optimize your code .You must create a method with parameter QString path
e'g;
@
void ShowPicture(QString path)
{
QMovie *movie = new QMovie(path);
QLabel *processLabel = new QLabel(this);
ui->processLabel->setMovie(movie);
movie->start();ui->label1->setText(QString::fromUtf8("chennai சென்னை")); ui->label1->show();
}
@
hope it helps -
wrote on 30 Sept 2014, 11:35 last edited by
[quote author="Francknos" date="1412070831"]construct a QVector<QMovie> _movies;
and add it to constructor then when you press F2:cpt++
ui->processLabel->setMovie(_movies[cpt]);
_movies[cpt]->start();
[/quote]okay i will try in this way...
-
wrote on 30 Sept 2014, 11:48 last edited by
[quote author="IamSumit" date="1412074165"]
hi..
this is not a right way.Because many of same instructions are repeating.you can optimize your code .You must create a method with parameter QString path
e'g;
@
void ShowPicture(QString path)
{
QMovie *movie = new QMovie(path);
QLabel *processLabel = new QLabel(this);
ui->processLabel->setMovie(movie);
movie->start();ui->label1->setText(QString::fromUtf8("chennai சென்னை")); ui->label1->show();
}
@
hope it helps[/quote]okay. How to call this function and where to call it.
-
wrote on 30 Sept 2014, 11:55 last edited by
i used like this getting error
/home/dev6/Desktop/animation-build-desktop-Qt_4_8_1_in_PATH__System__Release/../animation/dialog.cpp:53: error: a function-definition is not allowed here before '{' token
/home/dev6/Desktop/animation-build-desktop-Qt_4_8_1_in_PATH__System__Release/../animation/dialog.cpp:69: error: 'ShowPicture' was not declared in this scope
@void Dialog::keyPressEvent(QKeyEvent *e)
{void ShowPicture(QString path) { QMovie *movie = new QMovie(path); QLabel *processLabel = new QLabel(this); ui->processLabel->setMovie(movie); movie->start(); ui->label1->setText(QString::fromUtf8("chennai சென்னை")); ui->label1->show(); } static int key_press = 0; if(e->key() == Qt::Key_F3) { // QString path = "/home/dev6/Desktop/animation/3.gif"; ShowPicture("/home/dev6/Desktop/animation/3.gif"); }@
-
wrote on 30 Sept 2014, 12:19 last edited by
[quote author="sasireka" date="1412078116"]i used like this getting error
/home/dev6/Desktop/animation-build-desktop-Qt_4_8_1_in_PATH__System__Release/../animation/dialog.cpp:53: error: a function-definition is not allowed here before '{' token
/home/dev6/Desktop/animation-build-desktop-Qt_4_8_1_in_PATH__System__Release/../animation/dialog.cpp:69: error: 'ShowPicture' was not declared in this scope
@void Dialog::keyPressEvent(QKeyEvent *e)
{void ShowPicture(QString path) { QMovie *movie = new QMovie(path); QLabel *processLabel = new QLabel(this); ui->processLabel->setMovie(movie); movie->start(); ui->label1->setText(QString::fromUtf8("chennai சென்னை")); ui->label1->show(); } static int key_press = 0; if(e->key() == Qt::Key_F3) { // QString path = "/home/dev6/Desktop/animation/3.gif"; ShowPicture("/home/dev6/Desktop/animation/3.gif"); }@[/quote]
hi..
After looking your code ...i think you are new to programming.are you?
you are defining the method inside another method .this is not the correct
way
just call ShowPicture("/home/dev6/Desktop/animation/2.gif");
then you will not face any error.
hope it helps -
wrote on 1 Oct 2014, 04:23 last edited by
[quote author="IamSumit" date="1412079562"][quote author="sasireka" date="1412078116"]i used like this getting error
/home/dev6/Desktop/animation-build-desktop-Qt_4_8_1_in_PATH__System__Release/../animation/dialog.cpp:53: error: a function-definition is not allowed here before '{' token
/home/dev6/Desktop/animation-build-desktop-Qt_4_8_1_in_PATH__System__Release/../animation/dialog.cpp:69: error: 'ShowPicture' was not declared in this scope
@void Dialog::keyPressEvent(QKeyEvent *e)
{void ShowPicture(QString path) { QMovie *movie = new QMovie(path); QLabel *processLabel = new QLabel(this); ui->processLabel->setMovie(movie); movie->start(); ui->label1->setText(QString::fromUtf8("chennai சென்னை")); ui->label1->show(); } static int key_press = 0; if(e->key() == Qt::Key_F3) { // QString path = "/home/dev6/Desktop/animation/3.gif"; ShowPicture("/home/dev6/Desktop/animation/3.gif"); }@[/quote]
hi..
After looking your code ...i think you are new to programming.are you?
you are defining the method inside another method .this is not the correct
way
just call ShowPicture("/home/dev6/Desktop/animation/2.gif");
then you will not face any error.
hope it helps[/quote]good day. yes i am new to Qt programming. Okay i will do like this. Thanks Sumit. Give me a guide to learn Qt programming and library usage.
-
wrote on 1 Oct 2014, 04:33 last edited by
bq. hi..
After looking your code ...i think you are new to programming.are you?
you are defining the method inside another method .this is not the correct
way
just call ShowPicture("/home/dev6/Desktop/animation/2.gif");
then you will not face any error.
hope it helps[/quote]bq.i did like below. but getting errors.
@void ShowPicture(QString path)
{
QMovie *movie = new QMovie(path);
QLabel *processLabel = new QLabel(this);
ui->processLabel->setMovie(movie);
movie->start();ui->label1->setText(QString::fromUtf8("chennai சென்னை")); ui->label1->show();
}
void Dialog::keyPressEvent(QKeyEvent *e)
{if(e->key() == Qt::Key_F3) { ShowPicture(”/home/dev6/Desktop/animation/2.gif”); }
}@
-
wrote on 1 Oct 2014, 06:21 last edited by
Hi .
Can you show me your full code (including .h)?
and tell me what errors you are getting also? -
wrote on 1 Oct 2014, 08:43 last edited by
[quote author="IamSumit" date="1412144476"]Hi .
Can you show me your full code (including .h)?
and tell me what errors you are getting also?[/quote]
@#include "dialog.h"
#include "ui_dialog.h"
#include <QMovie>
#include <QImage>
#include <QFontDatabase>
#include <QDebug>
#include <QKeyEvent>Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}Dialog::~Dialog()
{
delete ui;
}void ShowPicture(QString path)
{
QMovie *movie = new QMovie(path);
QLabel *processLabel = new QLabel(this);
ui->processLabel->setMovie(movie);
movie->start();ui->label1->setText(QString::fromUtf8("chennai சென்னை")); ui->label1->show();
}
void Dialog::keyPressEvent(QKeyEvent *e)
{if(e->key() == Qt::Key_F3) { ShowPicture(”/home/dev6/Desktop/animation/2.gif”); }
}@
-
wrote on 1 Oct 2014, 09:27 last edited by
ok.
ShowPicture(QString) is the member function/method of Dialog class.
i can't see proper definition of this method.
in line 21.
@
Replace void Dialog::ShowPicture(QString path)
@and in .h file declare this method as member.
in .h file. add this line
@
public:
void ShowPicture(QString path);
@and your oops basics are not clear.So, Please improve your basics first.
Hope it helps.
-
wrote on 1 Oct 2014, 11:38 last edited by
i tried . but still getting error
-
wrote on 1 Oct 2014, 11:47 last edited by
What errors ?
-
wrote on 6 Oct 2014, 04:23 last edited by
Good day.
[quote author="IamSumit" date="1412164041"]What errors ?[/quote]@void Dialog::ShowPicture(QString path)
{
QMovie *movie = new QMovie(path);
QLabel *processLabel = new QLabel(this);
ui->processLabel->setMovie(movie);
movie->start();ui->label1->setText(QString::fromUtf8("chennai சென்னை")); ui->label1->show();
}
void Dialog::keyPressEvent(QKeyEvent *e)
{if(e->key() == Qt::Key_F3) { ShowPicture(/home/dev6/Desktop/animation/2.gif); }
}@
errors:
1. /home/dev6/Desktop/animation-build-desktop-Qt_4_8_1_in_PATH__System__Release/../animation/dialog.cpp:70: error: invalid suffix "gif" on floating constant
2. * /home/dev6/Desktop/animation-build-desktop-Qt_4_8_1_in_PATH__System__Release/../animation/dialog.cpp:-1: In member function 'void Dialog::ShowPicture(QString)':
1<* /home/dev6/Desktop/animation-build-desktop-Qt_4_8_1_in_PATH__System__Release/../animation/dialog.cpp:70: error: expected primary-expression before '/' token2< /home/dev6/Desktop/animation-build-desktop-Qt_4_8_1_in_PATH__System__Release/../animation/dialog.cpp:70: error: 'home' was not declared in this scope
3< /home/dev6/Desktop/animation-build-desktop-Qt_4_8_1_in_PATH__System__Release/../animation/dialog.cpp:70: error: 'home' was not declared in this scope
4< /home/dev6/Desktop/animation-build-desktop-Qt_4_8_1_in_PATH__System__Release/../animation/dialog.cpp:70: error: 'Desktop' was not declared in this scope
5< /home/dev6/Desktop/animation-build-desktop-Qt_4_8_1_in_PATH__System__Release/../animation/dialog.cpp:70: error: 'animation' was not declared in this scope
-
wrote on 6 Oct 2014, 08:44 last edited by
Hi.
You are not passing a string .string always encloses " " quotes.
@
19.ShowPicture(/home/dev6/Desktop/animation/2.gif);
@
Correct it as .
ShowPicture("/home/dev6/Desktop/animation/2.gif"); -
wrote on 6 Oct 2014, 09:19 last edited by
[quote author="IamSumit" date="1412585094"]Hi.
You are not passing a string .string always encloses " " quotes.
@
19.ShowPicture(/home/dev6/Desktop/animation/2.gif);
@
Correct it as .
ShowPicture("/home/dev6/Desktop/animation/2.gif");
[/quote]yes.. really i am so sorry.Thanks alot. great help.
-
wrote on 6 Oct 2014, 09:24 last edited by
Thanks alot IamSumit IamSumit and all.
-
wrote on 6 Oct 2014, 09:29 last edited by
That's ok .
Now please Make your thread title as [SOLVED] so others could notify that solution has been found.:)
-
wrote on 6 Oct 2014, 09:35 last edited by
[quote author="IamSumit" date="1412587771"]That's ok .
Now please Make your thread title as [SOLVED] so others could notify that solution has been found.:)[/quote]
yes.. done IamSumit. :)
Happy Coding :)
20/28