How do I get x,y, values of qml element after moving with mouse area? - c++

Using Qt5 official release --
I need to save the x,y properties of an image element. This
QObject* pic = rootitem1->findChild<QObject*>("image_objectName");
int x = pic->property("x").toInt();
gives the value the property was originally set with, but what I need is the current value after the image has been moved by the mouse. I'm probably missing something simple but I can't find any thing else on it. Really would appreciate any help, THANKS.

Related

how to get Qt Mouse Position all the time

So I have been struggling with this question for quite some time. I have tried many things, none of them seem to work.
So, I want to make a game in Qt, and one of the things I need is that player(QRectItem for now) rotate always to the mouse position. I just need to get readings of that position all the time, so not when i click or when I drag, all the time.
How can I do that?
I set
this->setMouseTracking(true);
on a class that inherits for QGraphicsView class, also I have set focus on it.
Dont know if the problem is with overriding functions(dont know which one to override) or with focus.
void Game::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << QCursor::pos();
}
Did this but it does not work at all.
Btw, I am noob it Qt, this is my first project.
Thanks in advance! :)
P.S.
I have really done research, but if I have somehow missed topic with same or similar question that can solve this problem, just paste it and accept my apologies. :)
EDITED
You can install an event filter on your QApplication object, examine the received events for mouse movement events, convert the resulting position into your scene, and then use it to orient your rectangle.
Look at QObject::installEventFilter. Event filters are pretty easy to use. When a mouse event is received by an object, its coordinates are in that object's coordinate space, so you'll need to convert from that to your graphics scene coordinates. There will probably be several conversions to get that because you'll need to map the received position to your QGraphicsView using mapTo and then map the result of that to your scene using QGraphicsView::mapToScene.
This should get you pretty close. Let me know if you need more help.

How to move a mouse using the concept of object tracking in opencv?

I was looking online and how people created this virtual mouse application using different principles such as color detector, object tracking, convexity defects etc etc. I wrote this program which actually tracks a single object from a webcam and its actually working really well. Now, the idea is to use this moving object which is being tracked and i want to control my mouse using this application. I dont want the source code and i want to do it myself. But i dont have any clue how to start it. I just hope if you guys can give me some clue or some ideas which can help me to connect my mouse to my program? I want to be able to do left click, right click, double click . Thanks
If you are able to track an object in the screen then you will get the object co-ordinates & set that as your target co-ordinate. I assume you already did that.
Now set the mouse cursor to that target co-ordinate refer this thread.

How do you make a clickable sprite in SFML?

I've been looking through the SFML documentation for making clickable sprites, but so far I haven't found anything.
Do you guys think you could help me out?
There is nothing like sf::ClickableSprite in SFML so far, and probably there will never be. (Current list of classes in SFML)
However, you can obtain this behavior with the sf::Sprite object and the events. The idea is simple - as soon as you get the sf::Mouse::isButtonPressed(sf::Mouse::Left) event, check if the mouse is in the sprite. If it is, perform the action. You can perform another action (maybe undo) when button is released.
There is sf::Sprite::getGlobalBounds() function which returns you the position and the dimensions of the sprite. There's also sf::Mouse::getPosition() function, which returns the current position of the mouse. You can use sprite.getGlobalBounds().contains(mousePos) to check whether the mouse is in the sprite.
If you're using views, you'll need to add the view's position to sf::Mouse::getPosition(window), since it gets the mouse position relative to window coordinates.
(thanks to Chaosed0 for additional notes.)

Tool tip to show plot values in Qwt

I have a qwt plot in my application. I want to show a small tool tip to show the value of the point at which mouse is pointed on the curve. I found that I have to use QwtPlotPicker for this, but couldn't find any proper example to implement this in my code. I am new to Qwt so it would be great if anyone could help me solve this problem.
Thanks, Rakesh.
The author himself says here:
A QwtPlotPicker gives you the current position of the mouse ( in screen and plot coordinates ). Then you need to find the closest points of your curves. You can use QwtPlotCurve::closestPoint(), but in most cases you can find a much faster implementation depending on the characteristics of your data.
When you need to compare the mouse position with the lines between the points you need the pixel position of these points ( use QwtPlot::canvasMap ).
Maybe looking at the CanvasPicker of the eventfilter example helps.
I implemented it in my own class, which is a subclass of QwtPlot. In the constructor I have the following:
QwtPlotPicker* plotPicker = new QwtPlotPicker(this->xBottom, this->yLeft, QwtPicker::CrossRubberBand, QwtPicker::AlwaysOn, this->canvas());
QwtPickerMachine* pickerMachine = new QwtPickerClickPointMachine();
plotPicker->setStateMachine(pickerMachine);
connect(plotPicker, SIGNAL(selected(const QPointF&)), this, SLOT(onSelected(const QPointF&)));
Now in my class (where the this pointer refers to) I should implement the slot onSelected(const QPointF&) which will give the plot coordinates.

C++ SFML 1.6 Sprite Position with Mouse

When using SFML 1.6, I have run into a small problem, that I know there is an easy solution, but I currently cannot think of it/ haven't been able to find the answer via research.
I know that I have to transform the sprite to a global position using TransformToGlobal(someVector), but I don't know where to put it.
Here is an example of my code that I am using which does not work because it isn't in the global position.
if(sprite.GetSubRect().Contains(mouseX, mouseY))
sprite.SetImage(someImage);
else
sprite.SetImage(someOtherImage);
I tried adding sprite.TransformToGlobal(sprite.GetPosition()); before it, but it did not work as well.
You could try using the transformed mouse position and then checking if the sprites contains the mouse:
sf::Vector2f mousePos = App.ConvertCoords(App.GetInput().GetMouseX(), App.GetInput().GetMouseY());
if(sprite.GetSubRect().Contains(mousePos.x, mousePos.y))
sprite.SetImage(someImage);
else
sprite.SetImage(someOtherImage);