PDA

View Full Version : GraphicsView rotate problem



forrestfsu
16th October 2006, 21:13
I currently have an image displayed in the graphics view with the following function:

void Form::setImage(std::string filename)
{
QPixmap image(filename.c_str());
image = image.transformed(QMatrix().rotate(-90), Qt::FastTransformation);
ui.graphicsView->setBackgroundBrush(image);
}


That seems to work fine (it takes the original image and rotates it 90 degrees cclockwise). So here's the problem, when I use the following function, to rotate the image further, it rotates the image but leaves the original image in the background. I went through the 40000 chips example, and I don't necessarily see anything different with my code. If anyone knows why the original image is shown beneath the rotation, please let me know. Here is the function:

void Form::scaleView(qreal scaleFactor)
{
qreal scale = pow(2.0, (scaleFactor) );
QMatrix matrix;
matrix.scale(scale, scale);
matrix.rotate(levelRotate);
ui.graphicsView->setMatrix(matrix);
}

wysota
16th October 2006, 21:29
Could you provide a minimal compilable example which reproduces the problem?

forrestfsu
16th October 2006, 21:39
I'll go ahead and try to put something together. In the meantime this is what it looks like when I add the original image:
_________
|######|
|######|
|######|
|######|

Then After the rotate it looks like this:
_________
|##/#\ #|
|#/###\ |
|/### /#|
|#\ #/##|

I want it to look like this:
_________
| /# \ |
| /### \ |
|/### / |
|\## / |

Sorry for the garbage diagrams, its just that my development box is not on the internet and its a pain to transfer data between the two.

jacek
16th October 2006, 22:48
Isn't that the same problem as in this thread (http://www.qtcentre.org/forum/f-qt-programming-2/t-how-to-set-background-image-with-no-repeat-without-using-css-4055.html)?

forrestfsu
17th October 2006, 16:10
No, that thread was about setting a forms background to a specific image. The problem (in that thread) was that it would tile the image on the background, which was not the desired effect.

In regards to this thread, I have a form, which has a graphics view within it. When I add an image to the graphics view scene, it looks perfect. However, if I rotate the graphics view, it displays the image rotate, but tiled. I want it to only show the image rotated, without tiling.

I hope this clears it up.

jacek
17th October 2006, 18:35
No, that thread was about setting a forms background to a specific image.
Maybe, but QBrush is involved in both cases.


However, if I rotate the graphics view, it displays the image rotate, but tiled.
Are you sure there is no tiling before the rotation? Maybe you have to reimplement QGraphicsScene::drawBackground()?

forrestfsu
19th October 2006, 19:05
Thanks everyone! Jacek, when you said that it might be tiled prior to the rotation you got me thinking. You were right....I was using "setBackgroundBrush(image)"...and it seems that by default that tiles the image across the background. I figured out a way to get my code to work, and here it is if anybody ever has a similar problem:



void Form::setImage(QPixmap image)
{
// Setup scene
scene = new QGraphicsScene;
scene->setSceneRect(0,0,512,640);
scene->setItemIndexMethod(QGraphics::NoIndex);
scene->setBackgroundBrush(Qt::black);
// Add pixmap to scene
scene->addPixmap(image);
// Display image on graphicsView
ui.graphicsView->setScene(&*scene);
ui.graphicsView->viewport()->repaint();
}

bjh
21st November 2007, 20:20
Exactly the problem I was having, and a nice solution. THANKS!


bjh