Results 1 to 7 of 7

Thread: Help implementing graphics classes

  1. #1

    Angry Help implementing graphics classes

    I have the files that I have posted below. My question is, how do I begin building my graph in the QGraphicsScene? I'm just really confused on how to place nodes and links on the scene to be visualized. I'm very new to QT and am working on a graph project for computer science. Here is all my code:

    Qt Code:
    1. //link.h
    2. class Node;
    3. #ifndef LINK_H
    4. #define LINK_H
    5. #include "diagramwindow.h"
    6. #include "Node.h"
    7.  
    8. using namespace Qt;
    9.  
    10. class Link : public QGraphicsLineItem{
    11. public:
    12. Link(Node *fromNode, Node *toNode){
    13. myFromNode = fromNode;
    14. myToNode = toNode;
    15.  
    16. myFromNode->addLink(this);
    17. myToNode->addLink(this);
    18.  
    19. setColor(Qt::black);
    20. }
    21. ~Link(){
    22. myFromNode->removeLink(this);
    23. myToNode->removeLink(this);
    24. }
    25.  
    26. void setColor(const QColor &color){
    27. setPen(QPen(color,1.0));
    28. }
    29. QColor color()const;
    30.  
    31. void trackNodes(){
    32. setLine(QLineF(myFromNode->pos(), myToNode->pos()));
    33. }
    34.  
    35. private:
    36. Node *myFromNode;
    37. Node *myToNode;
    38. };
    39. #endif // LINK_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //Node.h
    2. #ifndef NODE_H
    3. #define NODE_H
    4. #include <QGraphicsItem>
    5. #include "Link.h"
    6. #include <QFontMetricsF>
    7. #include <QSet>
    8. #include <QString>
    9. #include <QRectF>
    10. #include <QApplication>
    11. using namespace Qt;
    12. class Link;
    13. class Node : public QGraphicsItem{
    14.  
    15. public:
    16. Node(){
    17. myTextColor = Qt::black;
    18. myOutlineColor = Qt::black;
    19. myBackgroundColor = Qt::white;
    20. }
    21.  
    22. void setText(const QString &text){
    23. prepareGeometryChange();
    24. myText = text;
    25. update();
    26. }
    27. void setTextColor(const QColor &color){
    28. myTextColor = color;
    29. update();
    30. }
    31. void addLink(Link *link){
    32. myLinks.insert(link);
    33. }
    34. void removeLink(Link *link){
    35. myLinks.remove(link);
    36. }
    37. QRectF outlineRect()const{
    38. const int Padding = 8;
    39. QFontMetricsF metrics = qApp->font();
    40. QRectF rect = metrics.boundingRect(myText);
    41. rect.adjust(-Padding, -Padding, +Padding, +Padding);
    42. rect.translate(-rect.center());
    43. return rect;
    44. }
    45. QRectF boundingRect()const{
    46. const int Margin = 1;
    47. return outlineRect().adjusted(-Margin, -Margin, +Margin, +Margin);
    48. }
    49. QPainterPath shape()const{
    50. QRectF rect = outlineRect();
    51.  
    52. path.addRoundRect(rect, roundness(rect.width()),
    53. roundness(rect.height()));
    54. return path;
    55. }
    56. void paint(QPainter *painter,
    57. QWidget * /* widget*/){
    58. QPen pen(myOutlineColor);
    59. painter->setPen(pen);
    60. painter->setBrush(myBackgroundColor);
    61.  
    62. QRectF rect = outlineRect();
    63. painter->drawRoundRect(rect, roundness(rect.width()),
    64. roundness(rect.height()));
    65. painter->setPen(myTextColor);
    66. painter->drawText(rect, Qt::AlignCenter, myText);
    67. }
    68. private:
    69. int roundness(double size)const{
    70. const int DIAMETER = 12;
    71. return 100*DIAMETER/int(size);
    72. }
    73.  
    74. QSet<Link*>myLinks;
    75. QString myText;
    76. QColor myBackgroundColor;
    77. QColor myOutlineColor;
    78. QColor myTextColor;
    79. };
    80. #endif // NODE_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //diagramwindow.h
    2. #ifndef DIAGRAMWINDOW_H
    3. #define DIAGRAMWINDOW_H
    4. #include <QObject>
    5. #include <QMainWindow>
    6. #include <QGraphicsView>
    7. #include <QGraphicsScene>
    8. #include "Link.h"
    9. class Node;
    10. class List;
    11. namespace Ui {
    12. class DiagramWindow;
    13. }
    14.  
    15. class DiagramWindow : public QMainWindow
    16. {
    17. Q_OBJECT
    18.  
    19. public:
    20. explicit DiagramWindow();
    21. ~DiagramWindow();
    22. private slots:
    23. void addNode(){
    24. Node *node;
    25. node->setText(QObject::tr("Node %1").arg(seqNumber +1));
    26. setupNode(node);
    27. }
    28.  
    29. void addLink(Node *nodeA, Node *nodeB);
    30.  
    31. private:
    32. Ui::DiagramWindow *ui;
    33. int minZ;
    34. int maxZ;
    35. int seqNumber;
    36. void setupNode(Node *node){
    37. node->setPos(QPoint(80+ (100 * (seqNumber % 5)), (80+ (50 * ((seqNumber/5)%7)))));
    38. scene->addItem(node);
    39. ++seqNumber;
    40. }
    41. };
    42.  
    43. #endif // DIAGRAMWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //diagramwindow.cpp
    2. #include "diagramwindow.h"
    3. #include "ui_diagramwindow.h"
    4. #include "Node.h"
    5. #include "Link.h"
    6.  
    7. void DiagramWindow::addLink(Node *nodeA, Node *nodeB){
    8. Link *link = new Link(nodeA, nodeB);
    9. scene->addItem(link);
    10. }
    11. DiagramWindow::DiagramWindow() :
    12. ui(new Ui::DiagramWindow)
    13. {
    14. DiagramWindow window;
    15.  
    16. scene = new QGraphicsScene(0,0,600,500);
    17.  
    18. view = new QGraphicsView;
    19. view->setScene(scene);
    20. view->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    21. view->setContextMenuPolicy(Qt::ActionsContextMenu);
    22. setCentralWidget(view);
    23. minZ = 0;
    24. maxZ = 0;
    25. seqNumber = 0;
    26. setWindowTitle("Proteins Diagram");
    27.  
    28. ui->setupUi(this);
    29. window.addNode();
    30. }
    31.  
    32. DiagramWindow::~DiagramWindow()
    33. {
    34. delete ui;
    35. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //main.cpp
    2. #include <QtGui/QApplication>
    3. #include "diagramwindow.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. DiagramWindow w;
    9. w.show();
    10. w.update();
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    Thanks for all your help. I understand how to write these class, as you can see I wrote these, I'm just confused on how to put it all on the scene, or whatever I need to do.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Help implementing graphics classes

    Qt Code:
    1. Node *node;
    2. node->setText(QObject::tr("Node %1").arg(seqNumber +1));
    To copy to clipboard, switch view to plain text mode 

    This is wrong and will crash. You need to initialise node first.
    Node *node = new Node;

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Help implementing graphics classes

    Your classes bear more than a passing resemblance to those in C+ GUI Programming with Qt4:
    http://www.informit.com/articles/art...74421&seqNum=4

    Your QGraphicsView should be part of the Designer UI, or you should abandon the Designer UI. As it stands you are creating a QGraphicsView, setting it as the central widget of your main window, and then trashing it with the Designer UI (through setupUi() in diagramwindow.cpp).

  4. #4

    Default Re: Help implementing graphics classes

    Quote Originally Posted by tbscope View Post
    Qt Code:
    1. Node *node;
    2. node->setText(QObject::tr("Node %1").arg(seqNumber +1));
    To copy to clipboard, switch view to plain text mode 

    This is wrong and will crash. You need to initialise node first.
    Node *node = new Node;
    If i try to initialize *node, I get an error that says invalid use of incomplete type Node.

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Help implementing graphics classes

    Quote Originally Posted by tfarmer4 View Post
    If i try to initialize *node, I get an error that says invalid use of incomplete type Node.
    That is because in that header file you have only a forward declaration of class Node, move the definition of addNode() in the cpp file (diagramwindow.cpp) in there you have included the Node.h, so you can create objects, because the compiler knows how your objects look like.

  6. #6

    Default Re: Help implementing graphics classes

    Quote Originally Posted by ChrisW67 View Post
    Your QGraphicsView should be part of the Designer UI, or you should abandon the Designer UI. As it stands you are creating a QGraphicsView, setting it as the central widget of your main window, and then trashing it with the Designer UI (through setupUi() in diagramwindow.cpp).
    Ok, I see. So how would I make QGraphicsView part of the Designer UI?

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Help implementing graphics classes

    So how would I make QGraphicsView part of the Designer UI?
    Just like any other widget. Open the UI file in Designer then drag a QGraphicsView from the "Display Widgets" group onto the layout, name it, save and build. You will need to adjust your code to use the QGraphicsView generated for you rather than creating it yourself.

Similar Threads

  1. Implementing threads...
    By Abc in forum Qt Programming
    Replies: 7
    Last Post: 12th June 2008, 08:41
  2. implementing drawForeGround
    By Wirloff in forum Newbie
    Replies: 9
    Last Post: 11th April 2007, 16:03
  3. Implementing paint()
    By Wirloff in forum Qt Programming
    Replies: 3
    Last Post: 10th April 2007, 10:29
  4. Implementing IME using QT
    By pmohod in forum Qt Programming
    Replies: 0
    Last Post: 16th February 2007, 12:22

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.