title: Qt: What's the best way to swap QGraphicsRectItem positions
body: <hr>
  
  <p>I'm creating a visualization of sorting algorithms.
  I've got a vector of <code>QGraphicsRectItems</code> which are columns with different height and the same width, generated on the app start. They are being shuffled and then added to a <code>QGraphicsScene</code>.
  Class named "algorithms" sort a vector of float values which are used to set a height of columns. On swap - it emits a signal to the main class with two integers, so it looks like this:</p>
  
  <pre><code>emit comparison(array[first element to swap], array[second element to swap]);
  </code></pre>
  
  <p>Function(on_comparison) in the main class is connected with that signal. The problem appeared when I was trying to swap these 2 elements. I've created a variable to set <code>n</code> column position to it. After that I was trying to setPos of columns so I did something like:</p>
  
  <pre><code>void on_comparision(int n, int k)
  {
     auto nColumnPos = columns[n]-&gt;pos().x();
     columns[n]-&gt;setX(columns[k]-&gt;pos().x());
     columns[k]-&gt;setX(nColumnPos);
  }
  </code></pre>
  
  <p>But it doesn't work. Positions are not changing.
  Furthermore</p>
  
  <pre><code>qDebug() &lt;&lt;nColumnPos; 
  </code></pre>
  
  <p>shows value = 0.</p>
  
  <p>I was wondering if my whole program works so I decided to implement 2 sorting algorithms which swap 2 near each other elements and modified <code>on_comparison</code> function to</p>
  
  <pre><code>columns[n]-&gt;setX(columns[n].pos().x() + columnsWidth);
  columns[n]-&gt;setX(columns[n].pos().x() - columnsWidth);
  </code></pre>
  
  <p>It works but doesn't give a satisfying result. This function will work only with sorting algorithms that swap 2 near each other elements except eg. Bubblesort/cocktailsort.</p>
  
  <p>I was searching for the answer but didn't find anything helpful.</p>
  
  <hr>
  
  <p>Here's the code:
  <a href="https://gist.github.com/anonymous/e612e35a7c14eeb650099d4b997f437d" rel="nofollow noreferrer">https://gist.github.com/anonymous/e612e35a7c14eeb650099d4b997f437d</a></p>
  
  <hr>