Results 1 to 8 of 8

Thread: problem in mouseEvent

  1. #1
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default problem in mouseEvent

    friends,
    i create a custom tooltip .. inside there is a label with a closeButton pixmap ...
    i try to post mouseEvent on the label ... so i try like this
    Qt Code:
    1. QPoint mouseValue;
    2.  
    3. if(event->button() == Qt::LeftButton){
    4. mouseValue = event->pos();
    5. if((label2->rect().contains(mouseValue) == true){
    6. printf("*********he clicking on the label ... \n");
    7. }else{
    8. printf("clicking outside ....\n");
    9. }
    10. return ;
    11. }else{
    12. QWidget::mousePressEvent(event);
    13. return ;
    14. }
    To copy to clipboard, switch view to plain text mode 

    i try clicking on the label .. but still its giving clicking outside ... where i done the mistake ... please help ...

  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 in mouseEvent

    From QWidget::rect docs:
    The rect property equals QRect(0, 0, width(), height()).
    J-P Nurmi

  3. #3
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem in mouseEvent

    Quote Originally Posted by jpn View Post
    From QWidget::rect docs:
    thanks for reply ...
    but the docs tells
    bool QRect::contains ( const QPoint & point, bool proper = false ) const

    Returns true if the the given point is inside or on the edge of the rectangle, otherwise returns false. If proper is true, this function only returns true if the given point is inside the rectangle (i.e., not on the edge).




    so how can i handle this ... how to use QRect::Contains() function .. is there any alternative ..
    Last edited by wagmare; 7th April 2009 at 10:36.

  4. #4
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem in mouseEvent

    assuming you are handling this mouse event in your custom widget, pos() is returning point relative to the custom widget..not relative to the label..so use mapFromParent(assuming the custom widget is the parent) to get point relative to your own label widget..then use rect.contains(..)

  5. #5
    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 in mouseEvent

    Ok, so you want to know WHERE the label is, to be able to check whether some coordinates are inside it or not. Your problem is that QWidget::rect() does not tell you WHERE the label is, it returns an INTERNAL rectangle where top left corner is always (0,0). Please refer to the geometry documentation as proposed by the QWidget::rect docs.
    J-P Nurmi

  6. #6
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem in mouseEvent

    i posted my entire code ...
    Qt Code:
    1. ToolTipper::ToolTipper(QWidget *parent)
    2. :QWidget(parent ,Qt::ToolTip)
    3. {
    4. QString path = "<font color =red>The communication failed due to heavy rain all network jammed </font>";
    5. label = new QLabel();
    6. label2 = new QLabel();
    7. label2->setGeometry(QRect(0, 0, 116, 121));
    8. label2->setPixmap(QPixmap(QString::fromUtf8(":/images/closeButton2.png")));
    9. label2->setScaledContents(true);
    10. label->setText(path);
    11. QHBoxLayout *horizontalLayout = new QHBoxLayout();
    12. horizontalLayout->addWidget(label);
    13. horizontalLayout->addWidget(label2);
    14. QVBoxLayout *verticalLayout = new QVBoxLayout();
    15. verticalLayout->addLayout(horizontalLayout);
    16. setLayout(verticalLayout);
    17. layout()->setSizeConstraint(QLayout::SetFixedSize);
    To copy to clipboard, switch view to plain text mode 

    and in same class a mouseEvent
    Qt Code:
    1. void ToolTipper::mousePressEvent(QMouseEvent *event)
    2. {
    3. if(event->button() == Qt::LeftButton){
    4. checkClicked(event->pos());
    5. }else{
    6. QWidget::mousePressEvent(event);
    7. return ;
    8. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void ToolTipper::checkClicked( const QPoint &pos)
    2. {
    3. int i = pos.x();
    4. int j = pos.y();
    5.  
    6. if(label->rect().contains(i , j)){
    7. printf("the position is inside ...\n");
    8. }else{
    9. printf("no its not working ...\n");
    10. }
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

    here if it is label .. checkClicked() prints "the position is inside ..." but if i replace label to label2 ie the label with pixmap checkClicked() prints "no its not working ..."

    i dont know what is the difference b/w a label and label with pixmap .... please help

  7. #7
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem in mouseEvent

    Did you consider mapping the position from local to parent / global and then checking the condition, as said earlier by amulya ??

    please give time to understand what is being said to you. And also Assistant has quite good details about the geometry.

  8. #8
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem in mouseEvent

    Quote Originally Posted by aamer4yu View Post
    Did you consider mapping the position from local to parent / global and then checking the condition, as said earlier by amulya ??

    please give time to understand what is being said to you. And also Assistant has quite good details about the geometry.
    sorry ..
    yes i am trying only what amulya told me ... but i just posted with one doubt only ... the parent is having two label ... contains work on one label but not on another label ... its different doubt ... i am following only what u , amulya and jpn told me ...

Similar Threads

  1. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  2. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  3. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  4. Problem with QLabel & mouseEvent
    By harakiri in forum Newbie
    Replies: 5
    Last Post: 26th May 2007, 13:54
  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.