Results 1 to 9 of 9

Thread: How to declare a function for click button event

  1. #1
    Join Date
    Dec 2010
    Posts
    34
    Qt products
    Qt4
    Platforms
    Windows

    Post How to declare a function for click button event

    //mainwindow.h
    Qt Code:
    1. class ButtonLayout : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. ButtonLayout(QWidget *parent = 0);
    7.  
    8. public slots:
    9. void openImage();
    10.  
    11. };
    12.  
    13. class ImageViewer : public QWidget
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. ImageViewer(QWidget *parent = 0);
    19. };
    To copy to clipboard, switch view to plain text mode 
    //mainwindow.cpp
    Qt Code:
    1. ButtonLayout::ButtonLayout(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. QPushButton *btn1 = new QPushButton("IMAGE");
    5.  
    6. connect(btn1,SIGNAL(clicked()),this,SLOT(openImage()));
    7.  
    8. QVBoxLayout *layout = new QVBoxLayout;
    9. layout->addWidget(btn1);
    10. setLayout(layout);
    11. }
    12.  
    13. ImageViewer::ImageViewer(QWidget *parent):QWidget(parent)
    14. {
    15. QLabel *lblImage = new QLabel;
    16. QPixmap pixmap(QPixmap::fromImage(QImage(":/images/Resource/photo.JPG")));
    17. lblImage->setPixmap(pixmap);
    18.  
    19.  
    20. QVBoxLayout *layout = new QVBoxLayout;
    21. layout->addWidget(lblImage);
    22. setLayout(layout);
    23. }
    24.  
    25. void ButtonLayout::openImage()
    26. {
    27. ImageViewer *viewer = new ImageViewer;
    28. viewer->show();
    29. }
    To copy to clipboard, switch view to plain text mode 

    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...
    Last edited by prajnaranjan.das; 24th December 2010 at 09:36.
    Regards:

    Prajnaranjan Das
    e mail: prajnaranjan.das@gmail.com

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to declare a function for click button event

    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.

  3. #3
    Join Date
    Dec 2010
    Posts
    34
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to declare a function for click button event

    thanks for your reply....can you please elaborate....
    Regards:

    Prajnaranjan Das
    e mail: prajnaranjan.das@gmail.com

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to declare a function for click button event

    Put the code from your ImageViewer constructor into the openImage() slot directly.

  5. #5
    Join Date
    Dec 2010
    Posts
    34
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to declare a function for click button event

    Qt Code:
    1. void ButtonLayout::openImage()
    2. {
    3. QWidget *titleWidget = new QWidget;
    4. titleWidget->setObjectName(QString::fromUtf8("IMAGE"));
    5. titleWidget->show();
    6.  
    7. QLabel *lblImage = new QLabel;
    8. QPixmap pixmap(QPixmap::fromImage(QImage(":/images/Resource/photo.JPG")));
    9. lblImage->setPixmap(pixmap);
    10.  
    11.  
    12. QVBoxLayout *layout = new QVBoxLayout;
    13. layout->addWidget(titleWidget);
    14. layout->addWidget(lblImage);
    15. setLayout(layout);
    16. }
    To copy to clipboard, switch view to plain text mode 


    ------------OR-------------
    Qt Code:
    1. void ButtonLayout::openImage()
    2. {
    3. QLabel *lblImage = new QLabel;
    4. QPixmap pixmap(QPixmap::fromImage(QImage(":/images/Resource/photo.JPG")));
    5. lblImage->setPixmap(pixmap);
    6.  
    7. QVBoxLayout *layout = new QVBoxLayout;
    8. layout->addWidget(lblImage);
    9. setLayout(layout);
    10. }
    To copy to clipboard, switch view to plain text mode 


    I have tried in this type but it is not showing Image....
    Last edited by prajnaranjan.das; 24th December 2010 at 09:35.
    Regards:

    Prajnaranjan Das
    e mail: prajnaranjan.das@gmail.com

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to declare a function for click button event

    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.

  7. #7
    Join Date
    Dec 2010
    Posts
    34
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to declare a function for click button event

    how should I edit these tags...I already used @ tags but its not working ,what I hav to put before these tags ???
    Regards:

    Prajnaranjan Das
    e mail: prajnaranjan.das@gmail.com

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to declare a function for click button event

    E.g.:

    [code]int i = 0;
    i++;
    if (i == 1)
    quit();[/code]

  9. #9
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to declare a function for click button event

    Qt Code:
    1. class ButtonLayout : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. ButtonLayout(QWidget *parent = 0);
    7. QLabel *lblImage;
    8. //add remaining class members
    9. };
    10. ButtonLayout::ButtonLayout(QWidget *parent)
    11. : QWidget(parent)
    12. {
    13. QPushButton *btn1 = new QPushButton("IMAGE");
    14.  
    15. connect(btn1,SIGNAL(clicked()),this,SLOT(openImage()));
    16.  
    17. QVBoxLayout *layout = new QVBoxLayout;
    18. layout->addWidget(btn1);
    19. setLayout(layout);
    20.  
    21. //add like this
    22. lblImage = new QLabel;
    23. lblImage->setPixmap(":/images/Resource/photo.JPG");
    24. }
    25.  
    26. void ButtonLayout::openImage()
    27. {
    28. lblImage->show();
    29. }
    To copy to clipboard, switch view to plain text mode 

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

    Bala

Similar Threads

  1. How to declare SLOT as a parameter to member function?
    By QPlace in forum Qt Programming
    Replies: 2
    Last Post: 17th July 2018, 01:41
  2. Replies: 4
    Last Post: 27th November 2009, 14:00
  3. Replies: 4
    Last Post: 21st March 2009, 14:51
  4. Replies: 2
    Last Post: 12th January 2009, 00:24
  5. Why do i need to declare a function as a slot?
    By cbarmpar in forum Qt Programming
    Replies: 2
    Last Post: 31st August 2008, 21:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.