Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to declare a function for click button event

How to declare a function for click button event

Scheduled Pinned Locked Moved Mobile and Embedded
13 Posts 5 Posters 18.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    prajnaranjan.das
    wrote on last edited by
    #1

    //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]

    Prajnaranjan Das

    e mail: prajnaranjan.das@gmail.com

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vcsala
      wrote on last edited by
      #2

      Please use the '@' tag for code formatting

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kamalakshantv
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DenisKormalev
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • P Offline
            P Offline
            prajnaranjan.das
            wrote on last edited by
            #5

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

            Prajnaranjan Das

            e mail: prajnaranjan.das@gmail.com

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DenisKormalev
              wrote on last edited by
              #6

              For highlighting? Use @ symbol before and after code.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

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

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  prajnaranjan.das
                  wrote on last edited by
                  #8

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

                  Prajnaranjan Das

                  e mail: prajnaranjan.das@gmail.com

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    vcsala
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      prajnaranjan.das
                      wrote on last edited by
                      #10

                      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

                      Prajnaranjan Das

                      e mail: prajnaranjan.das@gmail.com

                      1 Reply Last reply
                      0
                      • V Offline
                        V Offline
                        vcsala
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        • P Offline
                          P Offline
                          prajnaranjan.das
                          wrote on last edited by
                          #12

                          So how should I declare this....

                          Prajnaranjan Das

                          e mail: prajnaranjan.das@gmail.com

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            goetz
                            wrote on last edited by
                            #13

                            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.

                            http://www.catb.org/~esr/faqs/smart-questions.html

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved