PDA

View Full Version : How to declare a function for click button event



prajnaranjan.das
24th December 2010, 05:27
//mainwindow.h


class ButtonLayout : public QWidget
{
Q_OBJECT

public:
ButtonLayout(QWidget *parent = 0);

public slots:
void openImage();

};

class ImageViewer : public QWidget
{
Q_OBJECT

public:
ImageViewer(QWidget *parent = 0);
};


//mainwindow.cpp


ButtonLayout::ButtonLayout(QWidget *parent)
: QWidget(parent)
{
QPushButton *btn1 = new QPushButton("IMAGE");

connect(btn1,SIGNAL(clicked()),this,SLOT(openImage ()));

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(btn1);
setLayout(layout);
}

ImageViewer::ImageViewer(QWidget *parent):QWidget(parent)
{
QLabel *lblImage = new QLabel;
QPixmap pixmap(QPixmap::fromImage(QImage(":/images/Resource/photo.JPG")));
lblImage->setPixmap(pixmap);


QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(lblImage);
setLayout(layout);
}

void ButtonLayout::openImage()
{
ImageViewer *viewer = new ImageViewer;
viewer->show();
}


I have declared two classes for it to show a image when I press the Image button...Can I declare these things in one class...

ChrisW67
24th December 2010, 06:22
How about you edit your post to put [code] tags around the code like Denis Kormalev did for you in whatever forum you cut and paste this from.

The answer to your question is yes, you can do this with only one class. Just construct and show() the QWidget in the openImage() slot.

prajnaranjan.das
24th December 2010, 06:44
thanks for your reply....can you please elaborate....

ChrisW67
24th December 2010, 07:10
Put the code from your ImageViewer constructor into the openImage() slot directly.

prajnaranjan.das
24th December 2010, 08:06
void ButtonLayout::openImage()
{
QWidget *titleWidget = new QWidget;
titleWidget->setObjectName(QString::fromUtf8("IMAGE"));
titleWidget->show();

QLabel *lblImage = new QLabel;
QPixmap pixmap(QPixmap::fromImage(QImage(":/images/Resource/photo.JPG")));
lblImage->setPixmap(pixmap);


QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(titleWidget);
layout->addWidget(lblImage);
setLayout(layout);
}




------------OR-------------


void ButtonLayout::openImage()
{
QLabel *lblImage = new QLabel;
QPixmap pixmap(QPixmap::fromImage(QImage(":/images/Resource/photo.JPG")));
lblImage->setPixmap(pixmap);

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(lblImage);
setLayout(layout);
}



I have tried in this type but it is not showing Image....

Lykurg
24th December 2010, 09:03
The second time, we have [code] tags here. So please edit your post and add them.

Further what is the need to construct a QImage and immediately convert it to a QPixmap. You also want spend some time and get familiar with layouts, how to use them and where you have to apply them. Start with the documentation.

prajnaranjan.das
24th December 2010, 09:23
how should I edit these tags...I already used @ tags but its not working ,what I hav to put before these tags ???

Lykurg
24th December 2010, 09:27
E.g.:


int i = 0;
i++;
if (i == 1)
quit();

BalaQT
24th December 2010, 13:17
class ButtonLayout : public QWidget
{
Q_OBJECT

public:
ButtonLayout(QWidget *parent = 0);
QLabel *lblImage;
//add remaining class members
};
ButtonLayout::ButtonLayout(QWidget *parent)
: QWidget(parent)
{
QPushButton *btn1 = new QPushButton("IMAGE");

connect(btn1,SIGNAL(clicked()),this,SLOT(openImage ()));

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(btn1);
setLayout(layout);

//add like this
lblImage = new QLabel;
lblImage->setPixmap(":/images/Resource/photo.JPG");
}

void ButtonLayout::openImage()
{
lblImage->show();
}

it all depends on ur need. as our Qt experts said, u should start studying QtDoc

Bala