so I have this MainWindow program.... I have multiple graphicsView windows open for a multitude of displays....
most importantly though, I have a QGraphicsItem custom class that paints individual line drawings...... I looked at the QtDemo code for graphicsView and have been through the code for the dragDropRobot, & the elasticNodes program.
Both of these are great examples of getting a GraphicsView Scene going, but the scenes are always the entire application.
How do I assign my custom class to an existing graphicsScene? one that is declared in a form... I know it has to do with how my class is intialized. This seems like a question that has an easy answer, but I am not seeing it... Oh worse, I am drawing QPainter also straight to the desktop also... (this is working fine). The graphicsScene I want to use the class instances as graphicsItems that can handle a hovermouse event, also is doing an intro screen which is just one picture, until I clear the scene and add the small class instances & setPos on them. The header and class I wrote just isn't getting passed the proper stuff and the classId error doesn't help me too much!!!!!!

here's my main.cpp
Qt Code:
  1. #include <QtGui/QApplication>
  2. #include "mainwindow.h"
  3. #include <QDesktopWidget>
  4. #include <QApplication>
  5.  
  6. void center(QWidget &widget)
  7. {
  8. int wx, wy;
  9. int screenWidth;
  10. int screenHeight;
  11.  
  12. int WIDTH = 770;
  13. int HEIGHT = 660;
  14.  
  15.  
  16. QDesktopWidget *desktop = QApplication::desktop();
  17.  
  18. screenWidth = desktop->width();
  19. screenHeight = desktop->height();
  20.  
  21. wx = (screenWidth - WIDTH) / 2;
  22. wy = (screenHeight - HEIGHT) / 2;
  23.  
  24. widget.setGeometry(wx, wy, WIDTH, HEIGHT);
  25. }
  26.  
  27.  
  28.  
  29.  
  30. int main(int argc, char *argv[])
  31. {
  32. QApplication a(argc, argv);
  33. MainWindow w;
  34.  
  35. w.setWindowTitle("Hex Inspector");
  36. w.show();
  37. center(w);
  38.  
  39. return a.exec();
  40. }
To copy to clipboard, switch view to plain text mode 



here's the node.h
Qt Code:
  1. #ifndef NODE_H
  2. #define NODE_H
  3.  
  4. #include <QGraphicsItem>
  5. #include <QList>
  6.  
  7.  
  8. class GraphWidget;
  9. QT_BEGIN_NAMESPACE
  10. QT_END_NAMESPACE
  11.  
  12. class Node : public QGraphicsItem
  13. {
  14. public:
  15. Node(QGraphicsItem *parent = 0, int altNum);
  16.  
  17. int type() const { return Type; }
  18.  
  19.  
  20. QRectF boundingRect() const;
  21. QPainterPath shape() const;
  22. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
  23.  
  24.  
  25.  
  26. protected:
  27.  
  28. void mousePressEvent(QGraphicsSceneMouseEvent *event);
  29. void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
  30.  
  31. private:
  32.  
  33. int altN, x, y;
  34. QPointF newPos;
  35. GraphWidget *graph;
  36. };
  37.  
  38. #endif
To copy to clipboard, switch view to plain text mode 

here's the node.cpp i'm not really using the int AltNum passed into it at the moment.... it draws a few things, right now I set it to just a two line X.

