Beginner question about colouring the sceneRect()
-
Hello,
I am very new to QT and have gotten stuck on what I assume should be a simple problem.
I have a sceneRect() inside of which several object are animated.
my code is something like this:
@
scene = new QGraphicsScene(this);
.
scene->setSceneRect(-200, -200, 200, 200);
.
@I have several small objects moving around inside the sceneRect.
At this point I want to fill the sceneRect().
I have tried the following:@
QPainter painter(this);
QBrush bBrush(Qt::blue, Qt::SolidPattern);/* Fills the entire area inside and outside of the scenerect */
scene->setBackgroundBrush(bBrush);/* Doesn't appear to do anything but compiles fine */
painter.fillRect(scene->sceneRect(), bBrush); ///* Compiles without complaint but does nothing */
QPainterPath path;
path.addRect(scene->sceneRect());
painter.fillPath(path, bBrush);
@What am I doing wrong?
Thanks for any help.
-
Where do you put your painting code? Do you have a QGraphicsView on top of this scene?
Looks like you want to paint some background where all the other items are later painted on.
For this, have a look at QGraphicsScene::drawBackground() and QGraphicsView::drawBackground() methods. -
Thank you for replying,
The scene is sitting in a graphicsView but this class is just a dialog.
I am new to C++ as much as Qt so apologies if what I am about to ask is silly, but when I try to do something like:
@
QPainter myPainter(this);
scene->drawBackground(myPainter , scene->sceneRect());
@I get a no matching function error at compilation because
@virtual void drawBackground(QPainter *painter, const QRectF &rect); @
wants a pointer to the painter.
But If I try passing it
@
QPainter *myPainter;
scene->drawBackground(myPainter , scene->sceneRect());
@It complains because drawBackground() is protected.
Am I suppose to implement and entire sub-class just to fill the background?
-
Yes, GraphicsView/Scene::drawBackground() is protected. You'll need to subclass and implement the function you like. Alternatively you can also fill the background by setting the background brush:
http://qt-project.org/doc/qt-4.8/qgraphicsview.html#backgroundBrush-propBut if you need more than QBrush provides you're going to subclass QGraphicsView and to implement your own drawBackground() function.
I've just read your original post again and see that you've already started with scene->setBackgroundBrush() function :)
It should be sufficient just to set the brush via this function. No additional painter->fillRect() etc. is needed. Try to call this setBackgroundBrush() function on the view. If this doesn't help, please post your code here where your create a scene and a view. -
I am not using QGraphicsView for this at the moment. I am using just linking the scene to a GraphicsView from the designer.
ie:
@
/* Create a QGraphicsScene object to hold the scene */
scene = new QGraphicsScene(this);/* Link it to the graphicView UI object */
ui->graphicsView->setScene(scene);
@Then I set the sceneRect() and put four lines around it as a border.
if I call:
@scene->setBackgroundBrush(bgBrush);@as mentioned above, it does work, but it fills the entire window. I just want it to fill the box.
Are you saying I should be using something like:
@
QGraphicsView view(scene);
view.setRenderHint(QPainter::Antialiasing);
view.setBackgroundBrush(bgBrush);
view.show();
@When I started making this the last thing I thought would be an issue would be trying to colour in a box... everything else has been reasonably straight forward.
-
It behaves like scene->setBackgroundBrush(bgBrush);
That is, it fills the entire window out beyond the scenerect().
It's like it is completely ignoring the dimensions of the scenerect()
I should add, my GraphicsView is set to fill the form.ui it sits in.
I thought this wouldn't be a problem as I assumed the scene would take its dimensions from the scenerect() I defined in the code.
-
What do you mean by "beyond the scenrect"? It should fill out the entire view. The physical size of the view is mapped to the logical size of the scene that you have defined via setSceneRect. Do you expect to see a filled rectangular of the size 200x200 pixel?
-
The sizes you specify in setSceneRect is in scene's logical units. At the first step, they are not related to any "geometrical" units like points, mm etc. Now, when you create a view, the size of the view, which can be for example 10000x10000 pixels on your monitor, corresponds to size of the scene in scene's units, i.e. 400x400 in your example. There are helper functions to map from scene to view and the other way around.
Check out the documentation and the available examples for the graphics view framework:
http://qt-project.org/doc/qt-4.8/graphicsview.html