Results 1 to 4 of 4

Thread: QGraphicsView + MainWindow

  1. #1

    Default QGraphicsView + MainWindow

    How PixmapItem in MainWindow:
    main.cpp:
    Qt Code:
    1. #include <QApplication>
    2. #include "diagramwindow.h"
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication app(argc, argv);
    6. DiagramWindow view;
    7. view.show();
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 
    graphwidget.h:
    Qt Code:
    1. #include <QApplication>
    2. #include "diagramwindow.h"
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication app(argc, argv);
    6. DiagramWindow view;
    7. view.show();
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 
    graphwidget.cpp:
    Qt Code:
    1. #include "graphwidget.h"
    2. #include "node.h"
    3. #include <QtGui>
    4. GraphWidget::GraphWidget(QWidget *parent)
    5. : QGraphicsView(parent)
    6. {
    7. QGraphicsScene *scene = new QGraphicsScene(this);
    8. scene->setSceneRect(-200, -200, 400, 400);
    9. setScene(scene);
    10. setRenderHint(QPainter::Antialiasing);
    11. setWindowTitle(tr("Elastic Nodes"));
    12. Node *node4 = new Node(this);
    13. centerNode = new Node(this);
    14. Node *node6 = new Node(this);
    15. scene->addItem(node4);
    16. scene->addItem(centerNode);
    17. scene->addItem(node6);
    18. node4->setPos(-50, 0);
    19. centerNode->setPos(0, 0);
    20. node6->setPos(50, 0);
    21. }
    To copy to clipboard, switch view to plain text mode 
    node.h:
    Qt Code:
    1. #ifndef NODE_H
    2. #define NODE_H
    3. #include <QGraphicsPixmapItem>
    4. class GraphWidget;
    5. class Node : public QGraphicsPixmapItem
    6. {
    7. public:
    8. Node(GraphWidget *graphWidget);
    9. enum { Type = UserType + 1 };
    10. int type() const { return Type; }
    11. QRectF boundingRect() const;
    12. QPainterPath shape() const;
    13. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    14. private:
    15. GraphWidget *graph;
    16. };
    To copy to clipboard, switch view to plain text mode 
    node.cpp:
    Qt Code:
    1. #include <QGraphicsScene>
    2. #include <QGraphicsSceneMouseEvent>
    3. #include <QPainter>
    4. #include <QStyleOption>
    5. #include "node.h"
    6. #include "graphwidget.h"
    7. Node::Node(GraphWidget *graphWidget)
    8. : graph(graphWidget)
    9. {
    10. setFlag(ItemIsMovable);
    11. setFlag(ItemSendsGeometryChanges);
    12. }
    13. QRectF Node::boundingRect() const
    14. {
    15. return QRectF(-24,-24,48,48);
    16. }
    17. QPainterPath Node::shape() const
    18. {
    19. path.addRect(-24, -24, 48, 48);
    20. return path;
    21. }
    22. void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
    23. {
    24. QPixmap pixmap = QPixmap("Hub.png");
    25. painter->drawPixmap(-24,-24,pixmap);
    26. }
    To copy to clipboard, switch view to plain text mode 
    What can be?

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView + MainWindow

    What do you want? What do you see? What have you tried?

    Ask smart questions.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3

    Default Re: QGraphicsView + MainWindow

    I want show PixmapItem this Node this Hub.png on window MainWindow.

  4. #4

    Default Re: QGraphicsView + MainWindow

    Sorry. All works.

Similar Threads

  1. Replies: 1
    Last Post: 6th July 2010, 13:03
  2. How to put 2 Widgets in a MainWindow ?
    By gen_mass in forum Newbie
    Replies: 13
    Last Post: 1st July 2010, 13:01
  3. MainWindow
    By amagdy.ibrahim in forum Newbie
    Replies: 6
    Last Post: 14th June 2008, 15:12
  4. MainWindow mask
    By SirBabyface in forum Qt Programming
    Replies: 0
    Last Post: 11th July 2007, 16:12
  5. How do I use a mainwindow .ui file?
    By thomaspu in forum Qt Tools
    Replies: 2
    Last Post: 23rd November 2006, 07:32

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.