Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Moving a figure
QtWS25 Last Chance

Moving a figure

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.1k Views
  • 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 Offline
    D Offline
    Demorald
    wrote on last edited by
    #1

    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?

    Pl45m4P 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      2
      • D Demorald

        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?

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #3

        @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
        0
        • Pl45m4P 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.

          D Offline
          D Offline
          Demorald
          wrote on last edited by
          #4

          @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

          mrjjM Pl45m4P 2 Replies Last reply
          0
          • D Demorald

            @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

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            1 Reply Last reply
            1
            • D Demorald

              @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

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by
              #6

              @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
              1
              • Pl45m4P Pl45m4

                @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.

                D Offline
                D Offline
                Demorald
                wrote on last edited by
                #7

                @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")));
                }
                
                mrjjM 1 Reply Last reply
                0
                • D Demorald

                  @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")));
                  }
                  
                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved