Results 1 to 14 of 14

Thread: I need help understanding QGraphicsView

  1. #1
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default I need help understanding QGraphicsView

    Ok so here is some code I wrote to get started:
    Qt Code:
    1. scene = QGraphicsScene(0,0,100,100);
    2. ui->view->addScene(scene);
    3. QGraphicsRectItem *rect = new QGraphicsRectItem(0,0,50,50);
    4. scene->addItem(rect);
    To copy to clipboard, switch view to plain text mode 

    The other parts of the code are cosmetics. But can't, even though I've read the help, understand the relationship between the dimensions and where the things appear drawn in the QGraphicsView.

    The rectagle appears not in the middle of the screen, it's like one of the corners is in the middle of the screen, which makes no sense to me with the definition of the scene up above. What's more the size of the rectangle should be of about half the widget in both height and width and it's much smaller than that.

    Can any one explain this to me? or tell me where I can find a example simple enough that explains this? Most of the examples I find are complex enough that I can't find a simple piece of explained code that says "This Item is drawn here with this size with respect to the scene".

    Thanks for any help.

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: I need help understanding QGraphicsView

    Quote Originally Posted by aarelovich View Post
    Ok so here is some code I wrote to get started:
    The rectagle appears not in the middle of the screen, it's like one of the corners is in the middle of the screen, which makes no sense to me with the definition of the scene up above. What's more the size of the rectangle should be of about half the widget in both height and width and it's much smaller than that.


    Thanks for any help.
    first u understand coordinate systems in Qt
    http://doc.trolltech.com/4.3/coordsys.html

    u have to use setPos(x,y) of QGraphicsItem to position the rect in scene ..

    ur whole scene rect is (0, 0, 100, 100);
    item rect is (0,0,50, 50);
    so
    position it at (50, 50) in scene like
    Qt Code:
    1. rect->setPos(50, 50)
    To copy to clipboard, switch view to plain text mode 
    otherwise construct
    Qt Code:
    1. QGraphicsRectItem *rect = new QGraphicsRectItem(50,50,50,50);
    To copy to clipboard, switch view to plain text mode 

    playing with coordinats is very simple and interesting
    "Behind every great fortune lies a crime" - Balzac

  3. #3
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: I need help understanding QGraphicsView

    Hi:

    Yes this I understand however it does not answer my question. I was afraid I wasn't clear enough. What I don't understand is:

    1) Why when the I add the rectangle it appears in the middle of the screen when by my understanding it should appear in a corner? I know I can change the position with setPos but It's not very useful when you can't predict where it's going to appear.

    2) Why if the rectangle has a width of 50 it uses much less than half of the screen, which was set to 100? Again I know I can change the size, but it's useless If i can't understand what It does?


    Thanks for the reply and for any future help.

  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: I need help understanding QGraphicsView

    Quote Originally Posted by aarelovich View Post
    1) Why when the I add the rectangle it appears in the middle of the screen when by my understanding it should appear in a corner? I know I can change the position with setPos but It's not very useful when you can't predict where it's going to appear.
    See QGraphicsView::setAlignment(). the default is center and middle.

    2) Why if the rectangle has a width of 50 it uses much less than half of the screen, which was set to 100? Again I know I can change the size, but it's useless If i can't understand what It does?.
    I guess with "screen" you mean the view. And therefore: View != Scene. Can you post a screenshot?

  5. #5
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: I need help understanding QGraphicsView

    Hi I figured out what's been bugging me.

    See I used the the setSceneRect(0,0,100,100) to set the scene. What I didn't figure out what the it created the 100x100 scene in the middle of the QGraphicsViewport and everything (position and size was relative to that). Which now makes sense.

    So how did I solved it? I just did setSceneRect(0,0,this->width(),this->height()); Then every position and size got refered to the scene which coincided with the viewport (in this case I'm referring to the QGraphicsView widget as the viewport).

    Thanks for all the help.

  6. #6
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: I need help understanding QGraphicsView

    HI:

    So I'm still a little fuzzy about some details.
    As I understand things now, if I draw say one rectangle in a specific position and with a specific size and I want that rectangle to always maintain a specifc size relationship with the GraphicsView widget, I have (there is no other way) to reimplement the resizeEvent in my main window (the one that contains the GraphicsView) and redraw said rectangle. Is this right?

    If it isn't, could you provide some code of how it's supposed to be done

    Thanks for any help in advanced.

  7. #7
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: I need help understanding QGraphicsView

    see this Pad Navigator Example .. try to resize the window .. it will stretch ....
    inside panel.cpp see
    Qt Code:
    1. void Panel::resizeEvent(QResizeEvent *event)
    2. {
    3. QGraphicsView::resizeEvent(event);
    4. fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
    5. }
    To copy to clipboard, switch view to plain text mode 

    this one is responsible for the resize event ... this you can use it in any graphicsView .
    "Behind every great fortune lies a crime" - Balzac

  8. #8
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: I need help understanding QGraphicsView

    Ok.

    Thank you so very much. That was exactly the kind of code I was looking for. I'll try it out and get back to you.

  9. #9
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: I need help understanding QGraphicsView

    Ok, so my dimensioning and positioning problems were solved but now I'm trying to create my own customized QGraphicsItem and I've run into a bit of a problem.

    Heres is my code for my peg.h
    Qt Code:
    1. #ifndef PEG_H
    2. #define PEG_H
    3.  
    4. #include <QtGui>
    5. #include <QtCore>
    6.  
    7. class Peg : public QGraphicsItem
    8. {
    9. public:
    10. Peg(QColor color);
    11.  
    12. void paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 );
    13. QRectF boundingRect() const;
    14.  
    15. private:
    16. QColor color;
    17. };
    18.  
    19. #endif // PEG_H
    To copy to clipboard, switch view to plain text mode 

    and its corresponding peg.cpp

    Qt Code:
    1. #include "peg.h"
    2.  
    3. Peg::Peg(QColor col){
    4. color = col;
    5. setFlag(ItemIsMovable);
    6. }
    7.  
    8. QRectF Peg::boundingRect() const {
    9. return QRectF(-1,-1,12,12);
    10. }
    11.  
    12. void Peg::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    13. painter->setPen(QPen(Qt::black));
    14. QRadialGradient gradient(3,3,9);
    15. gradient.setColorAt(0.0,Qt::white);
    16. gradient.setColorAt(0.5,color.lighter(120));
    17. gradient.setColorAt(1.0,color);
    18. painter->setBrush(gradient);
    19. painter->drawEllipse(0,0,10,10);
    20. }
    To copy to clipboard, switch view to plain text mode 

    Then this is the code of the constructor of my mainwindow:

    Qt Code:
    1. Mastermind::Mastermind(QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::Mastermind)
    3. {
    4. ui->setupUi(this);
    5. scene = new QGraphicsScene(-50,-50,100,100);
    6. scene->setBackgroundBrush(QBrush(QColor(23,23,23)));
    7. ui->gvDrawArea->setScene(scene);
    8. ui->gvDrawArea->setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing);
    9. Peg peg(Qt::red);
    10. scene->addItem(&peg);
    11. }
    To copy to clipboard, switch view to plain text mode 
    The only problem is, I thought I was supposed to see a somewhat red circle somewhere on the screen yet nothing has appeared.

    What did I do wrong?

    Thanks for the help.

  10. #10
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: I need help understanding QGraphicsView

    use peg.setPos(x, y) to set that item in a particular location on scene ...

    increase the ellipse size a little ... its very tiny on a big scene ...hahaha ..
    "Behind every great fortune lies a crime" - Balzac

  11. #11
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: I need help understanding QGraphicsView

    your code is completed ... see your item in the attachment ...

    actually u need to inherit QObject with your custom QGraphicsItem ...
    that is the problem ... now it solved ... also use setPos() in QGraphicsScene()..

    Qt Code:
    1. class Peg : public QObject, public QGraphicsRectItem
    2. {
    3.  
    4. Q_OBJECT //if u need any connect in future
    To copy to clipboard, switch view to plain text mode 

    have a good day
    Attached Images Attached Images
    "Behind every great fortune lies a crime" - Balzac

  12. #12
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: I need help understanding QGraphicsView

    Hi:

    So thanks for the help. But even though I did what you told me it still did not appear. But I figured out the problem and it did not have anything to do with QGraphicsView Framework and everything to do with C++.

    I chaged this code

    Qt Code:
    1. Peg peg(Qt::red);
    2. scene->addItem(&peg);
    To copy to clipboard, switch view to plain text mode 

    with this code
    Qt Code:
    1. Peg *peg = new Peg(Qt::red);
    2. scene->addItem(peg);
    To copy to clipboard, switch view to plain text mode 

    It seems that by creating the local variable peg instead of a pointer it gets destroy at the end of the constructor therefore, it never shows up in the screen.

    Thanks for all the help, I'll probably be posting more of my problems.

  13. #13
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: I need help understanding QGraphicsView

    HI:

    So another day another problem that I don't understand. I'm doing the whole drag the peg into a box thing. Looking at the examples I found that it was a nice idea to use the close and open hand during the dragging movement. Here is the code for the peg class:
    Qt Code:
    1. #include "peg.h"
    2.  
    3. Peg::Peg(QColor col){
    4. color = col;
    5. ScaleFactor = 1;
    6. }
    7.  
    8. QRectF Peg::boundingRect() const {
    9. double Margin = 2;
    10. return QRectF(-Margin,-Margin,10*ScaleFactor + 2*Margin,10*ScaleFactor + 2*Margin);
    11. }
    12.  
    13. void Peg::setMoveable(bool move){
    14. setFlag(ItemIsMovable,move);
    15. }
    16.  
    17. void Peg::setScaleFactor(double rad){
    18. ScaleFactor = rad;
    19. }
    20.  
    21. void Peg::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    22. painter->setPen(QPen(Qt::black));
    23. QRadialGradient gradient(3,3,9);
    24. gradient.setColorAt(0.0,Qt::white);
    25. gradient.setColorAt(0.7,color.lighter(120));
    26. gradient.setColorAt(1.0,color);
    27. painter->setBrush(gradient);
    28. painter->scale(ScaleFactor,ScaleFactor);
    29. painter->drawEllipse(0,0,10,10);
    30. }
    31.  
    32. void Peg::mousePressEvent(QGraphicsSceneMouseEvent *event){
    33. if (event->button() != Qt::LeftButton){
    34. event->ignore();
    35. return;
    36. }
    37. setCursor(Qt::ClosedHandCursor);
    38. }
    39.  
    40.  
    41. void Peg::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
    42. setCursor(Qt::OpenHandCursor);
    43. }
    To copy to clipboard, switch view to plain text mode 

    I added the thing and when I click it the hand effectively closes but a soon as I start to move the peg, the peg inmediately appears in coordinates (0,0) of the scene and it starts moving from there (mantaining the distance from where the item originally lied to the (0,0)), Other than that it actually moves and it stays where the item is when I let go of the mouse button (which is a ways off from where the close hand cursor is).

    Again, I ask,anyone got any idea of what am I doing wrong?

    Thanks for any help.

  14. #14
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: I need help understanding QGraphicsView

    It's me again with another problem solved and whole new one.

    The last problem was solved by calling QGraphicsItem::mouseReleaseEvent(event); and
    QGraphicsItem::mousePressEvent(event); in the respective redefined events.

    However now I've got a new problem. This is one that has apparently confused many before. I want to drag and drop a my own created Peg GraphicItem into a square also created by me. Here is the code I've done so far:

    peg.cpp
    Qt Code:
    1. Peg::Peg(QColor col){
    2. color = col;
    3. ScaleFactor = 1;
    4. }
    5.  
    6. QRectF Peg::boundingRect() const {
    7. double Margin = 2;
    8. return QRectF(-Margin,-Margin,10*ScaleFactor + 2*Margin,10*ScaleFactor + 2*Margin);
    9. }
    10.  
    11. void Peg::setMoveable(bool move){
    12. setFlag(ItemIsMovable,move);
    13. }
    14.  
    15. void Peg::setScaleFactor(double rad){
    16. ScaleFactor = rad;
    17. }
    18.  
    19. void Peg::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    20. painter->setPen(QPen(Qt::black));
    21. QRadialGradient gradient(3,3,9);
    22. gradient.setColorAt(0.0,Qt::white);
    23. gradient.setColorAt(0.7,color.lighter(120));
    24. gradient.setColorAt(1.0,color);
    25. painter->setBrush(gradient);
    26. painter->scale(ScaleFactor,ScaleFactor);
    27. painter->drawEllipse(0,0,10,10);
    28. }
    29.  
    30.  
    31. void Peg::mousePressEvent(QGraphicsSceneMouseEvent *event){
    32. if (event->button() != Qt::LeftButton){
    33. event->ignore();
    34. return;
    35. }
    36. setCursor(Qt::ClosedHandCursor);
    37. QDrag *drag = new QDrag(event->widget()); \\Problem Code
    38. QMimeData *mime = new QMimeData(); \\Problem Code
    39. mime->setColorData(color); \\Problem Code
    40. drag->setMimeData(mime); \\Problem Code
    41. drag->exec(); \\Problem Code
    42. QGraphicsItem::mousePressEvent(event);
    43. }
    44.  
    45. void Peg::mouseMoveEvent(QGraphicsSceneMouseEvent *event){
    46. QGraphicsItem::mouseMoveEvent(event);
    47. }
    48.  
    49. void Peg::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
    50. setCursor(Qt::OpenHandCursor);
    51. QGraphicsItem::mouseReleaseEvent(event);
    52. }
    To copy to clipboard, switch view to plain text mode 

    then there is the square.cpp

    Qt Code:
    1. Square::Square(int w, int h){
    2. Width = w;
    3. Height = h;
    4. setZValue(-1);
    5. setAcceptDrops(true);
    6. }
    7.  
    8. QRectF Square::boundingRect() const{
    9. return QRectF(0,0,50,50);
    10. }
    11.  
    12. void Square::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    13. painter->setBrush(QBrush(Qt::darkBlue));
    14. painter->drawRect(0,0,50,50);
    15. }
    16.  
    17. void Square::dropEvent(QGraphicsSceneDragDropEvent *event){
    18. qDebug() << "I've got something";
    19. event->acceptProposedAction();
    20. }
    To copy to clipboard, switch view to plain text mode 

    Now when I added code marked with "\\Problem Code" I finally got the box to accept the drop event (otherwise it wasn't triggered) but now I can't move the peg itself. Rather, a small box forms over the cursor (indicating the draggin action) and dissapears when I let go, however the peg stays in the exact same spot. Is there anyway for this to work properly or do I have to implement some sort of routine that draws the peg under the mouse?

    Thanks for any help.

Similar Threads

  1. QGraphicsView performance
    By Halabund in forum Newbie
    Replies: 3
    Last Post: 17th April 2009, 11:12
  2. Replies: 0
    Last Post: 5th March 2009, 07:54
  3. QGraphicsView and embeded widgets
    By bunjee in forum Qt Programming
    Replies: 10
    Last Post: 12th October 2008, 08:43
  4. QGraphicsView
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 26th July 2007, 09:00
  5. Speed, transparency, and drop issues with QGraphicsView
    By jefferai in forum Qt Programming
    Replies: 16
    Last Post: 30th June 2007, 17:14

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.