PDA

View Full Version : problem in mouseEvent



wagmare
6th April 2009, 12:39
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


QPoint mouseValue;

if(event->button() == Qt::LeftButton){
mouseValue = event->pos();
if((label2->rect().contains(mouseValue) == true){
printf("*********he clicking on the label ... \n");
}else{
printf("clicking outside ....\n");
}
return ;
}else{
QWidget::mousePressEvent(event);
return ;
}


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

jpn
6th April 2009, 13:10
From QWidget::rect docs:

The rect property equals QRect(0, 0, width(), height()).

wagmare
7th April 2009, 10:28
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 ..

talk2amulya
7th April 2009, 11:09
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(..)

jpn
7th April 2009, 11:10
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.

wagmare
8th April 2009, 10:58
i posted my entire code ...


ToolTipper::ToolTipper(QWidget *parent)
:QWidget(parent ,Qt::ToolTip)
{
QString path = "<font color =red>The communication failed due to heavy rain all network jammed </font>";
label = new QLabel();
label2 = new QLabel();
label2->setGeometry(QRect(0, 0, 116, 121));
label2->setPixmap(QPixmap(QString::fromUtf8(":/images/closeButton2.png")));
label2->setScaledContents(true);
label->setText(path);
QHBoxLayout *horizontalLayout = new QHBoxLayout();
horizontalLayout->addWidget(label);
horizontalLayout->addWidget(label2);
QVBoxLayout *verticalLayout = new QVBoxLayout();
verticalLayout->addLayout(horizontalLayout);
setLayout(verticalLayout);
layout()->setSizeConstraint(QLayout::SetFixedSize);


and in same class a mouseEvent


void ToolTipper::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton){
checkClicked(event->pos());
}else{
QWidget::mousePressEvent(event);
return ;
}




void ToolTipper::checkClicked( const QPoint &pos)
{
int i = pos.x();
int j = pos.y();

if(label->rect().contains(i , j)){
printf("the position is inside ...\n");
}else{
printf("no its not working ...\n");
}

}


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

aamer4yu
8th April 2009, 19:22
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.

wagmare
9th April 2009, 06:20
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 ...