Results 1 to 9 of 9

Thread: Help with mousePressEvent implementation

  1. #1
    Join Date
    Oct 2007
    Posts
    14
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Help with mousePressEvent implementation

    Hi all,

    I've a QLabel wherein, I've to trigger an action according to the mouse presses inside that label rectangle. For this I've implemented a mouePressEvent function in which, I'm checking for the mouse Press event's pos(), to check out whether it falls in the first half or the second half of the label rectangle,(I've divided the total rect of label widget into 2 halfs) and on the results of which I'm triggering the right action accordingly.
    Now the programs works fine in a way only thing is, it requires double click to actually trigger the particular action which I've set in the slot. I'm wondering why is it requiring double clicks and not working with a single click on it??
    The parent widget is the QMainWindow and there are couple of other widgets in the QMainWindow widget along with this QLabel widget.
    is there any problem of mouse focus to the QLabel widget? or am I missing something very basic over here?

    plz help.

    regards.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with mousePressEvent implementation

    No, it must be your implementation. Can you post some code, or review it yourself?

  3. #3
    Join Date
    Oct 2007
    Posts
    14
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with mousePressEvent implementation

    Quote Originally Posted by marcel View Post
    No, it must be your implementation. Can you post some code, or review it yourself?
    ok the mousePressEvent() is here:
    Qt Code:
    1. void MainWindow::mousePressEvent(QMouseEvent* event)
    2. {
    3. QRect r = centralWidget->rect().intersect(pageLabel->frameRect());
    4.  
    5. QRect firstHalf(r.x(), r.y(), (r.width() / 2), r.height());
    6.  
    7. QRect secondHalf((r.x() + (r.width() / 2) ), r.y() , (r.width() / 2), r.height());
    8.  
    9. if(firstHalf.contains(QPoint(event->x(), event->y()), true)) {
    10. scribblePad->navigatePrev();
    11. setPageNumber();
    12. }
    13. else if(secondHalf.contains(QPoint(event->x(), event->y()), true)) {
    14. scribblePad->navigateNext();
    15. setPageNumber();
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    and as i said the parent widget is the a QMainWindow widget, in which couple of other widgets are also along with this QLabel widget. The scribblePad over there is such another widget, which is used for scribbling purpose and this label represents its page numbers.
    And the problem is, the QLabel requires two clicks to actually trigger the action put into the respective slots.

    pls tell me your review of the code.

    Regards

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with mousePressEvent implementation

    You must reimplement the QLabel's mouse press event, not the one for the window.
    Most likely that is why you're getting thius behavior.

    To access scribblePad from the labels's mouse event you have to make your main window a singleton. Then you can do something like MainWindow::instance()->getScribblePad()->navigateNext().

    Regards

  5. The following user says thank you to marcel for this useful post:

    QuTe (29th October 2007)

  6. #5
    Join Date
    Oct 2007
    Posts
    14
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with mousePressEvent implementation

    Hi,

    as per your suggestion I've reimplemented the mousePressEvent() for QLabel only, and not for the main window; but unfortunately the problem still persists and it requires double clicks to actually trigger the respective actions!

    is there something else I need to do to solve this problem?

    Regards

  7. #6
    Join Date
    Oct 2007
    Posts
    14
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with mousePressEvent implementation

    is it too enigmatic to get a solution for?

    I still have not been able to get it with a single click somehow
    can someone help?

    Thanks

  8. #7
    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: Help with mousePressEvent implementation

    Is this still unsolved? How does the mousePressEvent() implementation look like now?
    J-P Nurmi

  9. #8
    Join Date
    Oct 2007
    Posts
    14
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with mousePressEvent implementation

    No, it has solved my problem.
    sry I couldn't msg over here back then b'coz it worked out after couple of days of my last post.
    the mousePressEvent() works fine now


    Thanks marcel

  10. #9
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with mousePressEvent implementation

    Please post your code. I'm having a problem with activating a particular QWidgetAction given a mouse event from a QPushButton in that QWidgetAction. You code could be very useful to me.

    Thanks

Similar Threads

  1. UI implementation style
    By goes2bob in forum Qt Tools
    Replies: 2
    Last Post: 16th August 2007, 00:37
  2. RubberBand zoom implementation for images
    By cdemirkir in forum Qt Programming
    Replies: 4
    Last Post: 16th July 2007, 23:55
  3. Replies: 6
    Last Post: 1st May 2007, 00:59
  4. Implementation of right indentation in a text editor
    By sukanyarn in forum Qt Programming
    Replies: 11
    Last Post: 26th September 2006, 14:06
  5. source and implementation
    By shrikarcse in forum Newbie
    Replies: 6
    Last Post: 29th March 2006, 06:02

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.