I am using the QT integrated designer in Visual Studio 2010 to design a simple GUI. Here is what I want to do: I want to click a button called Update and use that to change an image that is displayed. I’ve verified that I am able to trigger a signal by pressing an update and used a slot on a text-field to clear. But can’t seem to change the image displayed using the Signal/Slot method ….
Here is the relevant code snippet (GUI.cpp):
void GUI::changeImage()
{
ui.Image->updatesEnabled();
ui.Image->setPixmap(QPixmap("D:\\Documents\ and\ Settings\\Image.jpg"));
ui.Image->update();
}
GUI::GUI(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
connect(ui.updateButton, SIGNAL(clicked()), ui.textField, SLOT (clear()) );
connect(ui.updateButton, SIGNAL(clicked()), ui.Image, SLOT ( changeImage() ) );
}
GUI::~GUI()
{
}
Am I doing something wrong?
EDIT: The code for gui.h
#include <QtGui/QMainWindow>
#include "ui_gui.h"
class GUI : public QMainWindow
{
Q_OBJECT
public:
GUI(QWidget *parent = 0, Qt::WFlags flags = 0);
~GUI();
private:
Ui::GUIClass ui;
void changeImage() ;
};
The slot belongs to the
GUIclass instance, not toui.Image, so you should usethisas the target in theconnectcall.The slots have to be declared under a “slots” section: