Results 1 to 4 of 4

Thread: Positioning QGraphicsWidget

  1. #1
    Join Date
    Jun 2007
    Posts
    7
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Positioning QGraphicsWidget

    I've recently started playing around with the Graphics View Framework.

    With this code:

    Qt Code:
    1. void ButtonWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. painter->setPen( Qt::NoPen );
    4. painter->setBrush( QBrush( QColor(255, 0, 0)) );
    5. painter->drawRoundedRect(0, 0, 50, 50, 25, 25);
    6. }
    To copy to clipboard, switch view to plain text mode 

    I am able to position an instance of ButtonWidget, using ButtonWidget::setPos(), anywhere exactly I want it to appear on the view (top-right corner for one).

    However, when I start drawing multiple shapes I can no longer position an instance of ButtonWidget properly.

    Here is the code:

    Qt Code:
    1. void ButtonWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. painter->setPen( Qt::NoPen );
    4. painter->setBrush( QBrush( QColor(255, 0, 0)) );
    5. painter->drawRoundedRect(-20, -20, 50, 50, 25, 25);
    6.  
    7. painter->setBrush( QBrush( QColor(0, 0, 255)) );
    8. painter->drawRoundedRect(-10, -10, 30, 30, 15, 15);
    9. }
    To copy to clipboard, switch view to plain text mode 

    With this I get the exact appearance of the widget as I want it to be, but no matter what I do I can't position it properly on the view.

    I've been reading the docs, tutorials and a couple of books over and over gain but because 2D drawing is so new to me I can't seem to figure out what I am doing wrong.

  2. #2
    Join Date
    Jun 2007
    Posts
    7
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Positioning QGraphicsWidget

    Ok, after waking up this morning and seeing no replies, I did some experiment.

    I did not positioned the ButtonWidget instance after adding it to the scene and the answer was right there on the screen!

  3. #3
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Positioning QGraphicsWidget

    What is your ButtonWidget::boundingRect() like?

    Remember that geometry is relative to position. And position is relative to parent item.

  4. #4
    Join Date
    Jun 2007
    Posts
    7
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Positioning QGraphicsWidget

    scascio, I have already solved this problem but I'll answer your question anyway.

    In the first sample code it is:

    Qt Code:
    1. QRectF ButtonWidget::boundingRect()
    2. {
    3. return QRectF(0, 0, 50, 50);
    4. }
    To copy to clipboard, switch view to plain text mode 

    In the second:

    Qt Code:
    1. QRectF ButtonWidget::boundingRect()
    2. {
    3. return QRectF(-20, -20, 50, 50);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Edit: spelling correction

Similar Threads

  1. QGraphicsRectItem positioning problem
    By gufeatza in forum Qt Programming
    Replies: 3
    Last Post: 5th September 2009, 01:24
  2. QGraphicsWidget - How does it work?
    By been_1990 in forum Qt Programming
    Replies: 2
    Last Post: 31st July 2009, 14:15
  3. QGraphicsWidget vs. QGraphicsRectItem
    By Bill in forum Newbie
    Replies: 3
    Last Post: 27th July 2009, 10:02
  4. QwebPage within a QGraphicsWidget
    By Osprey in forum Qt Programming
    Replies: 0
    Last Post: 16th June 2009, 18:32
  5. Replies: 1
    Last Post: 22nd October 2007, 03:04

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.