Results 1 to 3 of 3

Thread: mousePressEvent and QGroupBox

  1. #1
    Join Date
    May 2010
    Posts
    11
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default mousePressEvent and QGroupBox

    Hi all,
    I'm trying to build a custom widget that act like a button, but with more feature, like icons and multple labels...

    Like any button I'd like to start a "callback" when the button is clicked. I think that the signal slot model does not work for this inter class case.
    So I try the following code:

    header
    Qt Code:
    1. class CallButton : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. CallButton(QWidget *parent = 0);
    7. ~CallButton();
    8.  
    9. protected:
    10. void mousePressEvent(QMouseEvent *event);
    11.  
    12. QGroupBox *groupBox;
    13. QLabel *label;
    14. QLabel *icon;
    15. };
    To copy to clipboard, switch view to plain text mode 

    and. cpp
    Qt Code:
    1. CallButton::CallButton(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. groupBox = new QGroupBox(this);
    5. label = new QLabel("Label...");
    6. icon = new QLabel;
    7.  
    8. QIcon fakeIcon = QIcon("eclipse24.png");
    9. QPixmap pixmap = fakeIcon.pixmap(QSize(16, 16), QIcon::Normal, QIcon::On);
    10. icon->setPixmap(pixmap);
    11.  
    12. QHBoxLayout *mainLayout = new QHBoxLayout;
    13. mainLayout->addWidget(label);
    14. mainLayout->addWidget(icon);
    15. mainLayout->insertStretch(1);
    16.  
    17. groupBox->setStyleSheet(QString::fromUtf8("\
    18. QGroupBox {\
    19. background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\
    20. stop: 0 #FFFFFF, stop: 1 #E0E0E0);\
    21. border: 2px solid gray;\
    22. border-radius: 5px;\
    23. }"));
    24.  
    25. groupBox->resize(180,60);
    26. groupBox->setLayout(mainLayout);
    27. }
    28.  
    29. void CallButton::mousePressEvent ( QMouseEvent * event )
    30. {
    31.  
    32. groupBox->setStyleSheet(QString::fromUtf8("\
    33. QGroupBox {\
    34. background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\
    35. stop: 0 #E0E0E0, stop: 1 #FFFFFF);\
    36. border: 2px solid gray;\
    37. border-radius: 5px;\
    38. }"));
    39. }
    40.  
    41. CallButton::~CallButton()
    42. {
    43. delete groupBox;
    44. delete label;
    45. delete icon;
    46. }
    To copy to clipboard, switch view to plain text mode 

    The application compile and run, but the event is not launched when I left click inside the button (the event is launched when I left click outside the QGroupBox area and anywhere where I right click).
    Why the left click does not work?
    Thank's in advance to everyone,
    Stefano.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: mousePressEvent and QGroupBox

    Read more about installEventFilter and eventFilter.
    http://doc.qt.nokia.com/4.6/qobject.html#eventFilter

  3. #3
    Join Date
    May 2010
    Posts
    11
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: mousePressEvent and QGroupBox

    Thanks tbscope,
    The eventFilter solution works! I have made the following method, but I am a little scared about the performace of this solution....
    Anyone has a more efficient implementation?

    Qt Code:
    1. bool CallButton::eventFilter(QObject *obj, QEvent *event)
    2. {
    3. if (obj == groupBox) {
    4. if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonDblClick) {
    5. groupBox->setStyleSheet(QString::fromUtf8(PRESS_STYLE));
    6. return true;
    7.  
    8. } else if (event->type() == QEvent::MouseMove) {
    9. // check if the pointer goes outside object area
    10. QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
    11. QGroupBox *groupBoxObj = static_cast<QGroupBox*>(obj);
    12. if( mouseEvent->x() < 0 || mouseEvent->x() > groupBoxObj->width()
    13. || mouseEvent->y() < 0 || mouseEvent->y() > groupBoxObj->height() ){
    14. groupBox->setStyleSheet(QString::fromUtf8(RELEASE_STYLE));
    15. }
    16. return true;
    17.  
    18. } else if (event->type() == QEvent::MouseButtonRelease) {
    19. groupBox->setStyleSheet(QString::fromUtf8(RELEASE_STYLE));
    20. return true;
    21.  
    22. } else {
    23. return false;
    24. }
    25.  
    26. } else {
    27. // pass the event on to the parent class
    28. return QWidget::eventFilter(obj, event);
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 8
    Last Post: 16th April 2010, 09:41
  2. mousePressEvent Problem in Qt4
    By hotjava in forum Qt Programming
    Replies: 2
    Last Post: 9th November 2008, 09:29
  3. QGraphicScene MousePressEvent
    By Spitz in forum Qt Programming
    Replies: 3
    Last Post: 16th November 2007, 21:46
  4. MousePressEvent for QTextEdit
    By anju123 in forum Qt Programming
    Replies: 9
    Last Post: 16th August 2007, 06:08
  5. mousePressEvent
    By mickey in forum Newbie
    Replies: 3
    Last Post: 21st March 2006, 15:36

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.