mr0il
mr0il

Reputation: 71

Changing QLabel setPixmap() through a QPushButton signal

I am writing a simple program that displays an image when a button is pressed. I'm very new to Qt, and I am not having any luck identifying where my problem is occurring.

class ImageSwitcher : public QWidget
{
    Q_OBJECT
  public:
ImageSwitcher();
    QPushButton *leftButton;
    QPushButton *rightButton;
~ImageSwitcher();
  private slots:
void switchImages(QPixmap display);
  private:
    QLabel  *canvas;
    QPixmap *one;
    QPixmap *two;
};

class declaration:

ImageSwitcher::ImageSwitcher (void) {
canvas      = new QLabel;
one         = new QPixmap;
two         = new QPixmap;
leftButton  = new QPushButton("&One");
rightButton = new QPushButton("&Two");


    one->load("one.png");
    two->load("two.png");

//Close the program if the images cannot be loaded.
//Load the images to the QPixmaps.
//Connect the left and right buttons.
QObject::connect(leftButton, SIGNAL(clicked()), canvas, SLOT(switchImages(*one)));
QObject::connect(rightButton, SIGNAL(clicked()), canvas, SLOT(switchImages(*two)));

QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(canvas);
layout->addWidget(leftButton);
layout->addWidget(rightButton);

QWidget window;
window.setLayout(layout);
}

void ImageSwitcher::switchImages(QPixmap display) {
canvas->setPixmap(display);
}

 ImageSwitcher::~ImageSwitcher (void) {
delete canvas;
delete one;
delete two;
delete leftButton;
delete rightButton;
 }

and lastly the main function:

int main (int args, char **argv)
{
QApplication app(args, argv);
ImageSwitcher test;
test.show();
    return app.exec();
}

The first problem I am having is that my layout is setup incorrectly. Secondly, the command line warns that switchImages() is not a SLOT for two. Oddly, it doesn't give me the same warning for one. None of the widgets populate when I run, so, I'm not really sure if the connections are working at all.

Any help is appreciated, thanks.

Upvotes: 0

Views: 1291

Answers (1)

JustMaximumPower
JustMaximumPower

Reputation: 1277

Your usage of signal and slots is wrong. In the connect call you have to give the signature of the signal and the slot not values. Values are transmitted via emit. Basically you should implement slots for each button click and than emit an new signal with an QPixmap argument.

signals:
void changeImage(QPixmap);
...

private slots:
...
void onLeftButton();

in constructor:

...
connect(this, SIGNAL(changeImage(QPixmap)), this, SLOT(switchImages(QPixmap)));
...

in onLeftButton():

emit changeImage(*one)

----- edit -----

Looking at your code again I see the problem. In the constructor:

ImageSwitcher::ImageSwitcher (void) {

...

QWidget window;
window.setLayout(layout);
}

QWidget window; creates an QWidget on stack. That means it will be destroyed before it can be displayed. Since ImageSwitcher inherits for QWidget (ImageSwitcher is a QWidget) you probably meant to write:

ImageSwitcher::ImageSwitcher (void) {

...

setLayout(layout); // same as this->setLayout(layout);
}

Upvotes: 1

Related Questions