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. How to swap QGraphicsRectItems on scene(changing theirs position)

How to swap QGraphicsRectItems on scene(changing theirs position)

Scheduled Pinned Locked Moved Solved General and Desktop
qgraphicsrectitqgraphicssceneswapchange positionget position
4 Posts 2 Posters 1.7k Views 2 Watching
  • 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.
  • L Offline
    L Offline
    lete
    wrote on last edited by
    #1

    Hello,
    I'm creating a visualization of sorting algorithms. I've got a vector of QGraphicsRectItems which are columns with different height and the same width, generated on the app start. They are being shuffled and then added to a QGraphicsScene. Class named "algorithms" sort a vector of float values which are use to set a height of columns. On swap - it emits a signal to a main class with two integers, so it looks like this:

    emit comparision(array[first element to swap], array[second element to swap]);
    

    Function(on_comparison) in main class is connected with that signal. Problem appeared when I was trying to swap these 2 elements. I've created a variable to set a n column position to it. After that I was trying to setPos of columns so I did something like:

    void on_comparision(int n, int k)
    {
       auto nColumnPos = columns[n]->pos().x();
       columns[n]->setX(columns[k]->pos().x());
       columns[k]->setX(nColumnPos);
       std::swap(columns[n], columns[k]);
    }
    

    But it doesn't work. Positions are not changing. Furthermore

    qDebug() <<nColumnPos;
    

    shows value = 0.

    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 on_comparison function to

    columns[n]->setX(columns[n].pos().x() + columnsWidth);
    columns[n]->setX(columns[n].pos().x() - columnsWidth);
    std::swap(columns[n], columns[k]);
    

    It works but doesn't give satisfying result. This function will work only with sorting algorithms that check elements 1 after another eg. bubble sort/cocktail sort. It will not work fine with any other sorting algorithm like Merge sort, or Quicksort.

    I was searching for the answer but didn't find anything helpful.

    source code online:
    source code to download, with .pro, .ui and main.cpp files

    Thank you in advance.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      Hi and welcome to devnet,

      You didn't set the position of your items but only worked with their rectangle property so you can do something like:

      auto nRect = columns[n]->rect();
      auto kRect = columns[k]->rect();
      auto nColumnPos = nRect.left();
      nRect.moveLeft(kRect.left());
      kRect.moveLeft(nColumnPos);
      
      columns[n]->setRect(nRect);
      columns[k]->setRect(kRect);
      std::swap(columns[n], columns[k]);
      
      comparisions++;
      ui->LabelComparisions_var->setNum(comparisions);
      

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi and welcome to devnet,

        You didn't set the position of your items but only worked with their rectangle property so you can do something like:

        auto nRect = columns[n]->rect();
        auto kRect = columns[k]->rect();
        auto nColumnPos = nRect.left();
        nRect.moveLeft(kRect.left());
        kRect.moveLeft(nColumnPos);
        
        columns[n]->setRect(nRect);
        columns[k]->setRect(kRect);
        std::swap(columns[n], columns[k]);
        
        comparisions++;
        ui->LabelComparisions_var->setNum(comparisions);
        
        L Offline
        L Offline
        lete
        wrote on last edited by
        #3

        @SGaist Oh that seems pretty obvious, what a shame I did not think about it. Thank you so much for helping me.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You're welcome !

          Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          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