Results 1 to 5 of 5

Thread: Graphics Item

  1. #1
    Join Date
    Jul 2010
    Posts
    72
    Thanks
    35
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Graphics Item

    hello, i am trying to create a graphics item. I created a class called wierdo using the following code:

    #ifndef WIERDO_H
    #define WIERDO_H

    #include <QGraphicsItem>

    class Wierdo : public QGraphicsItem
    {

    public:
    QRectF boundingRect() const { return QRectF(0, 0,100, 100);};

    QPainterPath shape() const{
    QPainterPath path;
    path.addRect(-10, -20, 20, 40);
    return path;
    };

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
    painter->drawEllipse(-10, -20, 20, 40);
    painter->drawEllipse(-10, -20, 20, 40);

    };

    #endif // WIERDO_H

    The compiler does not accept the method painter->drawEllipse(-10, -20, 20, 40); saying that there is an "unexpected `(' token". Any suggestions on why this is happening?

    Thank you

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Graphics Item

    Quote Originally Posted by Maluko_Da_Tola View Post
    The compiler does not accept the method painter->drawEllipse(-10, -20, 20, 40); saying that there is an "unexpected `(' token". Any suggestions on why this is happening?
    It's like the compiler says to you: Your syntax is wrong! Basic C++...
    Qt Code:
    1. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
    2. {
    3. painter->drawEllipse(-10, -20, 20, 40);
    4. painter->drawEllipse(-10, -20, 20, 40);
    5. }
    To copy to clipboard, switch view to plain text mode 

    ..And you probably need to include the header for QPainter.

  3. #3
    Join Date
    Jul 2010
    Posts
    72
    Thanks
    35
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Graphics Item

    the only syntax error i can find is the missing parenthesis that enclose the method 'paint'. However, the compiler still display the same error (i.e. that the '' painter->drawEllipse(-10, -20, 20, 40);'' has an unexpected `(' token)!
    I have now the following code:

    #ifndef WIERDO_H
    #define WIERDO_H

    #include <QGraphicsItem>

    class Wierdo : public QGraphicsItem
    {

    public:
    QRectF boundingRect() const { return QRectF(0, 0,100, 100);};

    QPainterPath shape() const{
    QPainterPath path;
    path.addRect(-10, -20, 20, 40);
    return path;
    };

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
    {
    painter->drawEllipse(-10, -20, 20, 40);
    painter->drawEllipse(-10, -20, 20, 40);
    };

    };

    #endif // WIERDO_H


    The syntax I used for ''painter->drawEllipse(-10, -20, 20, 40);'' was taken from the colliding mice example. Any suggestions on why this is happening?

    Thank you

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Graphics Item

    there is no problem with the drawEllipse function call. The problem is your semicolons! remove them. (only after } of course) Only the class ending } has a semicolon!

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Graphics Item

    and compare your code
    Quote Originally Posted by Maluko_Da_Tola View Post
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
    {
    painter->drawEllipse(-10, -20, 20, 40);
    painter->drawEllipse(-10, -20, 20, 40);
    };
    with mine:
    Qt Code:
    1. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
    2. {
    3. painter->drawEllipse(-10, -20, 20, 40);
    4. painter->drawEllipse(-10, -20, 20, 40);
    5. }
    To copy to clipboard, switch view to plain text mode 
    .

    Also note I am using the [CODE] tags which formats the source code in a nicer way...

Similar Threads

  1. Serialization and graphics item
    By prashant in forum Qt Programming
    Replies: 1
    Last Post: 12th November 2009, 11:03
  2. How do you embed a widget in a graphics item?
    By technoViking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 06:09
  3. how to you send signals to a graphics item?
    By technoViking in forum Qt Programming
    Replies: 4
    Last Post: 6th November 2009, 19:40
  4. Graphics item position after drag
    By Miihkali in forum Qt Programming
    Replies: 3
    Last Post: 17th March 2009, 10:01
  5. Reimplementing The MousePressEvent for Graphics Item
    By ashishrai in forum Qt Programming
    Replies: 3
    Last Post: 25th May 2008, 20: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.