Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Moving a figure

    General and Desktop
    3
    8
    423
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      Demorald last edited by

      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?

      Pl45m4 1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        Hi
        You can use the
        https://doc.qt.io/qt-5/animation-overview.html
        to move it around on screen but do notice that QWidgets are not that good for animations but
        if kept small it works fine.

        1 Reply Last reply Reply Quote 2
        • Pl45m4
          Pl45m4 @Demorald last edited by Pl45m4

          @Demorald

          Create timer with X secs update invervall ( = movement speed) and add Y to x-pos every time your timer event happens

          @Demorald said in Moving a figure:

          smoothly

          Too short intervalls will produce a lot of re-drawing, too long intervalls will make your item "jump". So you'll need to test, what works best for you.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          D 1 Reply Last reply Reply Quote 0
          • D
            Demorald @Pl45m4 last edited by

            @Pl45m4

            I did this, here's the timer:

            Colours::Colours(QWidget *parent) :
                QDialog(parent),
                ui(new Ui::Colours)
            {
                ui->setupUi(this);
                QTimer *timer = new QTimer(this);
                connect(timer, SIGNAL(timeout()), this, SLOT(update()));
                timer->start(1000);
            }
            
            

            And redraw function:

            void Colours::redraw(int x)
            {
            
                QPainter painter(this);
                QRectF rect = QRectF(x, 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")));
            }
            

            I call this function from paintEvent and it does draw new triangles, but the previous ones aren't being removed, so i just get a line of triangles instead initial one moving. I guess i made a mistake in the timer and it does not update a widget, but i can't find out what to do

            mrjj Pl45m4 2 Replies Last reply Reply Quote 0
            • mrjj
              mrjj Lifetime Qt Champion @Demorald last edited by

              @Demorald
              Hi
              Well you can use
              painter.drawRect(rect());
              with a brush set to clear it.

              1 Reply Last reply Reply Quote 1
              • Pl45m4
                Pl45m4 @Demorald last edited by

                @Demorald

                Ahh... my bad... my suggestion works, if your triangle is a QWidget or QGraphicsItem, but you paint directly onto your canvas.

                So, yeah, re-paint the whole area first, as @mrjj said, and then paint your new updated triangle.


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                D 1 Reply Last reply Reply Quote 1
                • D
                  Demorald @Pl45m4 last edited by

                  @Pl45m4
                  @mrjj
                  I'm not exactly sure, where should i put it? I tried putting it instead of update() in the timer or in the beginning of the redraw function, and it didn't work.. Just nothing happened in the timer case and in the function one the whole widget just became black. Seems like something in the logic has broken.

                  
                  void Colours::redraw(int x)
                  {
                      QPainter painter(this);
                  
                      painter.setBrush(QBrush("D2691E"));
                  
                      painter.drawRect(rect());
                  
                      QRectF rect = QRectF(x, 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")));
                  }
                  
                  mrjj 1 Reply Last reply Reply Quote 0
                  • mrjj
                    mrjj Lifetime Qt Champion @Demorald last edited by

                    @Demorald
                    That seems a good place. however, i think your color for the brush misses a #
                    so its become invalid and hence black.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post