How to declare a function for click button event



  • //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...

    [edit: code highlighted / Denis Kormalev]



  • Please use the '@' tag for code formatting



  • Yes you should rely put some effort in commenting your code properly so that others can understand it.



  • I've highlighted code, but please do it by yourself next time.



  • ok...But please tell me some answer for my Question



  • For highlighting? Use @ symbol before and after code.



  • [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.



  • thanks for your reply....If possible to declare it in a single class,please tell me about it....



  • 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.



  • 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



  • 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.



  • So how should I declare this....



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.