Results 1 to 5 of 5

Thread: My first program with QGraphicsView

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default My first program with QGraphicsView

    I think I store all my questions about QGraphicsView in this thread.

    First problem, I have a class Planet that is subclassedfrom GraphicsItem and I have this to draw it

    Qt Code:
    1. void Planet::paint(QPainter* painter, QStyleOptionGraphicsItem* option,QWidget* widget)
    2. {
    3. painter->setBrush(QColor(Qt::red));
    4. painter->drawEllipse(x,y,2*radius,2*radius);
    5. }
    To copy to clipboard, switch view to plain text mode 

    but all I get is a black circle on a white background. And the view isn't updated very much. How do I draw the ellipse and how do I update it more often?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: My first program with QGraphicsView

    There is QGraphicsEllipseItem which provides an GV ellipse item out of the box.
    You may then use QAbstractGraphicsShapeItem::setBrush() and QAbstractGraphicsShapeItem::setPen() to set brush and pen, respectively.
    J-P Nurmi

  3. #3
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My first program with QGraphicsView

    Quote Originally Posted by jpn
    There is QGraphicsEllipseItem which provides an GV ellipse item out of the box.
    You may then use QAbstractGraphicsShapeItem::setBrush() and QAbstractGraphicsShapeItem::setPen() to set brush and pen, respectively.
    So I tried:

    Qt Code:
    1. Planet::Planet()
    2. {
    3. radius = 31;
    4. x=rand()%500;
    5. y=rand()%500;
    6. setRect(x,y,2*radius,2*radius);
    7. brush =QBrush(QColor(45,221,182));
    8. pen = QPen(QColor(200,100,100));
    9. }
    10.  
    11. void Planet::paint(QPainter* painter, QStyleOptionGraphicsItem* option,QWidget* widget)
    12. {
    13. setBrush(brush);
    14. setPen(pen);
    15. painter->drawEllipse(x,y,2*radius,2*radius);
    16. }
    To copy to clipboard, switch view to plain text mode 

    but with the same result.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: My first program with QGraphicsView

    I meant that for an ellipse you don't need to implement your own QGraphicsItem, you can simply use a QGrapgicsEllipseItem instead:
    Qt Code:
    1. #include <QApplication>
    2. #include <QGraphicsView>
    3. #include <QGraphicsScene>
    4.  
    5. int main( int argc, char **argv )
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. QGraphicsScene scene(QRectF(0,0,100,100));
    10. scene.addEllipse(QRectF(25,40,50,20), QPen(Qt::green), QBrush(Qt::red));
    11. QGraphicsView view(&scene);
    12. view.show();
    13.  
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My first program with QGraphicsView

    With this constructor

    Qt Code:
    1. Planet::Planet() //:QGraphicsEllipseItem(QRect(0,0,0,0))
    2. {
    3. setBrush(QBrush(QColor(45,21,182)));
    4. setPen(QPen(QColor(200,10,10)));
    5. }
    To copy to clipboard, switch view to plain text mode 
    I get different colors, but it doesn't change the Brush if I add
    Qt Code:
    1. setBrush(QBrush(QColor(rand()%255,rand()%255,rand()%255)));
    To copy to clipboard, switch view to plain text mode 

    to the
    Qt Code:
    1. void Planet::paint(QPainter* painter, QStyleOptionGraphicsItem* option,QWidget* widget)
    To copy to clipboard, switch view to plain text mode 
    function.
    Strange.

Similar Threads

  1. how to embed an existing kde program into Konquerer
    By sephiroth_0 in forum KDE Forum
    Replies: 1
    Last Post: 15th August 2006, 12:21
  2. Release my simple program to other users ?
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 9th July 2006, 23:42
  3. why does qt program meet segmentation fault?
    By wquanw in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 15th May 2006, 16:52
  4. preparing program for release
    By impeteperry in forum Installation and Deployment
    Replies: 5
    Last Post: 22nd April 2006, 19:30
  5. Enter key causing program to exit
    By welby in forum Qt Programming
    Replies: 2
    Last Post: 9th March 2006, 16:11

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.