Results 1 to 3 of 3

Thread: color and size!!

  1. #1
    Join Date
    Jan 2007
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default color and size!!

    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,


    Qt Code:
    1. MainWindow:: MainWindow( QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. scene = new QGraphicsScene;
    5.  
    6. mango = new Mango; //instance of QGraphicsItem
    7. apple= new Apple; //instance of QGraphicsItem
    8.  
    9. scene->addItem(mango);
    10. scene->addItem(apple);
    11.  
    12. view = new QGraphicsView(scene); //make sure always pass the QGraphicsScene
    13. view->setSceneRect(-400,-400,800,800);
    14.  
    15. QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
    16. mutateAct = new QAction(tr("&Mutate"),this);
    17. connect(mutateAct, SIGNAL(triggered()), this, SLOT(mutate()));
    18. fileMenu->addAction(mutateAct);
    19.  
    20. view->setRenderHint(QPainter::Antialiasing,true);
    21. setCentralWidget(view);
    22.  
    23. }
    24. void MainWindow::mutate()
    25. {
    26. //change the size and color of apple??
    27. //change the size and color of mango??
    28.  
    29.  
    30. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 13th February 2007 at 12:14. Reason: missing [code] tags

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: color and size!!

    You need to maintain some variables in the QGraphicsItem class which will represent the shape and color of the item
    say..
    Qt Code:
    1. class Apple :public QGraphicsItem
    2. { QColor m_color;
    3. QPainterPath m_path;
    4. void ChangeShape(QPainterPath path) // This function will set the shape of the item to painterpath.
    5. { prepareGeometryChange(); // This is necessary
    6. .... // set shape as per path passed
    7. }
    8. ..
    9. ..
    10. };
    To copy to clipboard, switch view to plain text mode 

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

    in the
    Qt Code:
    1. mainwindow::mutant()
    2. { apple->ChangeShape(shape); // shape is the shape u want...
    3. ...
    4. }
    To copy to clipboard, switch view to plain text mode 


    hope this helps

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: color and size!!

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.