Update at(0)
-
Well (if) you are just messing around with a list of QRectF.
So nothing will change on screen.
If you kept a list of the QGraphicsRectItem you new, then
setRect() would work for those. -
Well, it does not work at all because at() returns
const T&
. You need a nonconst
accessor, which is most probably (you never specified the container you're using)operator[]
:rectan[ 0 ].setX( rectan.at( 1 ).x() ) ; rectan[ 0 ].setY( rectan.at( 1 ).y() ) ;
and so on...
-
@mrjj and can you tell me what's wrong with this code, I was trying to swap rectangles places it swaps only at the first time and then it goes crazy it swaps the way it wants
void MainWindow::Shot(int j){
QBrush greenBrush(Qt::green);
QPointF pos1_point;
QPointF pos2_point;
QRectF temp;
temp.setX(rectan.at(j).x());
temp.setY(rectan.at(j).y());
temp.setHeight(rectan.at(j).height());
temp.setWidth(rectan.at(j).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(-(pos2_point.x()-pos1_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());
rectan[j].setX(rectan.at(j+1).x());
rectan[j].setY(rectan.at(j+1).y());
rectan[j].setHeight(rectan.at(j+1).height());
rectan[j].setWidth(rectan.at(j+1).width());rectan[j+1].setX(temp.x());
rectan[j+1].setY(temp.y());
rectan[j+1].setHeight(temp.height());
rectan[j+1].setWidth(temp.width());}
-
"swaps the way it wants"
which is ?
Also is rectan of GraphicsItems ? -
@mrjj randomly. Rectan is QRectF and draw_rectan is QGraphicsRectItem.
Maybe the rectan update is wrong:rectan[j].setX(rectan.at(j+1).x());
rectan[j].setY(rectan.at(j+1).y());
rectan[j].setHeight(rectan.at(j+1).height());
rectan[j].setWidth(rectan.at(j+1).width());rectan[j+1].setX(temp.x());
rectan[j+1].setY(temp.y());
rectan[j+1].setHeight(temp.height());
rectan[j+1].setWidth(temp.width());I sort them together with the draw_rectan items
-
It does seems ok.
You should put the debugger on it and
check the rectan array after the swap. -
Hi!
Some general help:-
Create a list of pointers to QGraphicsRectItems:
QList<QGraphicsRectItem*> list;
-
Create a new QGraphicsRectItem:
QGraphicsRectItem *tmp = new QGraphicsRectItem(0, 0, 100, 50);
-
Add that to your list:
list.append(tmp);
-
Repeat steps 2 and 3 for all the rectangles you want to have.
-
Add all the rectangles to your scene:
for (int i=0; i<list.size(); i++) { QGraphicsRectItem *temp = list.at(i); scene->addItem(temp); }
- If you want to get the position and size of item number 0:
QRectF rect = list.at(0)->rect(); qreal x = rect.x(); qreal y = rect.y(); qreal width = rect.width(); qreal height = rect.height();
- If you want to set the position and size of item number 0:
QRectF rect(23,42,5,666); list.at(0)->setRect(rect);
-
-
@Wieland for example I have a List of QGraphicsRectItem and when I use the setPos
draw_rectan.at(j+1)->setPos( pos2_point.x()-pos1_point.x(),-draw_rectan.at(j+1)->y());
It will change the position of the rectangle, but will it change the data in the QList? or I have to swap it manually, I mean will the data be updated after I used the setPos func or not? -
The list,
QList<QGraphicsRectItem*>
, does not contain the QGraphicsRectItems themselves but pointers to these items. So, when you access an object that's in the list, say you do this:list.at(0)
, then you'll get a pointer. This is why you use the operator->
when you call setRect:list.at(0)->setRect(...)
. When you do that you don't change the actual content of the list because the actual content is just a handful of pointers. You don't change the pointers. You use the pointers to change the QGraphicsRectItems that these pointers point to. Once the actual objects change they will notify the scene they were added to.tl;dr : Yes, the scene gets updated.
-
@Wieland Ok, so I tried a lot of stuff and here is what I got, the rectangles are updating on the scene, but It jumps over a rectangle for instance: I'm implementing a bubble sort. It start with first and second elements, after it swapped this elements it swaps the other two elements whics can be swapped ignoring that swap whic was made before.
3 2 1 0
2 3 1 0
and then it does like this 2 3 0 1 but it has to be 2 1 3 0
What can cause this problem?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(3000*count, [=]{ MainWindow::Shot(j);}); } } }
void MainWindow::Shot(int j){
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);int shift_R = pos2_point.x()-pos1_point.x();
int shift_L = -shift_R;
draw_rectan.at(j)->setPos(shift_R,-draw_rectan.at(j)->y());
draw_rectan.at(j+1)->setPos(shift_L ,-draw_rectan.at(j+1)->y());draw_rectan[j]->update(temp2.x(),temp2.y(),temp2.width(),temp2.height());
draw_rectan[j+1]->update(temp1.x(),temp1.y(),temp1.width(),temp1.height());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());}
-
Let's put the visualization aspect aside for a minute. Does your sorting implementation work? I mean, if you have an array of numbers then does your program sort it correctly?
-
@Wieland The bubble algorithm is working I tested it. Now I'm checking is everything swaps correctly, cuz I think maybe I swap the numbers incorrectly and so it uses the old version of the list
-
Just print your whole array of numbers after each iteration and see if it does what it's supposed to do. You can also check wikipedia; it has pseudo code for bubble sort algorithms with different optimizations.
-
@Wieland how I can update the data in the QGraphicsRectItem list? Got it
-
I don't understand what you mean. What data exactly?