Unsolved mapToScene returns position with offset
-
@Peter_Dev said in mapToScene returns position with offset:
PointF res = ui->graphicsView->mapToScene(mapTo(ui->graphicsView,event->pos()));//here
What is "mapTo" here?
Did you check all the pointers as already suggested? -
@jsulm Checking now. mapTo maps click position relative to graphicsView, i guess. Am i wrong?
-
@Peter_Dev Did you debug and/or check the pointers? This is actually the first thing to do if your app is crashing...
-
@jsulm it crushes at mapTo, when it tries to call mapToParent inside itself. I think problem is in passing ui->graphicsView as first argument in mapTo
-
@Peter_Dev Again: did you check the pointers? Are those valid?
-
@jsulm Sorry, i'm new here. I don't quite understand what you mean by checking pointers, but as i understood you, yes, all pointers are valid. Before i put mapTo here, everything was working correctly. Sorry for wasting your time, but it's really important project for me, so i need your help very much.
-
@Peter_Dev
Your code showsmapTo(ui->graphicsView,event->pos())
And you wrote
@jsulm it crushes at mapTo, when it tries to call mapToParent inside itself. I think problem is in passing ui->graphicsView as first argument in mapTo
If you're saying that crashes, you need to look at its code to determine why. If you're asking for our help, how can we help if you don't show
mapTo()
's code? -
@JonB I think it's the mapTo from QMainWindow from QWidget :-)
This was my mistake also when reading this code :-) -
QPoint QWidget::mapTo(const QWidget * parent, const QPoint & pos) const { QPoint p = pos; if (parent) { const QWidget * w = this; while (w != parent) { Q_ASSERT_X(w, "QWidget::mapTo(const QWidget *parent, const QPoint &pos)", "parent must be in parent hierarchy"); p = w->mapToParent(p); w = w->parentWidget(); } } return p; }
QPoint QWidget::mapToParent(const QPoint &pos) const { return pos + data->crect.topLeft(); //crushes here }
-
@jsulm , @Peter_Dev
OK then. https://doc.qt.io/qt-5/qwidget.html#mapToQPoint QWidget::mapTo(const QWidget *parent, const QPoint &pos) const
Translates the widget coordinate pos to the coordinate system of parent. The parent must not be
nullptr
and must be a parent of the calling widget.I think we can assume parent,
ui->graphicsView
, is notnullptr
, elseui->graphicsView->mapToScene(...)
would dump, and it wouldn't be to do withmapTo()
. But you should check thatui->graphicsView
pointer value.If not that, I can't quite get my head around it, but
ui->graphicsView
cannot be parent of "calling widget" which isMainWindow
, can it?EDIT This post crossed with @Peter_Dev's latest.
return pos + data->crect.topLeft(); //crushes here
Assuming you are seeing this in the debugger, what is the value of
data
and what does it point to? -
@JonB written that "No such data". So, how to solve my problem? Should i use mapTo or something else?
-
@Peter_Dev
For the record, am I right that you (or at least the QtmapTo()
code you show) are not compiling for debug? I believe in debug it would have hit theQ_ASSERT_X(w, "QWidget::mapTo(const QWidget *parent, const QPoint &pos)", "parent must be in parent hierarchy");
You'll have to ask the experts what's going on/you should do here, I think
QMainWindow::mapTo(ui->graphicsView)
is the wrong thing to be doing, but I don't claim to know what you should be doing.... -
Ok, i understand. I'll be trying to figure it out. Thanks to everyone who helped me here and sorry for wasting your time.