Qt Code:
  1. #include <QGraphicsScene>
  2. #include <QGraphicsSceneMouseEvent>
  3. #include <QPainter>
  4. #include <QStyleOption>
  5.  
  6.  
  7. #include "node.h"
  8. #include "graphwidget.h"
  9.  
  10. Node::Node(GraphWidget *graphWidget, int altNum)
  11. : graph(graphWidget)
  12. {
  13. setFlag(ItemIsMovable);
  14. setFlag(ItemSendsGeometryChanges);
  15. setCacheMode(DeviceCoordinateCache);
  16. setZValue(-1);
  17. x = 0; y = 0;
  18. altN = altNum;
  19.  
  20.  
  21. }
  22.  
  23. QRectF Node::boundingRect() const
  24. {
  25. qreal adjust = 2;
  26. return QRectF(-10 - adjust, -10 - adjust,
  27. 23 + adjust, 23 + adjust);
  28. }
  29.  
  30. QPainterPath Node::shape() const
  31. {
  32. path.addRect(0, 0, 8, 16);
  33. return path;
  34. }
  35.  
  36. void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
  37. {
  38.  
  39. painter->setBrush(Qt::white);
  40.  
  41.  
  42. painter->setPen(QPen(Qt::white, 1, Qt::SolidLine));
  43.  
  44. painter->drawLine(0,0,8,16);
  45. painter->drawLine(8,0,0,16);
  46.  
  47.  
  48. // end of draw one item
  49.  
  50.  
  51.  
  52. }
  53.  
  54.  
  55. void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
  56. {
  57. update();
  58. QGraphicsItem::mousePressEvent(event);
  59. }
  60.  
  61. void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
  62. {
  63. update();
  64. QGraphicsItem::mouseReleaseEvent(event);
  65. }
To copy to clipboard, switch view to plain text mode 


now my problem lies in changing this, so that it will do all of this inside my ui->graphicsView_3
calling it from a void MainWindow:opulate() function.


graphwidget.h
Qt Code:
  1. #ifndef GRAPHWIDGET_H
  2. #define GRAPHWIDGET_H
  3.  
  4. #include <QtGui/QGraphicsView>
  5.  
  6. class Node;
  7.  
  8. class GraphWidget : public QGraphicsView
  9. {
  10. Q_OBJECT
  11.  
  12. public:
  13. GraphWidget();
  14.  
  15. void itemMoved();
  16.  
  17. protected:
  18. void keyPressEvent(QKeyEvent *event);
  19. void timerEvent(QTimerEvent *event);
  20. void wheelEvent(QWheelEvent *event);
  21. void drawBackground(QPainter *painter, const QRectF &rect);
  22.  
  23. void scaleView(qreal scaleFactor);
  24.  
  25. private:
  26. int timerId;
  27. Node *centerNode;
  28.  
  29. };
  30.  
  31. #endif
To copy to clipboard, switch view to plain text mode 


