PDA

View Full Version : color and size!!



rachana
13th February 2007, 12:07
Hi all,

How to change the color and size of QGraphicsItem? Can somebody explain how
to change the size and color of the items when mutate menu is clicked? here is the code,



MainWindow:: MainWindow( QWidget *parent)
: QMainWindow(parent)
{
scene = new QGraphicsScene;

mango = new Mango; //instance of QGraphicsItem
apple= new Apple; //instance of QGraphicsItem

scene->addItem(mango);
scene->addItem(apple);

view = new QGraphicsView(scene); //make sure always pass the QGraphicsScene
view->setSceneRect(-400,-400,800,800);

QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
mutateAct = new QAction(tr("&Mutate"),this);
connect(mutateAct, SIGNAL(triggered()), this, SLOT(mutate()));
fileMenu->addAction(mutateAct);

view->setRenderHint(QPainter::Antialiasing,true);
setCentralWidget(view);

}
void MainWindow::mutate()
{
//change the size and color of apple??
//change the size and color of mango??


}

aamer4yu
13th February 2007, 12:27
You need to maintain some variables in the QGraphicsItem class which will represent the shape and color of the item
say..

class Apple :public QGraphicsItem
{ QColor m_color;
QPainterPath m_path;
void ChangeShape(QPainterPath path) // This function will set the shape of the item to painterpath.
{ prepareGeometryChange(); // This is necessary
.... // set shape as per path passed
}
..
..
};

You need to override two methods of QGraphicsItem for this -
1) QPainterPath QGraphicsItem::shape ()
2) QRectF QGraphicsItem::boundingRect ()

in the

mainwindow::mutant()
{ apple->ChangeShape(shape); // shape is the shape u want...
...
}


hope this helps :)

wysota
13th February 2007, 13:17
You can use QGraphicsItem::data() and QGraphicsItem::setData() to keep data such as colour in your item. Then in the paint() method, just fetch the data and use it to initialise QPainter's pen and brush objects to obtain colours.