Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] changing image using keypress
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] changing image using keypress

Scheduled Pinned Locked Moved General and Desktop
28 Posts 3 Posters 7.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sasireka
    wrote on last edited by
    #1

    hi all,
    Good day. I want to change the image using keypress. If i am pressing F2, in screen i have to show first image. Again pressing F2 second time in screen have to show next image. please give me some idea to do it.

    Thanks & Regards
    Sasi

    .................................
    Thanks & Regards

    Sasi

    .................................
    Go Green

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Francknos
      wrote on last edited by
      #2

      Hi try this:

      @
      void MyClass::keyPressEvent(QKeyEvent *e)
      {
      switch ( e->key() )
      {
      case Qt::Key_F2 :
      MAKES YOUR CHANGE
      break;
      }
      }
      @

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sasireka
        wrote on last edited by
        #3

        [quote author="Francknos" date="1412059137"]Hi try this:

        @
        void MyClass::keyPressEvent(QKeyEvent *e)
        {
        switch ( e->key() )
        {
        case Qt::Key_F2 :
        MAKES YOUR CHANGE
        break;
        }
        }
        @[/quote]

        yes i added but not showing anything. and my code is below

        dialog.h
        @#ifndef DIALOG_H
        #define DIALOG_H

        #include <QDialog>
        #include <QMovie>

        namespace Ui {
        class Dialog;
        }

        class Dialog : public QDialog
        {
        Q_OBJECT

        public:
        explicit Dialog(QWidget *parent = 0);
        ~Dialog();

        protected:
        void keyPressEvent(QKeyEvent *e);

        private:
        Ui::Dialog *ui;

        };

        #endif // DIALOG_H@

        dialog.cpp

        @#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 Dialog::keyPressEvent(QKeyEvent *e)
        {
        switch ( e->key() )
        {
        case Qt::Key_F2 :
        QMovie *movie = new QMovie("/home/dev6/Desktop/animation/spectrum.gif");
        QLabel *processLabel = new QLabel(this);
        processLabel->setMovie(movie);
        movie->start();
        break;
        }
        }@

        .................................
        Thanks & Regards

        Sasi

        .................................
        Go Green

        1 Reply Last reply
        0
        • F Offline
          F Offline
          Francknos
          wrote on last edited by
          #4

          Try to have your QMovie in private membre of your class and construt it in constructor.

          but I think the function keyPressEvent works. The proble is in your QMovie.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sasireka
            wrote on last edited by
            #5

            [quote author="Francknos" date="1412060594"]Try to have your QMovie in private membre of your class and construt it in constructor.

            but I think the function keyPressEvent works. The proble is in your QMovie. [/quote]

            yes exactly. i changed like this. now it is working fine. Thanks alot.

            @void Dialog::keyPressEvent(QKeyEvent *e)
            {
            switch ( e->key() )
            {
            case Qt::Key_F2 :
            QMovie *movie = new QMovie("/home/dev6/Desktop/animation/spectrum.gif");
            QLabel *processLabel = new QLabel(this);
            ui->processLabel->setMovie(movie);
            movie->start();
            break;
            }
            }@

            .................................
            Thanks & Regards

            Sasi

            .................................
            Go Green

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sasireka
              wrote on last edited by
              #6

              but if i am pressing the same key again have to show next image is it possible..

              .................................
              Thanks & Regards

              Sasi

              .................................
              Go Green

              1 Reply Last reply
              0
              • S Offline
                S Offline
                sasireka
                wrote on last edited by
                #7

                This is working fine for showing the next next picture by pressing the same key.

                Thanks alot to Francknos and all for your great help.

                @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();
                    }
                

                }@

                .................................
                Thanks & Regards

                Sasi

                .................................
                Go Green

                1 Reply Last reply
                0
                • IamSumitI Offline
                  IamSumitI Offline
                  IamSumit
                  wrote on last edited by
                  #8

                  Hi.
                  Yeah it is possible.

                  Be Cute

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    sasireka
                    wrote on last edited by
                    #9

                    [quote author="IamSumit" date="1412069646"]Hi.
                    Yeah it is possible.

                    [/quote]

                    yes i done . thanks

                    .................................
                    Thanks & Regards

                    Sasi

                    .................................
                    Go Green

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      Francknos
                      wrote on last edited by
                      #10

                      construct a QVector<QMovie> _movies;
                      and add it to constructor then when you press F2:

                      cpt++
                      ui->processLabel->setMovie(_movies[cpt]);
                      _movies[cpt]->start();

                      1 Reply Last reply
                      0
                      • IamSumitI Offline
                        IamSumitI Offline
                        IamSumit
                        wrote on last edited by
                        #11

                        @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

                        Be Cute

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          sasireka
                          wrote on last edited by
                          #12

                          [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...

                          .................................
                          Thanks & Regards

                          Sasi

                          .................................
                          Go Green

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            sasireka
                            wrote on last edited by
                            #13

                            [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.

                            .................................
                            Thanks & Regards

                            Sasi

                            .................................
                            Go Green

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              sasireka
                              wrote on last edited by
                              #14

                              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");
                                  
                              }@
                              

                              .................................
                              Thanks & Regards

                              Sasi

                              .................................
                              Go Green

                              1 Reply Last reply
                              0
                              • IamSumitI Offline
                                IamSumitI Offline
                                IamSumit
                                wrote on last edited by
                                #15

                                [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

                                Be Cute

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  sasireka
                                  wrote on last edited by
                                  #16

                                  [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.

                                  .................................
                                  Thanks & Regards

                                  Sasi

                                  .................................
                                  Go Green

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    sasireka
                                    wrote on last edited by
                                    #17

                                    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”);
                                    }
                                    

                                    }@

                                    .................................
                                    Thanks & Regards

                                    Sasi

                                    .................................
                                    Go Green

                                    1 Reply Last reply
                                    0
                                    • IamSumitI Offline
                                      IamSumitI Offline
                                      IamSumit
                                      wrote on last edited by
                                      #18

                                      Hi .
                                      Can you show me your full code (including .h)?
                                      and tell me what errors you are getting also?

                                      Be Cute

                                      1 Reply Last reply
                                      0
                                      • S Offline
                                        S Offline
                                        sasireka
                                        wrote on last edited by
                                        #19

                                        [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”);
                                        }
                                        

                                        }@

                                        .................................
                                        Thanks & Regards

                                        Sasi

                                        .................................
                                        Go Green

                                        1 Reply Last reply
                                        0
                                        • IamSumitI Offline
                                          IamSumitI Offline
                                          IamSumit
                                          wrote on last edited by
                                          #20

                                          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.

                                          Be Cute

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved