How to declare a function for click button event
-
wrote on 23 Dec 2010, 12:43 last edited by
//mainwindow.h
@
class ButtonLayout : public QWidget
{
Q_OBJECTpublic:
ButtonLayout(QWidget *parent = 0);public slots:
void openImage();};
class ImageViewer : public QWidget
{
Q_OBJECTpublic:
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...
[edit: code highlighted / Denis Kormalev]
-
wrote on 23 Dec 2010, 12:45 last edited by
Please use the '@' tag for code formatting
-
wrote on 23 Dec 2010, 12:48 last edited by
Yes you should rely put some effort in commenting your code properly so that others can understand it.
-
wrote on 23 Dec 2010, 12:54 last edited by
I've highlighted code, but please do it by yourself next time.
-
wrote on 23 Dec 2010, 13:00 last edited by
ok...But please tell me some answer for my Question
-
wrote on 23 Dec 2010, 13:02 last edited by
For highlighting? Use @ symbol before and after code.
-
wrote on 23 Dec 2010, 21:19 last edited by
[quote author="Prajnaranjan Das" date="1293108212"]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...[/quote]
Surely it's doable, but your code is simple and easy to understand. I do not see any need to change it.
-
wrote on 24 Dec 2010, 04:16 last edited by
thanks for your reply....If possible to declare it in a single class,please tell me about it....
-
wrote on 24 Dec 2010, 05:51 last edited by
But why do you want to have it in a single class? One of OOP principle is separation of concerns which in usually provides more maintainable code.
-
wrote on 24 Dec 2010, 08:56 last edited by
For a click button event I want It to show two buttons such that if I clicks a button it should show another two buttons or QLabel Image ....So for every Button click event If I want to declare different things I have to create a new class for it to pass that class name inside that function as I have created above ....So is it possible to declare these in a single class
-
wrote on 24 Dec 2010, 09:50 last edited by
Maybe I misunderstand you but creating new instances from a class does not require declaring new and new classes. Other important principle of OOP is generalization.
-
wrote on 24 Dec 2010, 10:50 last edited by
So how should I declare this....
-
wrote on 24 Dec 2010, 21:49 last edited by
If you want to add buttons you can do this in a method in your main class:
@
void addButto()
{
QPushButton *button = new QPushButton(this);
button->setText(tr("fancy button"));
// add the button to a layout here
connect(button, SIGNAL(clicked()), this, SLOT(fancyButtonClicked()));
}
@Although it's hard to understand what you want to do in your user interface. It's makes a big difference if you show a button (that a user can click on) or if you show a label (that displays a bunch of text or image). And if you click the first button over and over, you get a big bunch of new buttons and labels... looks a bit weird to me.
Also, the method that creates the new buttons or labels belongs into that class, that is the "controller" of your user interface or the respective part of it.
5/13