Hi,
sorry, I think you don't understand what I mean, it is not a problem with C++, but with Qt. Let me try to explain:
Originally Posted by
wysota
No. You are not supposed to call this method. You are supposed to write one. You will be given a painter and all that stuff.
I know, that I have to write a "new" drawBackground-Methode. So first I have to make a subclass of QGraphicsView with declare a drawBackground-Methode:
...
{
protected:
virtual void drawBackground(...);
...
}
class View : public QGraphicsView
...
{
protected:
virtual void drawBackground(...);
...
}
To copy to clipboard, switch view to plain text mode
Ok?
Then I have to write the method:
void drawBackground(...)
{
...
}
void drawBackground(...)
{
...
}
To copy to clipboard, switch view to plain text mode
Ok? Ok!
So, my question was: What have I to write in this NEW method? So, I don't know how the original drawBackground-method painted a Background, I took a look on the Qt-Source, especially on the source of QGraphicsView, of course!
Let me show the source of the original drawBackground-method of QGraphicsView:
/*!
Draws the background of the scene using \a painter, before any items and
the foreground are drawn. Reimplement this function to provide a custom
background for this view.
If all you want is to define a color, texture or gradient for the
background, you can call setBackgroundBrush() instead.
All painting is done in \e scene coordinates. \a rect is the exposed
rectangle.
The default implementation fills \a rect using the view's backgroundBrush.
If no such brush is defined (the default), the scene's drawBackground()
function is called instead.
\sa drawForeground(), QGraphicsScene::drawBackground()
*/
{
if (d->scene && d->backgroundBrush.style() == Qt::NoBrush) {
d->scene->drawBackground(painter, rect);
return;
}
painter->fillRect(rect, d->backgroundBrush);
}
/*!
Draws the background of the scene using \a painter, before any items and
the foreground are drawn. Reimplement this function to provide a custom
background for this view.
If all you want is to define a color, texture or gradient for the
background, you can call setBackgroundBrush() instead.
All painting is done in \e scene coordinates. \a rect is the exposed
rectangle.
The default implementation fills \a rect using the view's backgroundBrush.
If no such brush is defined (the default), the scene's drawBackground()
function is called instead.
\sa drawForeground(), QGraphicsScene::drawBackground()
*/
void QGraphicsView::drawBackground(QPainter *painter, const QRectF &rect)
{
Q_D(QGraphicsView);
if (d->scene && d->backgroundBrush.style() == Qt::NoBrush) {
d->scene->drawBackground(painter, rect);
return;
}
painter->fillRect(rect, d->backgroundBrush);
}
To copy to clipboard, switch view to plain text mode
Ok, this method gets a pointer on an instance of QPainter and a QRectF. Then the methode will either call the drawBackground-method of the scene or the fillRect-Method of the painter.
In the case the method of the scene is called, this source-code will runs:
/*!
Draws the background of the scene using \a painter, before any items and
the foreground are drawn. Reimplement this function to provide a custom
background for the scene.
All painting is done in \e scene coordinates. The \a rect
parameter is the exposed rectangle.
If all you want is to define a color, texture, or gradient for the
background, you can call setBackgroundBrush() instead.
\sa drawForeground(), drawItems()
*/
{
if (d->backgroundBrush.style() != Qt::NoBrush) {
if (d->painterStateProtection)
painter->save();
painter->setBrushOrigin(0, 0);
painter->fillRect(rect, backgroundBrush());
if (d->painterStateProtection)
painter->restore();
}
}
/*!
Draws the background of the scene using \a painter, before any items and
the foreground are drawn. Reimplement this function to provide a custom
background for the scene.
All painting is done in \e scene coordinates. The \a rect
parameter is the exposed rectangle.
If all you want is to define a color, texture, or gradient for the
background, you can call setBackgroundBrush() instead.
\sa drawForeground(), drawItems()
*/
void QGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect)
{
Q_D(QGraphicsScene);
if (d->backgroundBrush.style() != Qt::NoBrush) {
if (d->painterStateProtection)
painter->save();
painter->setBrushOrigin(0, 0);
painter->fillRect(rect, backgroundBrush());
if (d->painterStateProtection)
painter->restore();
}
}
To copy to clipboard, switch view to plain text mode
You can see: Both drawBackground-method use a QPainter. But which painter? Yes, I know that it's the painter that drawBackground of the view gets as an parameter, but...I don't know how I can explain what I mean
Ähm, it's difficult to explain my problem:
I dont know how QGraphicsView/Scene works to use a background. Is there a QPainter or so? It looks so, or?
Can you understand my problem now?
edit: Or an other idea: I have only to set an BackgroundBrush. And the drawBackground is only calling from the view/scene automatically, when the scene will be rendered?
And know I have only to write a drawBackground similiar to the original method, but I have to try that this method will paint the backgroundBrush only one time? How can I make that?
Bookmarks