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.
Related
Good day to all!
I would like to learn from the respected community how it is possible using Qt Designer to create a simple GUI that performs what is shown in the attached image. Namely: after a single click of the LMB, a marker is placed at the place of the click, indicating the beginning of the polygon, and a temporary polygon of the desired type (but of different sizes) begins to follow the mouse cursor until we click on the LMB again, after which the final polygon will be built from the point of the first click of the LMB to the point of the second click. Such a ready-made polygon will have to be stored somewhere in memory for further work with it.
As a polygon here, I show a complex sector built on some calculated points, but as a simple example, you can take a straight line - I don't think that the essence of the program changes much.
I have already looked at many examples of different implementations of different things on QGraphicsScene, but I could not figure out exactly how to create an object in the way I needed. That is, I know that we need a separate class describing the polygon and calculating the coordinates of which it consists, but I don't quite understand how to implement the dynamics - with each mousemoveivent, delete the temporary polygon and draw a new one for the new coordinates, or how?
P.S. If it won't be so difficult, could someone also show how to implement what was conceived through the redefined paintScene class, replacing the standard QGraphicsScene class and inheriting from it? I am faced with the fact that when creating a custom class of the scene object, clicking on the objects attached to it is ignored by the program, and instead of, for example, dragging an object on the scene when clicking on it, the mousePressEvent of the scene, not the object, is triggered, and I do not understand what the problem is.
P.P.S. I apologize for my English and thank everyone for any help!
Now, I have geometry information of some curves, which are given with the following format.
<LineString><coordinates>-43.276042355627,-32.8022614460173,0.0 -43.9683944443137,-31.9135623685828,0.0 -44.4979806584518,-31.1926527722131,0.0</coordinates></LineString>
I would like to show them in my scene. I know I can do this by creating multiple QGraphicsLineItems one by one. But is there any other easy way to do this, like one curve item?
You can use QGraphicsPathItem along with QPainterPath.
Reimplement the QGraphicsPathItem, add a method that receives your original points. Create a QPainterPath and iterate over points. Use moveTo on the first point and lineTo for the next points. Then call setPath of QGraphicsPathItem to redraw the new curve.
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.
I'm using QT creator and have all my methods etc designed and trying to design a UI and I'm not sure what widget I should be using to reference my puzzle layout.
I've been reading over the documentation for a while and I still am nowhere closer to finding a solution to what I should be using to display my values. I know what I should like but all I seem to find information on is various layouts of entry boxes that are in no way linked. I have no problem with coding it myself to communicate with my classes if I just knew what to use to start off with.
I need something that can help me create a layout that has up to two numbers with positions specific to the type of hint it is with a slash between the hints, black blocks that are neither hints nor stored values and squares that have values that can take up the whole square.
Layout is something similar to http://www.nikoli.co.jp/en/puzzles/kakuro/
I wish I knew what to pick >.<
Personally I'd use QGraphicsView and handle the drawing myself.
It can draw rectangles, triangles, circles and text without much effort, and that's pretty much everything you need as far as I can see. You just need to add the objects to a QGraphicsScene and you get them on the screen. You can also interact with the objects (you can find which object you're pointing at etc.)
I subclassed QGraphicsItem and reimplemented paint.
In paint I wrote something like this for labeling the item:
painter->drawText("Test",10,40);
After some time I think It may be useful to handle labeling with seperate item. So I wrote something like this.
QGraphicsTextItem *label = new QGraphicsTextItem("TEST",this);
setPos(10,40);
But two "TEST" drawing do not appear in the same place on screen. I guess difference may be related with item coordinates - scene coordinates. I tried all mapFrom... and mapTo... combinations inside QGraphicsItem interface but no progress. I want to drawings to appear in the same place on screen.
What I miss?
I assume that you are using the same font size and type in both cases. If the difference in position is very small the reason can be the QGraphicTextItem is using some padding for the text it contains. I would try to use QGraphicsSimpleTextItem that is not going to add fancy stuff internally and see if you still have the same problem. The coordinates system is the same one if you use painter or setPost so that is not the problem. If this doesn't help I will suggest to specify the same rect for both to avoid Qt adding it owns separation spaces.