Incorrect setPos of rectangles
-
For 5 days i'm trying to swap the rectangles places in the Graphics View, but no luck it just swaps I don't know how, it has to be a bubble sort. Can someone check what am I doing wrong?
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
error = new QErrorMessage(this);
random_numbers_.clear();
user_input_numbers_.clear();for(int i=0;i<QUANTITY_RANDOM_NUM;i++){
random_numbers_.push_back(rand()%RANDOM_MAX);
}
scene = new QGraphicsScene(this);
ui->graphicsView_animation->setScene(scene);
ui->graphicsView_animation->setAlignment(Qt::AlignBottom | Qt::AlignRight | Qt::AlignLeft);
QBrush darkMagentaBrush(Qt::darkMagenta);
QBrush greenBrush(Qt::green);
QPen Pen(Qt::black);
Pen.setWidth(2);
for(int i=0;i<random_numbers_.size();i++){
QRectF temp;
temp.setRect((i+1)*25,-random_numbers_[i],20,random_numbers_.at(i));
rectan.append(temp);
}for(int i=0;i<random_numbers_.size();i++){ QGraphicsRectItem *tempi = new QGraphicsRectItem; tempi = scene->addRect(rectan.at(i),Pen,darkMagentaBrush); draw_rectan.append(tempi); }
t
QList <QPointF> h;
int count=0;
for( int i=0;i<random_numbers_.size()-1;i++){
for(int j=0;j<random_numbers_.size()-i-1;j++){if(random_numbers_[j]>random_numbers_[j+1]){ count++; QTimer::singleShot(1000*count, [=]{ MainWindow::Shot(j);}); random_numbers_[j] = random_numbers_[j] -random_numbers_[j+1]; random_numbers_[j+1] = random_numbers_[j+1] + random_numbers_[j]; random_numbers_[j] = random_numbers_[j+1] - random_numbers_[j]; } } }
void MainWindow::Shot(int j){
QPen Pen(Qt::black);
Pen.setWidth(2);
QBrush greenBrush(Qt::green);
QPointF pos1_point;
QPointF pos2_point;
QRectF temp1;
QRectF temp2;temp1.setX(rectan.at(j).x());
temp1.setY(rectan.at(j).y());
temp1.setHeight(rectan.at(j).height());
temp1.setWidth(rectan.at(j).width());temp2.setX(rectan.at(j+1).x());
temp2.setY(rectan.at(j+1).y());
temp2.setHeight(rectan.at(j+1).height());
temp2.setWidth(rectan.at(j+1).width());pos1_point.setX(rectan.at(j).x());
pos2_point.setX(rectan.at(j+1).x());//draw_rectan.at(j+1)->setBrush(greenBrush);
draw_rectan.at(j)->setBrush(greenBrush);draw_rectan.at(j)->setPos((-pos1_point.x())+pos2_point.x(),-draw_rectan.at(j)->y());
draw_rectan.at(j+1)->setPos(-(pos2_point.x())+pos1_point.x() ,-draw_rectan.at(j+1)->y());draw_rectan[j]->setX(-pos1_point.x()+pos2_point.x());
draw_rectan[j]->setY(-draw_rectan.at(j)->y());draw_rectan[j+1]->setX(-(pos2_point.x())+pos1_point.x());
draw_rectan[j+1]->setY(-draw_rectan.at(j+1)->y());//draw_rectan[j]->setX(shift_L);
//draw_rectan[j]->setY(-draw_rectan.at(j)->y());//draw_rectan[j+1]->setX(shift_R);
//draw_rectan[j+1]->setY(-draw_rectan.at(j+1)->y());rectan[j].setX(temp2.x());
rectan[j].setY(temp2.y());
rectan[j].setHeight(temp2.height());
rectan[j].setWidth(temp2.width());rectan[j+1].setX(temp1.x());
rectan[j+1].setY(temp1.y());
rectan[j+1].setHeight(temp1.height());
rectan[j+1].setWidth(temp1.width());}
}