Results 1 to 5 of 5

Thread: problem with slot

  1. #1
    Join Date
    May 2008
    Posts
    11
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default problem with slot

    Hello!
    I've created my own button. But when I try to connect it with SLOT(quit()) this slot dosn't work. Could anybody find a problem?
    Qt Code:
    1. #ifndef MYBUTTON_H
    2. #define MYBUTTON_H
    3.  
    4. #include <QtGui>
    5.  
    6. class MYButton : public QAbstractButton
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. MYButton(QWidget * parent = 0);
    12. ~MYButton();
    13.  
    14. void setDefaulImageFileName(QString &filename);
    15. void setActionImageFileName(QString &filename);
    16.  
    17. protected:
    18. void paintEvent(QPaintEvent * pe);
    19. QSize sizeHint();
    20.  
    21. void enterEvent(QEvent * e);
    22. void leaveEvent(QEvent * e);
    23. void mousePressEvent(QMouseEvent * e);
    24. void mouseReleaseEvent(QMouseEvent * e);
    25.  
    26. private:
    27. QString defaultImageFileName;
    28. QString actionImageFileName;
    29. QImage *defaultImage;
    30. QImage *actionImage;
    31. bool m_enter;
    32. bool m_leave;
    33. bool m_mousePressEvent;
    34. bool m_mouseReleaseEvent;
    35. };
    36.  
    37. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtGui>
    2. #include "MYbutton.h"
    3. static const int buttonWidth=233;
    4. static const int buttonHeight=55;
    5. static const QSize buttonSize(buttonWidth, buttonHeight);
    6.  
    7.  
    8.  
    9. MYButton::MYButton(QWidget *parent) : QAbstractButton(parent)
    10. {
    11. QSize frameSize(buttonWidth+3, buttonHeight+6);
    12. setPalette(QPalette(QColor(0, 0, 0)));
    13. setAutoFillBackground(true);
    14. setMaximumSize(frameSize);
    15. setMinimumSize(frameSize);
    16. m_leave=true;
    17. m_enter=false;
    18. m_mousePressEvent=false;
    19. m_mouseReleaseEvent=false;
    20. }
    21.  
    22. MYButton::~MYButton()
    23. {
    24. }
    25.  
    26. void MYButton::paintEvent(QPaintEvent *pe)
    27. {
    28. Q_UNUSED(pe);
    29. QImage *defaultImage = new QImage;
    30. QImage *actionImage = new QImage;
    31. defaultImage->load(defaultImageFileName);
    32. actionImage->load(actionImageFileName);
    33. QImage *image = new QImage;
    34. if (!m_enter)
    35. {
    36. image=defaultImage;
    37. }
    38. else
    39. {
    40. image=actionImage;
    41. };
    42.  
    43.  
    44. QPainter painter(this);
    45. QPointF point;
    46. if(!m_mousePressEvent)
    47. {
    48. point.setX(1);
    49. point.setY(1);
    50. }
    51. else
    52. {
    53. point.setX(3);
    54. point.setY(3);
    55. }
    56.  
    57. painter.drawImage(point, *image);
    58.  
    59. }
    60.  
    61. QSize MYButton::sizeHint()
    62. {
    63. return buttonSize;
    64. }
    65.  
    66. void MYButton::enterEvent(QEvent *e)
    67. {
    68. m_enter=true;
    69. this->repaint();
    70. }
    71.  
    72. void MYButton::leaveEvent(QEvent *e)
    73. {
    74. m_enter=false;
    75. m_mousePressEvent=false;
    76. this->repaint();
    77. }
    78.  
    79. void MYButton::mousePressEvent(QMouseEvent *e)
    80. {
    81. m_mousePressEvent=true;
    82. this->repaint();
    83. }
    84.  
    85. void MYButton::mouseReleaseEvent(QMouseEvent *e)
    86. {
    87. m_mousePressEvent=false;
    88. this->repaint();
    89. }
    90.  
    91. void MYButton::setActionImageFileName(QString &filename)
    92. {
    93. actionImageFileName=filename;
    94. }
    95.  
    96. void MYButton::setDefaulImageFileName(QString &filename)
    97. {
    98. defaultImageFileName=filename;
    99. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QtGui/QVBoxLayout>
    3. #include "MYbutton.h"
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MYButton *button1=new MYButton;
    8. button1->setDefaulImageFileName(QString("images/default_button.png"));
    9. button1->setActionImageFileName(QString("images/action_button.png"));
    10. QObject::connect(button1,SIGNAL(clicked()),&a, SLOT(quit()));
    11. button1->show();
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by as001622; 23rd May 2008 at 09:42.

  2. #2
    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: problem with slot

    You have to call the base class implementation of mouse events.

  3. The following user says thank you to wysota for this useful post:

    as001622 (23rd May 2008)

  4. #3
    Join Date
    May 2008
    Posts
    11
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem with slot

    Thanks, but could you explain me where I have to do this, please?

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: problem with slot

    Qt Code:
    1. void SubClass::function()
    2. {
    3. BaseClass::function();
    4. ...
    5. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    as001622 (23rd May 2008)

  7. #5
    Join Date
    May 2008
    Posts
    42
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4

    Default Re: problem with slot

    Hi,

    I think you must set setMouseTracking(true).
    Try this.

    Thanks and regards.
    V. Santhosh

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, 00:41
  2. Replies: 2
    Last Post: 20th September 2007, 12:27
  3. Problem with slot
    By beerkg in forum Qt Programming
    Replies: 29
    Last Post: 3rd April 2007, 19:54
  4. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  5. Replies: 7
    Last Post: 15th February 2006, 11:34

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.