PDA

View Full Version : Designing a user interface. input/output , changing images



dieter
11th November 2011, 10:28
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::on_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

d_stranz
12th November 2011, 02:01
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:



// MainWindow.h:

class MainWindow : public QMainWindow
{
Q_OBJECT;

public:
// usual constructor / destructor stuff

protected slots:
void onButtonClicked( bool );

protected:
void showImage( int index );

private:
QVector< QPixmap * > images;
int currentImageIndex;
};

// MainWindow.cpp:

MainWindow::MainWindow( QWidget * parent )
: QMainWindow( parent )
{

// ... load your images from a file or whatever and put the pixmaps into the vector

currentImageIndex = 0;

showImage( currentImageIndex );
}

MainWindow::onButtonClicked( bool )
{
// increment the image index each time the button is clicked. The bool argument is irrelevant and ignored

int nImages = images.size();
currentImageIndex = (currentImageIndex + 1) % nImages;

showImage( currentImageIndex );
}

// Assumes your main window uses a QLabel named "label"to display the image
MainWindow::showImage( int index )
{
if ( index >= 0 && index < images.size() )
label->setPixmap( *(images[ index ]) );
}


NB: Not compiled, not tested.

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