How about I just show you what worked for me?
First, here is the header file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QWidget>
{
Q_OBJECT
public:
~MainWindow();
public slots:
void updatePixmap();
private:
int numUpdates;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QWidget>
class QLabel;
class QPushButton;
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void updatePixmap();
private:
QPixmap pmap1, pmap2;
QLabel *label;
QPushButton *button;
int numUpdates;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
Now, here is the .cpp file:
#include "mainwindow.h"
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
MainWindow
::MainWindow(QWidget *parent
){
pmap1
= QPixmap("C:\\path\\to\\image1.jpg");
pmap2
= QPixmap("C:\\path\\to\\image2.jpg");
label->setPixmap(pmap1);
connect(button, SIGNAL(clicked()), this, SLOT(updatePixmap()));
layout->addWidget(label);
layout->addWidget(button);
layout
->setSizeConstraint
(QLayout::SetFixedSize);
setLayout(layout);
}
void MainWindow::updatePixmap()
{
++numUpdates % 2 ? label->setPixmap(pmap2) : label->setPixmap(pmap1);
}
MainWindow::~MainWindow()
{
}
#include "mainwindow.h"
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent), numUpdates(0)
{
pmap1 = QPixmap("C:\\path\\to\\image1.jpg");
pmap2 = QPixmap("C:\\path\\to\\image2.jpg");
label = new QLabel();
label->setPixmap(pmap1);
button = new QPushButton("Update!");
connect(button, SIGNAL(clicked()), this, SLOT(updatePixmap()));
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(label);
layout->addWidget(button);
layout->setSizeConstraint(QLayout::SetFixedSize);
setLayout(layout);
}
void MainWindow::updatePixmap()
{
++numUpdates % 2 ? label->setPixmap(pmap2) : label->setPixmap(pmap1);
}
MainWindow::~MainWindow()
{
}
To copy to clipboard, switch view to plain text mode
update: images now toggle back and forth instead of being a one-shot deal
Bookmarks