Results 1 to 4 of 4

Thread: Custom Widget - clicked() signal

  1. #1
    Join Date
    Aug 2006
    Location
    Russia, St. Petersburg <-> Omsk
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Custom Widget - clicked() signal

    What shoud i do to create clicked() signal in my custom widget?

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom Widget - clicked() signal

    You can have a boolean variable which will become true in mousePressEvent. In mouseReleaseEvent you can emit the signal clicked() if the boolean is true and then reset it again for the next click.

  3. #3
    Join Date
    May 2006
    Posts
    32
    Thanks
    1
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom Widget - clicked() signal

    Hello.

    Here is the sample code to do that..

    Qt Code:
    1. class Button : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Button(....);
    7.  
    8. protected:
    9. void mousePressEvent(QMouseEvent*);
    10. void mouseReleaseEvent(QMouseEvent*);
    11. void paintEvent(QPaintEvent*);
    12.  
    13. private:
    14. bool state;
    15.  
    16. signals:
    17. void clicked();
    18. void release();
    19. void pressed();
    20. };
    21.  
    22. Button::Button(............)
    23. {
    24. state = 0;
    25. }
    26.  
    27. void Button::mousePressEvent(....)
    28. {
    29. state = 1;
    30. repaint();
    31. emit pressed();
    32. }
    33.  
    34. void Button::mouseReleaseEvent(....)
    35. {
    36. state = 0;
    37. repaint();
    38. emit released();
    39. emit clicked();
    40. }
    41.  
    42. void Button::paintEvent(....)
    43. {
    44. if(state)
    45. ...............
    46. else
    47. .............;
    48. }
    To copy to clipboard, switch view to plain text mode 
    .

    Based on the state value repaint the button as you need and call the signal with any object like this

    Qt Code:
    1. Button *b = new Button(0);
    2. connect(b, SIGNAL(clicked()), qApp, SLOT(quit()));
    To copy to clipboard, switch view to plain text mode 

    You can try this options here

    Cheers
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Custom Widget - clicked() signal

    Quote Originally Posted by sarode View Post
    Qt Code:
    1. void Button::mouseReleaseEvent(....)
    2. {
    3. state = 0;
    4. repaint();
    5. emit released();
    6. emit clicked();
    7. }
    To copy to clipboard, switch view to plain text mode 
    .

    Based on the state value repaint the button as you need
    It's better to update() than to repaint(), repaint() calls are not merged which may slow down the application a bit (or much, depending on how complex is the painting routine).

Similar Threads

  1. Replies: 1
    Last Post: 5th November 2006, 23:50
  2. Problem applying setWindowOpacity to a custom Widget
    By yellowmat in forum Qt Programming
    Replies: 8
    Last Post: 1st November 2006, 10:05
  3. Custom Widget Plugin Analog Clock
    By kemp in forum Qt Programming
    Replies: 8
    Last Post: 11th August 2006, 13:22
  4. Replies: 3
    Last Post: 12th April 2006, 08:20
  5. Replies: 4
    Last Post: 24th March 2006, 22:50

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.