Results 1 to 2 of 2

Thread: Designing a user interface. input/output , changing images

  1. #1
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    2

    Default Designing a user interface. input/output , changing images

    I want to design a program, a sort of game.
    I want to use some pictures and when they get clicked replacing them with another picture. I have done this:
    void MainWindow:n_pushButton_clicked(bool checked)
    {

    if(checked)
    checked = false;
    else
    checked = true;

    }
    The problem that I have is that I want to replace the pictures not only once and back, I want to swap between 4 images. The data when a image is clicked I must send to my main program who does al the calculations if that's legit or not. I also want to update my user interface after my main program has done a few stuff. (input and output)

    How should I do this with code or with the user interface... I'm totally new to Qt

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Designing a user interface. input/output , changing images

    I am not exactly sure what it is you want to do, but if you want the current image to be changed to a new one each time the button is clicked, you can do something like this:

    Qt Code:
    1. // MainWindow.h:
    2.  
    3. class MainWindow : public QMainWindow
    4. {
    5. Q_OBJECT;
    6.  
    7. public:
    8. // usual constructor / destructor stuff
    9.  
    10. protected slots:
    11. void onButtonClicked( bool );
    12.  
    13. protected:
    14. void showImage( int index );
    15.  
    16. private:
    17. QVector< QPixmap * > images;
    18. int currentImageIndex;
    19. };
    20.  
    21. // MainWindow.cpp:
    22.  
    23. MainWindow::MainWindow( QWidget * parent )
    24. : QMainWindow( parent )
    25. {
    26.  
    27. // ... load your images from a file or whatever and put the pixmaps into the vector
    28.  
    29. currentImageIndex = 0;
    30.  
    31. showImage( currentImageIndex );
    32. }
    33.  
    34. MainWindow::onButtonClicked( bool )
    35. {
    36. // increment the image index each time the button is clicked. The bool argument is irrelevant and ignored
    37.  
    38. int nImages = images.size();
    39. currentImageIndex = (currentImageIndex + 1) % nImages;
    40.  
    41. showImage( currentImageIndex );
    42. }
    43.  
    44. // Assumes your main window uses a QLabel named "label"to display the image
    45. MainWindow::showImage( int index )
    46. {
    47. if ( index >= 0 && index < images.size() )
    48. label->setPixmap( *(images[ index ]) );
    49. }
    To copy to clipboard, switch view to plain text mode 

    NB: Not compiled, not tested.

    Please use CODE tags (as I have done) when posting source code.

  3. The following user says thank you to d_stranz for this useful post:

    dieter (19th November 2011)

Similar Threads

  1. Which is the best way to make a fluid user interface in QT?
    By Kevin Hoang in forum Qt Programming
    Replies: 5
    Last Post: 9th March 2010, 12:37
  2. Using SVG as user interface for fast prototyping
    By benlau in forum Qt Programming
    Replies: 1
    Last Post: 8th January 2010, 12:01
  3. Customizing UIC (User Interface Compiler)
    By Thalionath in forum Qt Programming
    Replies: 1
    Last Post: 13th October 2009, 17:53
  4. Regarding Database Display through user interface
    By Tavit in forum Qt Programming
    Replies: 1
    Last Post: 1st April 2008, 10:32
  5. User Interface with QTableView
    By Brandybuck in forum Qt Programming
    Replies: 1
    Last Post: 22nd March 2006, 23:24

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.