now this is where the whole thing goes wrong.... I don't need to do the graphicsScene this way with a QGraphicsScene *scene = new QGraphisScene(this);
but would like to get this GraphWidget to be the graphicsScene for ui->graphicsScene_3....... so set up everything in this class, and then call it from a MainWindow:opulate() function........
graphwidget.cpp
Qt Code:
  1. #include "graphwidget.h"
  2. #include "node.h"
  3.  
  4. #include <QDebug>
  5. #include <QGraphicsScene>
  6. #include <QWheelEvent>
  7.  
  8. #include <math.h>
  9.  
  10. GraphWidget::GraphWidget()
  11. : timerId(0)
  12. {
  13. QGraphicsScene *scene = new QGraphicsScene(this);
  14. scene->setItemIndexMethod(QGraphicsScene::NoIndex);
  15. scene->setSceneRect(-200, -200, 400, 400);
  16. setScene(scene);
  17. setCacheMode(CacheBackground);
  18. setViewportUpdateMode(BoundingRectViewportUpdate);
  19. // setRenderHint(QPainter::Antialiasing);
  20. setTransformationAnchor(AnchorUnderMouse);
  21. setResizeAnchor(AnchorViewCenter);
  22.  
  23.  
  24. int px = -100; int py = -100;
  25.  
  26. for (int i = 0; i < 256; i++)
  27. {
  28. Node *nodeALL = new Node(this, i);
  29. scene->addItem(nodeALL);
  30. nodeALL->setPos(px, py);
  31.  
  32. if (px < 148) px+=8;
  33. else { px = -100; py+=16; }
  34.  
  35. }
  36.  
  37.  
  38. scale(qreal(1.0), qreal(1.0));
  39. setMinimumSize(400, 400);
  40.  
  41. }
  42.  
  43. void GraphWidget::itemMoved()
  44. {
  45. if (!timerId)
  46. timerId = startTimer(1000 / 25);
  47. }
  48.  
  49. void GraphWidget::keyPressEvent(QKeyEvent *event)
  50. {
  51.  
  52.  
  53.  
  54. }
  55.  
  56. void GraphWidget::timerEvent(QTimerEvent *event)
  57. {
  58. Q_UNUSED(event);
  59.  
  60. QList<Node *> nodes;
  61. foreach (QGraphicsItem *item, scene()->items()) {
  62. if (Node *node = qgraphicsitem_cast<Node *>(item))
  63. nodes << node;
  64. }
  65.  
  66.  
  67. if (!itemsMoved) {
  68. killTimer(timerId);
  69. timerId = 0;
  70. }
  71. }
  72.  
  73. void GraphWidget::wheelEvent(QWheelEvent *event)
  74. {
  75. scaleView(pow((double)2, -event->delta() / 240.0));
  76. }
  77.  
  78. void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
  79. {
  80. Q_UNUSED(rect);
  81.  
  82. // Shadow
  83. QRectF sceneRect = this->sceneRect();
  84. QRectF rightShadow(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height());
  85. QRectF bottomShadow(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5);
  86. if (rightShadow.intersects(rect) || rightShadow.contains(rect))
  87. painter->fillRect(rightShadow, Qt::darkGray);
  88. if (bottomShadow.intersects(rect) || bottomShadow.contains(rect))
  89. painter->fillRect(bottomShadow, Qt::darkGray);
  90.  
  91. // Fill
  92. /* QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight());
  93.   gradient.setColorAt(0, Qt::black);
  94.   gradient.setColorAt(1, Qt::black);
  95.   */
  96.  
  97. // painter->fillRect(rect.intersect(sceneRect), gradient);
  98. painter->setBrush(Qt::NoBrush);
  99. painter->setBrush(QBrush(Qt::black));
  100. painter->drawRect(sceneRect);
  101.  
  102.  
  103. }
  104.  
  105. void GraphWidget::scaleView(qreal scaleFactor)
  106. {
  107. qreal factor = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
  108. if (factor < 0.07 || factor > 100)
  109. return;
  110.  
  111. scale(scaleFactor, scaleFactor);
  112. }
To copy to clipboard, switch view to plain text mode 

the mainwindow.h and mainwindow.cpp are a bit involved with the other graphicsView screens I am using....

but the main.cpp starts out like this

Qt Code:
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. #include <QGraphicsScene>
  5. #include <QGraphicsView>
  6. #include <QGraphicsItem>
  7. #include <QGraphicsSimpleTextItem>
  8. #include <QGraphicsTextItem>
  9. #include <QPixmap>
  10. #include <QPushButton>
  11. #include <QWidget>
  12. #include <QMap>
  13. #include <QFile>
  14. #include <QTextStream>
  15. #include <QTimer>
  16. #include <QRegExp>
  17. #include <QPainter>
  18. #include <QTextEdit>
  19.  
  20. #include "graphwidget.h"
  21. #include "node.h"
  22.  
  23. MainWindow::MainWindow(QWidget *parent) :
  24. QMainWindow(parent),
  25. ui(new Ui::MainWindow)
  26. {
  27. ui->setupUi(this);
  28. initArray();
  29. initTitle(); drawTitle();
  30.  
  31. initText();
  32.  
  33.  
  34. }
  35.  
  36. void MainWindow::populateScene()
  37. {
  38.  
  39. //GraphWidget widget;
  40.  
  41. // add the widget to the scene here....
  42.  
  43.  
  44.  
  45. ui->graphicsView_3->setScene(& dataScene);
  46. ui->graphicsView_3->show();
  47. }
To copy to clipboard, switch view to plain text mode