PDA

View Full Version : how can we get the coordinate of desktop when mouse is placed on a particular poin



vinayaka
17th January 2012, 09:40
hai all,


how can we get the coordinate of desktop when mouse is placed on a particular point of desktop?
Can someone help me ?

Lykurg
17th January 2012, 11:05
QWidget::mapToGlobal() and QDesktopWidget if you like.

wysota
17th January 2012, 11:10
QCursor::pos()

vinayaka
18th January 2012, 06:41
We need the cursor position on the desktop while the application is running.
In our project we need to apply magnetic effects on desktop widgets.When we drag the widget to the right edge of the desktop it move to the right edge and hide from the desktop(its working fine in our application). We need the widget to pop- up from the right of the desktop to the left if we slide the mouse on the right edge of the desktop.We need to check whether the current cursor position is on the right edge of desktop. How can we do this?







bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::MouseMove)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
qDebug()<<"Mouse Move event...."<<mouseEvent->pos().x()<<mouseEvent->pos().y();
}
return false;
}




This code works fine inside the widget but don't get the coordinates outside the widget. We need to get the cursor coordinates on desktop

wysota
18th January 2012, 09:25
Either grabMouse() (but then nobody else will receive the events) or periodically poll the cursor position using QCursor::pos(), however this will be inefficient and will probably worsen your user experience. The proper approach would be to hook into the native event system to be notified upon interesting events.

vinayaka
18th January 2012, 09:40
We dont get you?
The proper approach would be to hook into the native event system to be notified upon interesting events.plz specify briefly?

wysota
19th January 2012, 00:06
Briefly speaking -- use WinAPI.

Spitfire
19th January 2012, 16:09
I'm not sure I've understood you well but I think you want to be able to drag window to the right edge of the desktop and then make it pop from the left, right?

If that's the case then it's simple:
void MainWindow::moveEvent( QMoveEvent* )
{
QRect dr = QApplication::desktop()->rect();
QPoint mp = QCursor::pos();

if( mp.x() > dr.right() - 5 )
{
dr.moveLeft( 0 );
mp.setX( 0 );

this->move( dr.topLeft() );
QCursor::setPos( mp );
}
}If that's not the case then I don't know what you want to achieve.

ChrisW67
20th January 2012, 00:46
I think the requirement is to build a "menu" app that normally is invisible, but slides out from the RHS of the desktop when the mouse pointer is pushed to the RHS of the desktop. Presumably the app will slide back off the RHS of the desktop when the mouse pointer is no longer over it.

It seems to me that a peek at QCursor::pos() every 250ms or so (using QTimer) is the easiest way. If the cursor is against the edge in two consecutive checks then pop up. Elegant? Maybe not.

Another approach might be to leave a (transparent?) borderless, always-on-top widget a few pixels wide on the desktop RHS and look for mouse move events on it.

vinayaka
20th January 2012, 12:33
thank you all for your valuable suggestion.Here I am attaching a sample video that show the interaction of Widget.we need to apply the effects like in video.How can we do the same?
http://www.youtube.com/watch?v=jcopjUuVTfU&feature=youtu.be

wysota
20th January 2012, 13:27
So you want a sheet-like behavior. For hiding you can use leaveEvent(), for showing I think Chris has already given you a possible solution.

vinayaka
23rd January 2012, 04:14
can anyone please send the sample code for applying these effects into our Widget?

ChrisW67
23rd January 2012, 23:14
How about you show us what you have tried?

vinayaka
25th January 2012, 05:52
QDesktopWidget *desktop = QApplication::desktop();
screenWidth1 = desktop->width();
screenHeight1 = desktop->height();
x1 = screenWidth1-400;
y1 = screenHeight1-675;
QTimer *cursor_Pos = new QTimer(this);
connect(cursor_Pos, SIGNAL(timeout()), this, SLOT(updatePos()));
cursor_Pos->setInterval(250);
cursor_Pos->start();

void RightSideBar::updatePos()
{
dr = QApplication::desktop()->rect();
QPoint mp = QCursor::pos();
int cursor_check;
cursor_check=QApplication::desktop()->width()-this->width();
if( mp.x() > dr.right() - 10&&this->isVisible()&&cursorRight==true)
{
qDebug()<<"Cursor on Right Edge of desktop!!!";
cursorRight=false;
cursorLeft=true;
widgetRight=true;

this->setGeometry(x1+52,y1,340,screenHeight1-200);

}
if(this->width()+this->x()>dr.right() - 10&&this->isVisible()&&widgetRight==true)
{
qDebug()<<"Widget on Right Edge of desktop!!!";
widgetRight=false;
cursorRight=true;
this->setGeometry(this->width()+this->x()*2,this->y(),340,screenHeight1-200);


}
if(cursor_check>mp.x()&&this->isVisible()&&cursorLeft==true)
{
qDebug()<<"Cursor Position is less then Widget Position";
cursorLeft=false;
cursorRight=true;
this->setGeometry(this->width()+this->x()*2,this->y(),340,screenHeight1-200);

}

}

First and third conditions are working fine in our application.In this code second condition is not working properly.How can we rectify the issues?