Results 1 to 14 of 14

Thread: I need help understanding QGraphicsView

Hybrid View

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

    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.

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

    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.

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

    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

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

    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

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

    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.

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
  •  
Qt is a trademark of The Qt Company.