PDA

View Full Version : fillRect does not display anything?



Ishmael
30th September 2009, 00:02
Ok, I'm missing something simple here. For some reason, QPainter isn't displaying anything. I thought this code should simply fill the background of the scene with blue. Any ideas? This is the entirety of my code:


#include <QtGui>

int main(int argc, char **argv){
QApplication app(argc, argv);
QGraphicsView view;
QGraphicsScene scene;
view.setScene(&scene);

QPainter painter;

QRectF sceneRect = scene.sceneRect();
painter.fillRect(sceneRect, Qt::blue);

view.show();
return app.exec();
}

yogeshgokul
30th September 2009, 06:25
You should use QPainter in PaintEvent only.
Here you can use scene.setBackgroundBrush(Qt::blue);
So now your code will look like :


#include <QtGui>
int main(int argc, char **argv){
QApplication app(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
scene.setBackgroundBrush(Qt::blue);
view.show();
return app.exec();
}

Ishmael
30th September 2009, 19:33
Thanks for the response. I think I oversimplified my example. Actually I'd like to be able to paint a filled rectangle anywhere on the screen, not necessarily just the background. What should I use instead of QPainter to do this?

wysota
30th September 2009, 19:59
You can't paint "anywhere on the screen" (meaning the desktop). You can only paint on widgets.

Ishmael
30th September 2009, 20:28
Oops, sorry. I meant to say "anywhere on the SCENE" (not SCREEN). Is this possible? I'd like to be able to superimpose simple drawings on the scene - it seemed like QPainter was supposed to do exactly that.

For that matter, maybe you have a better solution for my specific current problem. I'm trying to fill in the rectangle that contains a character (QSimpleTextItem - thanks wysota!). I am able to get a rectangle to appear around the item by doing the following
QRectF r = item->sceneBoundingRect();
scene->addRect(r);
but what I'd really like to do is display a filled rectangle. How do I do this?

wysota
30th September 2009, 21:03
The scene should be composed from items, so use items and forget drawing on the scene directly. It's possible but I don't think this is something you really want.