0

I created a triangle:


QPainter painter(this);

  painter.setRenderHint(QPainter::Antialiasing);
  painter.setPen(QColor("#d4d4d4"));

QRectF rect = QRectF(100, 50, 300, 100);



  QPainterPath path;
  painter.setBrush(QBrush("#D2691E"));
  path.moveTo(rect.left() + (rect.width()/2), rect.top());
  path.lineTo(rect.bottomLeft());
  path.lineTo(rect.bottomRight());
  path.lineTo(rect.left() + (rect.width() / 2), rect.top());

  painter.fillPath(path, QBrush(QColor ("#D2691E")));

Now I want it to move smoothly on the x coordinate. How can I make it?

  • You cannot move a figure. You have to repaint it with varying x coordinates. Use a `QTimer` to trigger a periodic repaint (calling `QWidget::update();`. Set x dependent of time (or number of repeated calls). – Scheff's Cat Sep 06 '20 at 14:40
  • FYI: [Qt doc. - 2D Painting Example](https://doc.qt.io/qt-5/qtopengl-2dpainting-example.html) - In fact, it doesn't change the coordinates of drawn stuff but the overall transformation of the `QPainter`. ;-) – Scheff's Cat Sep 06 '20 at 14:44
  • What class is this code part of? – scopchanov Sep 06 '20 at 15:56
  • I recommend to keep backend and frontend apart here. Have backend classes like some sort of map and some sort of entities on the map with coordinates and a frontend class which renders the scene, that is completely redraws it on demand, which does nothing of the backend logic, only drawing (and possibly catching mouse input). In such an arrangement, you'd have a backend process that moves the triangle and then calls the render function, or the render function being called in regular intervals. – Aziuth Sep 06 '20 at 16:30

0 Answers0