PDA

View Full Version : QGraphicsView Problem !!



Gamalof
13th June 2008, 18:54
i'm making a project, one of its parts in to control the size and place of any shape using sliders.

what i have got till now is, The shape's size is increasing when the slider's value increases,
the Problem is when decreasing the value of the slider, Nothing happen to the shape.

here is wt i did




// MYSlot.h
public:
int GVar;
QGraphicsScene scene;
QGraphicsView * view;

public slots:
void Slider_ValueChanged();

private:
Ui_MyForm ui;


--------------------------------------------



// MySlot.cpp

// The Constructor

MySlot::MySlot(QWidget *parent)
: QWidget(parent)
{
Ui_MyForm d;
MySlot::GVar=1;
//scene = new QGraphicsScene;
view = new QGraphicsView(&(MySlot::scene));
ui.setupUi(this);
}


----------------------------------------

Slider Value Changed Slot.



void MySlot::Slider_ValueChanged()
{
GVar= (int)(ui.horizontalSlider->value());
MySlot::scene.addEllipse (10, 10, GVar, 10,
QPen(Qt::black, 15, Qt::SolidLine, Qt::RoundCap,
Qt::MiterJoin),
QBrush(Qt::blue, Qt::DiagCrossPattern));

MySlot::scene.setBackgroundBrush(Qt::red);
MySlot::view->setAttribute(Qt::WA_DeleteOnClose);
CustomSlot::view ->show();

}


Again, The Problem is .. When the slider's value increases, the shape's size increases, when the slider's value decreases, Nothing happen to the shape.

Thanks :)

Brandybuck
13th June 2008, 19:40
Each time your slot is called, you're creating a new ellipse. Instead, you will want to change the size of an existing item, then call update() to repaint it.

Gamalof
13th June 2008, 20:03
Actually i used update function, but nothing happened !!

here is the Slot with updated function.



GVar= (int)(ui.horizontalSlider->value());

MySlot::scene.addEllipse (10, 10, GVar, GVar,
QPen(Qt::black, 15, Qt::SolidLine, Qt::RoundCap,
Qt::MiterJoin),
QBrush(Qt::blue, Qt::DiagCrossPattern));

MySlot::scene.setBackgroundBrush(Qt::red);

MySlot::view->update();

MySlot::view->setAttribute(Qt::WA_DeleteOnClose);
MySlot::view ->show();
}

Gamalof
14th June 2008, 14:55
I Got My incredible error :| , i was drawing circle"s" is the QSlider's value changed slot.

which gave me this bad result - many objects in the memory plus something like shadow appears - i solved it simply by drawing only one circle by this check :-



if (MySlot::MyE == NULL)
{
MySlot::MyE = MySlot::scene.addEllipse (10, 10, 10, 10,
QPen(Qt::red),
QBrush(Qt::white));
MySlot::scene.setBackgroundBrush(Qt::red);
}


then added the code which controls the size and location of the circle



MySlot::MyE->setRect(GVar3, GVar2, GVar, GVar );
MySlot::view->setAttribute(Qt::WA_DeleteOnClose);
MySlot::view ->show();
MySlot::view->update();


Thanks :)