PDA

View Full Version : GraphicsView Foreground



augusbas
23rd June 2011, 11:15
Hi all,

I have sub-classed graphics view and i have implemented "draw foreground" function ..I need to draw ellipse's on the view foreground...Provided my code for reference .

Header File:


#include <QGraphicsView>

class MyView: public QGraphicsView
{
Q_OBJECT

public:

MyView(QWidget *parent = 0);

protected:

void drawForeground(QPainter * painter, const QRectF & rect);


};


Source File:



#include <QtGui>
#include <QX11Info>

#include "graphics.h"


MyView::MyView(QWidget *parent) : QGraphicsView(parent)

{


//--- Graphics View Properties

this->resize ( 768, 768 );



this->setStyleSheet("background-image: url(/aegean-120km.png)");

/*

QGraphicsScene scene;

scene.setSceneRect (0,0, 768, 768 );

this->setScene ( &scene );

*/

QColor color(Qt::yellow);

QBrush brush(color, Qt::SolidPattern);

this->setForegroundBrush(brush);


this->show();




}


void MyView::drawForeground(QPainter * painter, const QRectF & rect)
{

QColor color1(156, 189, 176,254);

painter->setPen(Qt::lightGray);

int x,y,w,h,i;

double centre_x=384, centre_y=384;

double step_size=38.5;

for(i=1;i<=20 ;i++)
{
x = centre_x - step_size * i ;
y = centre_y - step_size * i ;

w=h=step_size * i * 2;

painter->drawEllipse(x,y,w,h);
}

}




I didn't get any ellipse on the foreground ....By ref to manual Whether "draw foreground " function will be applicable only to QGraphicsScene ? how the draw foreground function will get called, automatically or any update mechanism need to be added in the code ...

Please advise..

Rachol
23rd June 2011, 11:51
Did you try this: viewport()->update(); ?

And yet remove this part:

#
QBrush brush(color, Qt::SolidPattern);

this->setForegroundBrush(brush);#
QBrush brush(color, Qt::SolidPattern);

this->setForegroundBrush(brush);

augusbas
23rd June 2011, 12:00
Did you try this: viewport()->update(); ?

I have tried the method suggested , it didnt have any impact

viewport()->update()

Any ideas?

mcosta
23rd June 2011, 12:04
Have you tried to draw backgroung using backgroundBrush and not using Stylesheet?

stampede
23rd June 2011, 12:08
Try calling base-class implementation QGraphicsView::drawForeground(painter,rect) after your drawing code.

Rachol
23rd June 2011, 12:08
I don't know if you have noticed, but I have modified my previous message. Remove this->setForegroundBrush(brush); and I am quite sure it will work.

augusbas
23rd June 2011, 12:23
I have tried the follwing suggested by you people:

Call the base class implementation after drawing code .....QGraphicsView::drawForeground(painter,rect) ...... Didnt get any drawing ouptut on graphics view....

I have removed this "QBrush brush(color, Qt::SolidPattern); this->setForegroundBrush(brush);" .....

Didnt get any output....


Have you tried to draw backgroung using backgroundBrush and not using Stylesheet?

I haven't tried this ....Whether this will have an impact ?

Rachol
23rd June 2011, 12:24
Might do something with your problem

mcosta
23rd June 2011, 12:26
I tried this code



Widget::Widget(QWidget *parent)
: QGraphicsView(parent)
{
QPixmap px (":/images/bg.png");
this->setBackgroundBrush (QBrush(px));
}

Widget::~Widget()
{

}

void Widget::drawForeground(QPainter *painter, const QRectF &rect)
{
// QColor color1(156, 189, 176,254);

painter->setPen(Qt::darkRed);

double centre_x=384, centre_y=384;

double step_size=38.5;

int x, y, h, w;

for(int i=1;i<=20 ;i++) {
x = centre_x - step_size * i ;
y = centre_y - step_size * i ;

w=h=step_size * i * 2;

painter->drawEllipse(x,y,w,h);
}

}

This is the result
6603

augusbas
23rd June 2011, 12:52
Might do something with your problem

Hi ,

Thanks to your support ...

Is my code is right completely ? Any changes need to be done....

Added after 5 minutes:


I tried this code



Widget::Widget(QWidget *parent)
: QGraphicsView(parent)
{
QPixmap px (":/images/bg.png");
this->setBackgroundBrush (QBrush(px));
}

Widget::~Widget()
{

}

void Widget::drawForeground(QPainter *painter, const QRectF &rect)
{
// QColor color1(156, 189, 176,254);

painter->setPen(Qt::darkRed);

double centre_x=384, centre_y=384;

double step_size=38.5;

int x, y, h, w;

for(int i=1;i<=20 ;i++) {
x = centre_x - step_size * i ;
y = centre_y - step_size * i ;

w=h=step_size * i * 2;

painter->drawEllipse(x,y,w,h);
}

}

This is the result
6603



hi mcosta,

surprise.....

I have tried the same ......

set the background brush and used the drawforeground......

I didnt get any circle and image too....

With stylesheet i could set the image and it is visible, but by background brush its not visible......

How it can be........

Are you drawing in a graphics Scene ......


If you don't mind can i have your complete source code of main and header and i will try it in my system....

Added after 13 minutes:

hi mcosta,

I have tried the code of yours too ....i didnt get any output.......

Any problem specific to system or code ....

mcosta
23rd June 2011, 12:53
Hi, my code use a Resource File

gg.qrc


<RCC>
<qresource prefix="/images">
<file>bg.png</file>
</qresource>
</RCC>


This is my .pro


#-------------------------------------------------
#
# Project created by QtCreator 2011-06-23T12:06:26
#
#-------------------------------------------------

QT += core gui

TARGET = gg
TEMPLATE = app


SOURCES += main.cpp\
Widget.cpp

HEADERS += Widget.h

OTHER_FILES += \
bg.png

RESOURCES += \
gg.qrc


and my main


#include <QtGui/QApplication>
#include <QtGui/QGraphicsScene>

#include "Widget.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

Q_INIT_RESOURCE (gg);

QGraphicsScene scene;
scene.setSceneRect (0, 0, 768, 768);

Widget w;
w.resize (768, 768);
w.setScene (&scene);

w.show();

return a.exec();
}

augusbas
23rd June 2011, 13:06
Hi mcosta,

I tried your code and found working fine.....

Its the problem with the Qgraphicsscene ....i.e drawforeground applies to the graphics scene ....

Thanks to your support dude..

if i need to set the foreground fully opaque ... I need to set through painter ?

One more thing i found when a image is added in back ground , the complete view is having the replica of images in the background ...how can i have only one time a image in view...