How to mapToGlobal a QRect?
-
I have a QWidget (Width-500,500) inside which I have 2 child QWidgets.The 2 child QWidgets have different geometry's.One having ( 0,0,100,100) other at (200,0,100,100).I want to check if a QPoint lies inside these child widgets. I have tried this
QPoint p1;//which is inside 1st child
QPoint p2;//which is inside 2nd child
rectChildWidget1.contains(p1) and rectChildWidget2.contains(p2)But rectChildWidget2.contains(p2) is always getting false , rectChildWidget1.contains(p1) is working fine(If the point goes outside its returning false).I tried mapping cordinates of rectChildWidget2 to global with setX and setY's. No good.Please suggest how to map the rects to global so that rect.contains(point) works fine no matter where the rect is.....
-
Hi,
How is p1 and p2 created ?
-
-
@medyakovvit
No its child's -
Can you show the complete code ?
-
void ChildWidget::mouseMoveEvent(QMouseEvent* event)
{if (event->buttons() & Qt::LeftButton)
{
QRect r=this->geometry();if(r.contains(event->globalPos()))//Also tried event->pos() { qDebug()<<"point inside"; } else { qDebug()<<"point outside"; }
}
QWidget::mouseMoveEvent(event);
}
If the child widget starts at 0,0 works fine...If child widget is not at origin "point outside" will be printed even if the pos is inside.Tried making point global.
-
@Sriu1 said in How to mapToGlobal a QRect?:
If the child widget starts at 0,0 works fine...If child widget is not at origin "point outside" will be printed even if the pos is inside.Tried making point global.
It's been a while since I used this, but mapToGlobal should work just fine!?
> void ChildWidget::mouseMoveEvent(QMouseEvent* event) { if (event->buttons() & Qt::LeftButton) { qDebug() << "Position relative to ChildWidget:" << objectName() << event->pos(); qDebug() << "Position relative to physical screen:" << objectName() << mapToGlobal(event->pos()); } }
-
-
@Sriu1 said in How to mapToGlobal a QRect?:
@J.Hilk Hi,
event()->globalPos() and mapToGlobal(event->pos()) both give same.My problem was with rect.contains().how to map the rect of childWidget to global? So that even if event is occuring anywhere in the screen I should be able to know if its inside the qrect or outsideWell, the same way.
QRect r=this->geometry(); QRect globalRect(mapToGlobal( r.pos() ), r.size() );
-
@J.Hilk Hi,
I tried this
QRect r=this->geometry();
QPoint p(r.x(),r.y());
QRect globalRect(mapToGlobal(p, r.size());getting error
no matching function for call to ‘ChildWidget::mapToGlobal(QPoint&, QSize)’
QRect globalRect(mapToGlobal(p, r.size()); -
@J.Hilk
Still getting....
error: no matching function for call to ‘ChildWidget::mapToGlobal(QPoint&, QSize)’
QRect globalRect(mapToGlobal(p, r.size()));QRect r=this->geometry();
QPoint p(r.x(),r.y());
QRect globalRect(mapToGlobal(p, r.size()));if(!r.contains(event->globalPos()))