Results 1 to 3 of 3

Thread: mousePressEvent do not work

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy mousePressEvent do not work

    Hi all,

    I fail to get the mousePressEvent working for QDateEdit.

    If the right button is clicked a menue shows up. Is there a way to setup my one menue for this ? (add items at least)

    Thanks
    Dexli

    Qt Code:
    1. // Mywidget.h
    2.  
    3. #ifndef MYWIDGET_H
    4. #define MYWIDGET_H
    5.  
    6. #include <QtGui>
    7.  
    8. class MyWidget : public QDateEdit
    9.  
    10. {
    11. Q_OBJECT
    12. public:
    13. explicit MyWidget(QWidget *parent = 0);
    14. protected:
    15. virtual void mousePressEvent ( QMouseEvent * event ) ;
    16. public slots:
    17.  
    18. };
    19.  
    20. #endif // MYWIDGET_H
    21.  
    22. // MyWidget.cpp
    23.  
    24. #include <QMessageBox>
    25. #include "mywidget.h"
    26.  
    27. MyWidget::MyWidget(QWidget *parent ):
    28. QDateEdit(parent)
    29. {
    30. setDate(QDate(1977,2,15));
    31.  
    32. }
    33. void MyWidget::mousePressEvent ( QMouseEvent * event )
    34. {
    35. QMessageBox *box = new QMessageBox();
    36. box->setText("Button geklickt");
    37. box->show();
    38. };
    39.  
    40. // Main.cpp
    41. #include <QtGui>
    42. #include "mywidget.h"
    43.  
    44. int main(int argc, char *argv[])
    45. {
    46. QApplication app(argc, argv);
    47. QWidget window;
    48. MyWidget* dateed = new MyWidget(&window);
    49. QHBoxLayout* layout = new QHBoxLayout(&window);
    50. layout->addWidget(dateed);
    51. window.show();
    52. return app.exec();
    53. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2009
    Location
    Greece
    Posts
    69
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mousePressEvent do not work

    If you want to do something with the right click you should subclass the contextMenuEvent(QContextMenuEvent *event)

    Mywidget.h
    Qt Code:
    1. #ifndef MYWIDGET_H
    2. #define MYWIDGET_H
    3.  
    4. #include <QtGui>
    5. class QAction;
    6.  
    7. class MyWidget : public QDateEdit
    8.  
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit MyWidget(QWidget *parent = 0);
    13. protected:
    14. virtual void contextMenuEvent(QContextMenuEvent *event);
    15. public slots:
    16. private:
    17. QAction *actionNew;
    18.  
    19. };
    20.  
    21. #endif // MYWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    MyWidget.cpp
    Qt Code:
    1. #include <QMessageBox>
    2. #include <QAction>
    3. #include "mywidget.h"
    4.  
    5. MyWidget::MyWidget(QWidget *parent ):
    6. QDateEdit(parent)
    7. {
    8. setDate(QDate(1977,2,15));
    9. actionNew = new QAction(tr("Do Something New"), this);
    10. actionNew->setShortcuts(QKeySequence::New);
    11. // connect(actionNew, SIGNAL(triggered()), this, SLOT(DoSomethingNew()));
    12. }
    13.  
    14. void MyWidget::contextMenuEvent(QContextMenuEvent *event)
    15. {
    16. QMenu menuRight(this);
    17. menuRight.addAction(actionNew);
    18.  
    19. menuRight.exec(event->globalPos());
    20. }
    To copy to clipboard, switch view to plain text mode 

    NOTE: this code replaces the default context menu
    Last edited by Rhayader; 24th March 2011 at 20:00. Reason: Typos

  3. #3
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mousePressEvent do not work

    Hi Rhayader, hi rest

    thanks for this hint.
    In the meantime i found out why the mouseevents do not work as i expected.
    Actually they work but not for the edit field (QlineEdit) they work for the QSpinBox.
    If one click on the arrow the are called.


    Two additional questions :
    1. How to overwrite the QLineEdit mouseevent methods.
    2. How can I access the 'original' menue to add the action there instead of creatin a new menu.

    Thanks and have fun
    Dexli

Similar Threads

  1. webview from UI and mousepressevent
    By maston in forum Qt Programming
    Replies: 1
    Last Post: 1st September 2010, 12:58
  2. mousePressEvent in QLabel
    By Sparx in forum Newbie
    Replies: 4
    Last Post: 23rd June 2010, 10:28
  3. mousePressEvent Problem in Qt4
    By hotjava in forum Qt Programming
    Replies: 2
    Last Post: 9th November 2008, 09:29
  4. QGraphicScene MousePressEvent
    By Spitz in forum Qt Programming
    Replies: 3
    Last Post: 16th November 2007, 21:46
  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
  •  
Qt is a trademark of The Qt Company.