tfarmer4
27th July 2010, 00:41
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:
//link.h
class Node;
#ifndef LINK_H
#define LINK_H
#include "diagramwindow.h"
#include "Node.h"
using namespace Qt;
class Link : public QGraphicsLineItem{
public:
Link(Node *fromNode, Node *toNode){
myFromNode = fromNode;
myToNode = toNode;
myFromNode->addLink(this);
myToNode->addLink(this);
setColor(Qt::black);
}
~Link(){
myFromNode->removeLink(this);
myToNode->removeLink(this);
}
void setColor(const QColor &color){
setPen(QPen(color,1.0));
}
QColor color()const;
void trackNodes(){
setLine(QLineF(myFromNode->pos(), myToNode->pos()));
}
private:
Node *myFromNode;
Node *myToNode;
};
#endif // LINK_H
//Node.h
#ifndef NODE_H
#define NODE_H
#include <QGraphicsItem>
#include "Link.h"
#include <QFontMetricsF>
#include <QSet>
#include <QString>
#include <QRectF>
#include <QApplication>
using namespace Qt;
class Link;
class Node : public QGraphicsItem{
public:
Node(){
myTextColor = Qt::black;
myOutlineColor = Qt::black;
myBackgroundColor = Qt::white;
}
void setText(const QString &text){
prepareGeometryChange();
myText = text;
update();
}
void setTextColor(const QColor &color){
myTextColor = color;
update();
}
void addLink(Link *link){
myLinks.insert(link);
}
void removeLink(Link *link){
myLinks.remove(link);
}
QRectF outlineRect()const{
const int Padding = 8;
QFontMetricsF metrics = qApp->font();
QRectF rect = metrics.boundingRect(myText);
rect.adjust(-Padding, -Padding, +Padding, +Padding);
rect.translate(-rect.center());
return rect;
}
QRectF boundingRect()const{
const int Margin = 1;
return outlineRect().adjusted(-Margin, -Margin, +Margin, +Margin);
}
QPainterPath shape()const{
QRectF rect = outlineRect();
QPainterPath path;
path.addRoundRect(rect, roundness(rect.width()),
roundness(rect.height()));
return path;
}
void paint(QPainter *painter,
QWidget * /* widget*/){
QPen pen(myOutlineColor);
painter->setPen(pen);
painter->setBrush(myBackgroundColor);
QRectF rect = outlineRect();
painter->drawRoundRect(rect, roundness(rect.width()),
roundness(rect.height()));
painter->setPen(myTextColor);
painter->drawText(rect, Qt::AlignCenter, myText);
}
private:
int roundness(double size)const{
const int DIAMETER = 12;
return 100*DIAMETER/int(size);
}
QSet<Link*>myLinks;
QString myText;
QColor myBackgroundColor;
QColor myOutlineColor;
QColor myTextColor;
};
#endif // NODE_H
//diagramwindow.h
#ifndef DIAGRAMWINDOW_H
#define DIAGRAMWINDOW_H
#include <QObject>
#include <QMainWindow>
#include <QGraphicsView>
#include <QGraphicsScene>
#include "Link.h"
class Node;
class List;
namespace Ui {
class DiagramWindow;
}
class DiagramWindow : public QMainWindow
{
Q_OBJECT
public:
explicit DiagramWindow();
~DiagramWindow();
private slots:
void addNode(){
Node *node;
node->setText(QObject::tr("Node %1").arg(seqNumber +1));
setupNode(node);
}
void addLink(Node *nodeA, Node *nodeB);
private:
Ui::DiagramWindow *ui;
QGraphicsScene *scene;
QGraphicsView *view;
int minZ;
int maxZ;
int seqNumber;
void setupNode(Node *node){
node->setPos(QPoint(80+ (100 * (seqNumber % 5)), (80+ (50 * ((seqNumber/5)%7)))));
scene->addItem(node);
++seqNumber;
}
};
#endif // DIAGRAMWINDOW_H
//diagramwindow.cpp
#include "diagramwindow.h"
#include "ui_diagramwindow.h"
#include "Node.h"
#include "Link.h"
void DiagramWindow::addLink(Node *nodeA, Node *nodeB){
Link *link = new Link(nodeA, nodeB);
scene->addItem(link);
}
DiagramWindow::DiagramWindow() :
QMainWindow(),
ui(new Ui::DiagramWindow)
{
DiagramWindow window;
scene = new QGraphicsScene(0,0,600,500);
view = new QGraphicsView;
view->setScene(scene);
view->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
view->setContextMenuPolicy(Qt::ActionsContextMenu);
setCentralWidget(view);
minZ = 0;
maxZ = 0;
seqNumber = 0;
setWindowTitle("Proteins Diagram");
ui->setupUi(this);
window.addNode();
}
DiagramWindow::~DiagramWindow()
{
delete ui;
}
//main.cpp
#include <QtGui/QApplication>
#include "diagramwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
DiagramWindow w;
w.show();
w.update();
return a.exec();
}
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.
//link.h
class Node;
#ifndef LINK_H
#define LINK_H
#include "diagramwindow.h"
#include "Node.h"
using namespace Qt;
class Link : public QGraphicsLineItem{
public:
Link(Node *fromNode, Node *toNode){
myFromNode = fromNode;
myToNode = toNode;
myFromNode->addLink(this);
myToNode->addLink(this);
setColor(Qt::black);
}
~Link(){
myFromNode->removeLink(this);
myToNode->removeLink(this);
}
void setColor(const QColor &color){
setPen(QPen(color,1.0));
}
QColor color()const;
void trackNodes(){
setLine(QLineF(myFromNode->pos(), myToNode->pos()));
}
private:
Node *myFromNode;
Node *myToNode;
};
#endif // LINK_H
//Node.h
#ifndef NODE_H
#define NODE_H
#include <QGraphicsItem>
#include "Link.h"
#include <QFontMetricsF>
#include <QSet>
#include <QString>
#include <QRectF>
#include <QApplication>
using namespace Qt;
class Link;
class Node : public QGraphicsItem{
public:
Node(){
myTextColor = Qt::black;
myOutlineColor = Qt::black;
myBackgroundColor = Qt::white;
}
void setText(const QString &text){
prepareGeometryChange();
myText = text;
update();
}
void setTextColor(const QColor &color){
myTextColor = color;
update();
}
void addLink(Link *link){
myLinks.insert(link);
}
void removeLink(Link *link){
myLinks.remove(link);
}
QRectF outlineRect()const{
const int Padding = 8;
QFontMetricsF metrics = qApp->font();
QRectF rect = metrics.boundingRect(myText);
rect.adjust(-Padding, -Padding, +Padding, +Padding);
rect.translate(-rect.center());
return rect;
}
QRectF boundingRect()const{
const int Margin = 1;
return outlineRect().adjusted(-Margin, -Margin, +Margin, +Margin);
}
QPainterPath shape()const{
QRectF rect = outlineRect();
QPainterPath path;
path.addRoundRect(rect, roundness(rect.width()),
roundness(rect.height()));
return path;
}
void paint(QPainter *painter,
QWidget * /* widget*/){
QPen pen(myOutlineColor);
painter->setPen(pen);
painter->setBrush(myBackgroundColor);
QRectF rect = outlineRect();
painter->drawRoundRect(rect, roundness(rect.width()),
roundness(rect.height()));
painter->setPen(myTextColor);
painter->drawText(rect, Qt::AlignCenter, myText);
}
private:
int roundness(double size)const{
const int DIAMETER = 12;
return 100*DIAMETER/int(size);
}
QSet<Link*>myLinks;
QString myText;
QColor myBackgroundColor;
QColor myOutlineColor;
QColor myTextColor;
};
#endif // NODE_H
//diagramwindow.h
#ifndef DIAGRAMWINDOW_H
#define DIAGRAMWINDOW_H
#include <QObject>
#include <QMainWindow>
#include <QGraphicsView>
#include <QGraphicsScene>
#include "Link.h"
class Node;
class List;
namespace Ui {
class DiagramWindow;
}
class DiagramWindow : public QMainWindow
{
Q_OBJECT
public:
explicit DiagramWindow();
~DiagramWindow();
private slots:
void addNode(){
Node *node;
node->setText(QObject::tr("Node %1").arg(seqNumber +1));
setupNode(node);
}
void addLink(Node *nodeA, Node *nodeB);
private:
Ui::DiagramWindow *ui;
QGraphicsScene *scene;
QGraphicsView *view;
int minZ;
int maxZ;
int seqNumber;
void setupNode(Node *node){
node->setPos(QPoint(80+ (100 * (seqNumber % 5)), (80+ (50 * ((seqNumber/5)%7)))));
scene->addItem(node);
++seqNumber;
}
};
#endif // DIAGRAMWINDOW_H
//diagramwindow.cpp
#include "diagramwindow.h"
#include "ui_diagramwindow.h"
#include "Node.h"
#include "Link.h"
void DiagramWindow::addLink(Node *nodeA, Node *nodeB){
Link *link = new Link(nodeA, nodeB);
scene->addItem(link);
}
DiagramWindow::DiagramWindow() :
QMainWindow(),
ui(new Ui::DiagramWindow)
{
DiagramWindow window;
scene = new QGraphicsScene(0,0,600,500);
view = new QGraphicsView;
view->setScene(scene);
view->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
view->setContextMenuPolicy(Qt::ActionsContextMenu);
setCentralWidget(view);
minZ = 0;
maxZ = 0;
seqNumber = 0;
setWindowTitle("Proteins Diagram");
ui->setupUi(this);
window.addNode();
}
DiagramWindow::~DiagramWindow()
{
delete ui;
}
//main.cpp
#include <QtGui/QApplication>
#include "diagramwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
DiagramWindow w;
w.show();
w.update();
return a.exec();
}
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.