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:
{
Q_OBJECT;
public:
// usual constructor / destructor stuff
protected slots:
void onButtonClicked( bool );
protected:
void showImage( int index );
private:
int currentImageIndex;
};
// MainWindow.cpp:
MainWindow
::MainWindow( QWidget * 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 ]) );
}
// 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 ]) );
}
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.
Bookmarks