Results 1 to 10 of 10

Thread: MousePressEvent for QTextEdit

  1. #1
    Join Date
    Jun 2007
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default MousePressEvent for QTextEdit

    I have used a QTextEdit on my main widget, Now the problem is that it is not recognizing the MousePress event when I click on the QTextEdit area. I have read similar posts on this problem, but nothing is working. Another strange thing is that the QTextEdit is responding to "Doubleclick" event perfectly but not for "MousePress" event. I am not able to understand this behaviour.
    Any kind of help is more than welcome.

  2. #2
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: MousePressEvent for QTextEdit

    Hi Anju,
    have you used installEventFilter() for QTextEdit?

  3. #3
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: MousePressEvent for QTextEdit

    you subdrive QTextEdit and write
    void MyTextEdit::mousePressEvent(QMouseEvent *event)
    {
    //event->ignore();
    }

    OR

    you can use installEventFilter
    see the example:

    Qt Code:
    1. class MyMainClass
    2. {
    3. Q_OBJECT
    4.  
    5.  
    6. protected:
    7. bool eventFilter(QObject *target, QEvent *event);
    8. private:
    9. QTextEdit * m_pTextEdit;
    10. };
    11.  
    12. in constructor:
    13. m_pTextEdit->installEventFilter(this);
    14.  
    15. bool MyMainClass::eventFilter(QObject *target, QEvent *event)
    16. {
    17. if (target == m_pTextEdit)
    18. {
    19. if (event->type() == QEvent::MouseButtonPress)
    20. {
    21. // you can write your code here, like this:
    22. }
    23. if (event->type() == QEvent::KeyPress)
    24. {
    25. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    26. std::string str = keyEvent->text().toStdString();
    27. char ch = str[0];
    28. if (!((ch <= '9' && ch >= '0') | (ch <= 'F' && ch >= 'A') |
    29. (ch <= 'f' && ch >= 'a')))
    30. {
    31. if (ch != 9) return true;
    32. else
    33. return QWidget::eventFilter(target, event);
    34. }
    35.  
    36. }
    37. }
    38. else
    39. return QWidget::eventFilter(target, event);
    40. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jun 2007
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: MousePressEvent for QTextEdit

    I have tried the installeventFilter as well, I am now pasting the code as I think it might be the problem with my code.
    Here is my class:
    Qt Code:
    1. SelectableItem_C::SelectableItem_C(QWidget* parent)
    2. {
    3. _label_textedit = new QTextEdit(this);
    4. _label_textedit->setFont(font);
    5. _label_textedit->setAlignment(Qt::AlignCenter);
    6. _label_textedit->setPaletteBackgroundColor(dark_background);
    7. _label_textedit->setAutoFillBackground(true);
    8. _label_textedit->setFrameShape(QFrame::NoFrame);
    9. _label_textedit->setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
    10. _label_textedit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    11. _label_textedit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    12. _label_textedit->setTextInteractionFlags(Qt::NoTextInteraction);
    13.  
    14.  
    15. QSizePolicy sizePolicy(static_cast<QSizePolicy::Policy>(5), static_cast<QSizePolicy::Policy>(5));
    16. sizePolicy.setHorizontalStretch(0);
    17. sizePolicy.setVerticalStretch(0);
    18. sizePolicy.setHeightForWidth(_label_textedit->sizePolicy().hasHeightForWidth());
    19. _label_textedit->setSizePolicy(sizePolicy);
    20.  
    21. _label_textedit->viewport()->setCursor(Qt::ArrowCursor);
    22. _label_textedit->installEventFilter(this);
    23. _label_textedit->viewport()->setMouseTracking(true);
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 

    and here is the event filter function in this class
    Qt Code:
    1. bool SelectableItem_C::eventFilter( QObject *obj, QEvent *ev )
    2. {
    3. //printf("got in\n");
    4. if ( obj == _label_textedit )
    5. {
    6. if ( ev->type() == QEvent::MouseButtonPress )
    7. {
    8. emit MousePressed_Signal(this);
    9. return TRUE;
    10. }
    11. }
    12. else
    13. {
    14. //printf("af: %d,%d\n",e->x(),e->y());
    15. // pass the event on to the parent class
    16. return QWidget::eventFilter( obj, ev );
    17. }
    18. return TRUE;
    19. }
    To copy to clipboard, switch view to plain text mode 
    I have defined a "MousePressed_Signal" which is doing the desired work.
    I could not even come in to the "If condition" with my debugging.
    One more thing is that I have overridden the functions "mousePressEvent" and "mouseDoubleClickEvent" for my class. I can see that control goes to the function "mouseDoubleClickEvent" when I double click on the QTextEdit control, but not when I just do a single click on the same QTextEdit ?

  5. #5
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: MousePressEvent for QTextEdit

    Anju,
    which version of Qt you are using?
    I doesn't find setPaletteBackgroundColor() in Qt 4.3

    in my code mousePressEvent(QMouseEvent *event) working fine.
    anyway I will test your code and write you back.

  6. #6
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: MousePressEvent for QTextEdit

    Anju,
    I tested your code in my application and mousePressEvent(QMouseEvent *event) working fine. even eventFilter() not required.
    I have Qt4.3.1 on windows xp.

  7. #7
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MousePressEvent for QTextEdit

    Try attaching the event filter into the viewport (look into the documentation of QAbstractScrollArea.

  8. The following user says thank you to elcuco for this useful post:

    anju123 (16th August 2007)

  9. #8
    Join Date
    Jun 2007
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: MousePressEvent for QTextEdit

    Ok..
    So it could be the problem of QT versiosn as well. I am using Qt 4.2.2 on windows Xp

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

    Default Re: MousePressEvent for QTextEdit

    It's not a problem of any specific Qt version. Read QObject::eventFilter() docs:
    In your reimplementation of this function, if you want to filter the event out, i.e. stop it being handled further, return true; otherwise return false.
    J-P Nurmi

  11. #10
    Join Date
    Jun 2007
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: MousePressEvent for QTextEdit

    Thanks to everybody,
    I need to use _label_textedit->viewport()->installEventFilter(this); to get the control for required mousepress event

Similar Threads

  1. Replies: 6
    Last Post: 30th April 2007, 23:59

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.