0

To determine the coordinates of an object with Qapplication and QGraphicsItem Library,Usually used setPos and setPos(mapToParent(yvelocity,-xvelocity)).The problem with these commands is that they determine the "speed" of the object move(Not their X Y Coordinates!). So by what Command I can give X & Y inputs and the object goes to those coordinates? thanks in advance.

esmaeil
  • 1
  • 2
  • 1
    You could retrieve the current position (with [pos()](https://doc.qt.io/qt-5/qgraphicsitem.html#pos)) and set the new position by adding `yvelocity` and `-xvelocity` to that respectively. – Scheff's Cat Jun 06 '20 at 08:07
  • @Scheff, How is this done? Can you explain with an example and write Commands? – esmaeil Jun 06 '20 at 08:31
  • Something like `setPos(pos() + mapToParent(yvelocity,-xvelocity))`. Although `pos()` returns [QPointF](https://doc.qt.io/qt-5/qpointf.html), you may apply the `operator+`. - There is a suitable overload. Are you familiar a bit with vector algebra? – Scheff's Cat Jun 06 '20 at 08:34
  • @Scheff, Yes, Thanks a lot, I will test this. – esmaeil Jun 06 '20 at 08:38
  • @Scheff, This is not possible.My Codes `QPointF location =this->pos(); xvelocity = .6*cos(head); yvelocity =.6*sin(head); setPos(location + mapToParent(yvelocity,-xvelocity));`with this codes output is: QPointF(3.37262,-2.16553) QPointF(7.04894,-4.19207) QPointF(7510.47,-4139.51) QPointF(961316,-529883) QPointF(1.92263e+06,-1.05977e+06) QPointF(7.8751e+09,-4.3408e+09) QPointF(1.57502e+10,-8.6816e+09) QPointF(3.15004e+10,-1.73632e+10) &........ – esmaeil Jun 06 '20 at 09:58
  • The `mapToParent()` is IMHO the wrong part in this. Sorry, I didn't think about this before but updated my answer respectively. – Scheff's Cat Jun 06 '20 at 10:30

1 Answers1

0

In my comment I gave the recommendation:

Something like setPos(pos() + mapToParent(yvelocity,-xvelocity)). Although pos() returns QPointF, you may apply the operator+. - There is a suitable overload.

While thinking twice, I struggled over mapToParent() which appears wrong to me.

According to the OP, the velocity describes a direction. The mapToParent() is dedicated to translate a local position to a position in parents coordinate system.

QPointF QGraphicsItem::mapToParent(const QPointF &point) const

Maps the point point, which is in this item's coordinate system, to its parent's coordinate system, and returns the mapped coordinate. If the item has no parent, point will be mapped to the scene's coordinate system.

So, the better recommendation might be:

setPos(pos() + QPointF(yvelocity,-xvelocity));

I made an MCVE testQGraphicsItemSetPos.cc to demonstrate this:

// Qt header:
#include <QtWidgets>

// main application
int main(int argc, char **argv)
{
  qDebug() << "Qt Version:" << QT_VERSION_STR;
  QApplication app(argc, argv);
  // setup data
  QGraphicsScene qGScene;
  QImage qImg("smiley.png");
  QGraphicsPixmapItem qGItemImg(QPixmap::fromImage(qImg));
  qGScene.addItem(&qGItemImg);
  const qreal v = 2.0;
  qreal xVel = 0.0, yVel = 0.0;
  // setup GUI
  QWidget qWinMain;
  qWinMain.resize(320, 200);
  qWinMain.setWindowTitle("QGraphicsView - Move Item");
  QVBoxLayout qVBox;
  QGraphicsView qGView;
  qGView.setScene(&qGScene);
  qVBox.addWidget(&qGView, 1);
  qWinMain.setLayout(&qVBox);
  qWinMain.show();
  // timer for periodic update
  using namespace std::chrono_literals;
  QTime qTime(0, 0);
  QTimer qTimerAnim;
  qTimerAnim.setInterval(50ms);
  // install signal handlers
  QObject::connect(&qTimerAnim, &QTimer::timeout,
    [&]() {
      // change velocity xVel and yVel alternating in range [-1, 1]
      const qreal t = 0.001 * qTime.elapsed(); // t in seconds
      xVel = v * std::sin(t); yVel = v * std::cos(t);
      // apply current velocity to move item
      qGItemImg.setPos(qGItemImg.pos() + QPointF(xVel, yVel));
    });
  // runtime loop
  qTimerAnim.start();
  return app.exec();
}

and a CMakeLists.txt:

project(QGraphicsItemSetPos)

cmake_minimum_required(VERSION 3.10.0)

set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

find_package(Qt5Widgets CONFIG REQUIRED)

include_directories("${CMAKE_SOURCE_DIR}")

add_executable(testQGraphicsItemSetPos testQGraphicsItemSetPos.cc)

target_link_libraries(testQGraphicsItemSetPos Qt5::Widgets)

with which I built and started the sample in VS2017:

snapshot of testQGraphicsItemSetPos.exe (animated)

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56