Results 1 to 19 of 19

Thread: Problem of mouseDoubleClickEvent

  1. #1
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Problem of mouseDoubleClickEvent

    hi ,
    I have a parent widget to which many widgets are added. Similarly, a QTextEdit is also added to it. But how to catch mouseDoubleClickEvent on the textedit. I have overwritten the mouseDoubleClickEvent(QMouseEvent *), but it is able to catch the event when mouse is double-clicked on the parent widget. kindly help me out, if possible.

    Thanks in advance,
    Sarma.

  2. #2
    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 of mouseDoubleClickEvent

    Use an event filter (unless you want to inherit QTextEdit and override the doubleClickEvent).

    http://doc.trolltech.com/3.3/qobject...allEventFilter
    http://doc.trolltech.com/3.3/eventsandfilters.html
    Last edited by jpn; 7th March 2006 at 10:07.

  3. #3
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Problem of mouseDoubleClickEvent

    Sir,
    Sorry that I couldnot understand that. Iam showing you the part of my code:

    CPT::CPT(QWidget *parent):QWidget(parent)
    {
    //various child widgets are added
    QTextEdit *te=new QTextEdit(this);
    //some other widgets
    }

    Since te is the child widget of CPT, I cannot catch the mouseDoubleClickEvent on it , if I write the following:
    void CPT::mouseDoubleClickEvent(QMouseEvent *)
    { ... }

    How can I catch this event when mouse is double clicked on te. Kindly show the code part if possible.

    Thanks alot ,
    Sarma.

  4. #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 of mouseDoubleClickEvent

    Qt Code:
    1. class CPT : public ...
    2. {
    3. ...
    4. protected:
    5. bool eventFilter(QObject* o, QEvent* e);
    6. ...
    7. };
    8.  
    9. CPT::CPT(QWidget *parent):QWidget(parent)
    10. {
    11. //various child widgets are added
    12. QTextEdit *te=new QTextEdit(this);
    13. te->installEventFilter(this);
    14. //some other widgets
    15. }
    16.  
    17. bool CPT::eventFilter(QObject* o, QEvent* e)
    18. {
    19. if (e->type() == QEvent::MouseButtonDblClick)
    20. {
    21. // process double click
    22. return true; // eat event
    23. }
    24. // standard event processing
    25. return false;
    26. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Problem of mouseDoubleClickEvent

    sorry sir for posting it twice. well, it is not working. Inorder to test it, I have written the following step in line 22 :
    system("echo Double click detected");

    But when I run it and double click on te, I couldnot find anything on the console.
    Also please tell me how to use the code tags in the forums.

    Thanks,
    Sarma.

  6. #6
    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 of mouseDoubleClickEvent

    Did you notice line 13:
    Qt Code:
    1. te->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 
    Basically it tells that you wanna filter all events going to that text edit (in this object).
    So you can catch the mouse double click event without subclassing the text edit.

    Wrap your code with [C O D E] .. [/C O D E] (without spaces)
    Last edited by jpn; 7th March 2006 at 10:58.

  7. #7
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Problem of mouseDoubleClickEvent

    Sir,
    But I have not subclassed QTextEdit. Please show me the way of executing the system("..........") command (I mean, where I have to write it and so on). Don't think otherwise. I am not an expert in Qt. So I need things to be clear for better understanding.

    Thanks once again,
    Sarma.

  8. #8
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Problem of mouseDoubleClickEvent

    For install eventFilter you dont must sublassing your textedit. Read previous posts closely!
    a life without programming is like an empty bottle

  9. #9
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Problem of mouseDoubleClickEvent

    hi jojo,
    Kindly explain me clearly. I couldnot understand what you have said. Where should I place the commands that I want to perform on occurance of this event.
    Last edited by Sarma; 7th March 2006 at 11:58.

  10. #10
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Problem of mouseDoubleClickEvent

    Sir,
    I could see the message on the console. But the thing is that I could catch the event only when I double click along the borders of TextEdit. What is the procedure of making it onto the whole TextEdit area. Please help me out.

    Thanks a lot,
    Sarma.

  11. #11
    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 of mouseDoubleClickEvent

    Quote Originally Posted by Sarma
    I could see the message on the console. But the thing is that I could catch the event only when I double click along the borders of TextEdit.
    Seems so. So the double click event goes to some child of the text edit.

    Quote Originally Posted by Sarma
    What is the procedure of making it onto the whole TextEdit area. Please help me out.
    I did a little test.
    I installed the same event filter also to all child objects of the text edit:
    Qt Code:
    1. te->installEventFilter(this);
    2. QObject* child = 0;
    3. QObjectList children = te->children();
    4. foreach (child, children)
    5. {
    6. child->installEventFilter(this);
    7. }
    To copy to clipboard, switch view to plain text mode 
    Then in the event filter I printed the name of the object like this:
    Qt Code:
    1. if (e->type() == QEvent::MouseButtonDblClick)
    2. {
    3. qDebug() << o->objectName();
    4. return true;
    5. }
    To copy to clipboard, switch view to plain text mode 
    The output was: "qt_scrollarea_viewport"

    So, the conclusion:
    QTextEdit inherits QScrollView (or QScrollArea in Qt 4, which I'm using), and it's the viewport child which seems to receive the double click event.

    The solution:
    Qt Code:
    1. CPT::CPT(QWidget *parent):QWidget(parent)
    2. {
    3. //various child widgets are added
    4. QTextEdit *te=new QTextEdit(this);
    5. te->installEventFilter(this);
    6. te->viewport()->installEventFilter(this);
    7. //some other widgets
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 7th March 2006 at 13:54.

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

    Sarma (7th March 2006)

  13. #12
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Problem of mouseDoubleClickEvent

    Thanks alot jojo,
    Thats working really good. So, In this way can I also implement contextMenuEvent for the TextEdit?
    That must be true.Isn't It?

  14. #13
    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 of mouseDoubleClickEvent

    Quote Originally Posted by Sarma
    Thanks alot jojo,
    Thats working really good. So, In this way can I also implement contextMenuEvent for the TextEdit?
    That must be true.Isn't It?
    You're welcome! Yep, just add a new branch to your filter

  15. #14
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Problem of mouseDoubleClickEvent

    hi jojo,
    How is that done? Is it like this:
    Qt Code:
    1. bool CPT::eventFilter(QObject *o,QEvent *e)
    2. {
    3.  
    4. if( e->type() == QEvent::MouseButtonDblClick )
    5. {
    6. // QMouseEvent *m = (QMouseEvent *)e;
    7. system("echo mouse double clicked");
    8. return true;
    9. }
    10. else if( e->button() == QEvent::RightButton ) // Right button pressed
    11. {
    12. system("echo Right button pressed");
    13. return true;
    14. }
    15.  
    16. return false;
    17. }
    To copy to clipboard, switch view to plain text mode 

    But it is giving me an error saying that there is no button() method in QEvent. Kindly solve this problem too.
    with regards,
    Sarma.

  16. #15
    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 of mouseDoubleClickEvent

    Qt Code:
    1. else if (e-type() == QEvent::ContextMenu)
    To copy to clipboard, switch view to plain text mode 
    See: http://www.qtcentre.org/forum/showpo...2&postcount=11
    Last edited by jpn; 7th March 2006 at 14:42.

  17. #16
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Problem of mouseDoubleClickEvent

    So, I have used what you have suggested. I want to show the following menu on the right click event. After implementing this, I couldnot see any context menu (though the textedit contains some text).
    Qt Code:
    1. if( e->type() == QEvent::ContextMenu ) // Right button pressed
    2. {
    3. if(!te->lines())
    4. {
    5. QPopupMenu *context=new QPopupMenu(this);
    6. context->insertItem("Reload Trace Log File",this,SLOT(reload_log()));
    7. context->insertItem("Find in Trace Log",this,SLOT(defaults()));
    8. context->show();
    9. }
    10. else {}
    11. return true;
    12. }
    To copy to clipboard, switch view to plain text mode 
    Even I have tried it without line 8 and also without "this" in line5

  18. #17
    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 of mouseDoubleClickEvent

    Does changing:
    Qt Code:
    1. context->show();
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. QContextMenuEvent* menuevent = static_cast<QContextMenuEvent*>(event);
    2. context->exec(menuevent->globalPos());
    To copy to clipboard, switch view to plain text mode 
    help?

  19. #18
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Problem of mouseDoubleClickEvent

    No.It is showing following Errors:
    Error: Taking address of the bound function QWidget::event(QEvent*).
    Error: Using static_cast to convert from int to QContextMenuEvent* not allowed.
    If I change "event" in line1 to "e" (as we have used e previously for defining the events), I am not finding the above errors but Iam unable see the context menu on right clicking the mouse.

  20. #19
    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 of mouseDoubleClickEvent

    Quote Originally Posted by Sarma
    If I change "event" in line1 to "e"
    (as we have used e previously for defining the events), I am not finding the above errors
    Oops, sorry for the typo.

    Quote Originally Posted by Sarma
    but Iam unable see the context menu on right clicking the mouse.
    Are you hundred percent sure about this condition:
    Qt Code:
    1. if(!te->lines())
    To copy to clipboard, switch view to plain text mode 
    Wouldn't that only show the context menu when there are NO lines at all...

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

    Sarma (8th March 2006)

Similar Threads

  1. Very strange socket programming problem
    By montylee in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2008, 12:05
  2. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  3. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  4. problem with paint and erase in frame
    By M.A.M in forum Qt Programming
    Replies: 9
    Last Post: 4th May 2008, 20:17
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.