Results 1 to 3 of 3

Thread: QGraphics Item custom class not compiling

  1. #1
    Join Date
    Mar 2010
    Posts
    86
    Thanks
    11
    Thanked 7 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default QGraphics Item custom class not compiling

    So it was 7 am and I had been up all night, and in frustration I posted a thread in a not very awake state.... I've re-looked at the problem and it is much easier to explain than my up for 40 hours, fried explanation. I have a mainwindow program that I am working on. It does almost everything through QGraphicsScenes/Views at one point I want to load a bunch of items from a custom QGraphicsItem class.... node.h node.cpp I wrote modified the node class from the elasticnodes demo program... because everytime I write my own class, it is not interacting with the mainwindow correctly and not compiling... so I've been stripping down the example and I have my class working with the example, but re-implementing the class from the example to my code is trying more than my patience... I am under time limits and am very pressed for time. The 'working' part I am posting here is the stripped example and is just drawing a simple 2 lines. it is much more intensive actually as I have all 256 ascii characters as quick little drawLine routines....



    main.cpp
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "graphwidget.h"
    4.  
    5. int main(int argc, char **argv)
    6. {
    7. QApplication app(argc, argv);
    8. qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    9.  
    10. GraphWidget widget;
    11. widget.show();
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    the class I'm trying to divorce from the next class

    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 

    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. int px = -100; int py = -100;
    24.  
    25. for (int i = 0; i < 256; i++)
    26. {
    27.  
    28. if (px < 148) px+=8;
    29. else { px = -100; py+=16; }
    30.  
    31. }
    32.  
    33.  
    34. scale(qreal(1.0), qreal(1.0));
    35. setMinimumSize(400, 400);
    36. // setWindowTitle(tr("Elastic Nodes"));
    37. }
    38.  
    39. void GraphWidget::itemMoved()
    40. {
    41. if (!timerId)
    42. timerId = startTimer(1000 / 25);
    43. }
    44.  
    45. void GraphWidget::keyPressEvent(QKeyEvent *event)
    46. {
    47.  
    48.  
    49. }
    50.  
    51. void GraphWidget::timerEvent(QTimerEvent *event)
    52. {
    53. Q_UNUSED(event);
    54.  
    55. QList<Node *> nodes;
    56. foreach (QGraphicsItem *item, scene()->items()) {
    57. if (Node *node = qgraphicsitem_cast<Node *>(item))
    58. nodes << node;
    59. }
    60.  
    61.  
    62.  
    63.  
    64. if (!itemsMoved) {
    65. killTimer(timerId);
    66. timerId = 0;
    67. }
    68. }
    69.  
    70. void GraphWidget::wheelEvent(QWheelEvent *event)
    71. {
    72. scaleView(pow((double)2, -event->delta() / 240.0));
    73. }
    74.  
    75. void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
    76. {
    77. Q_UNUSED(rect);
    78.  
    79. // Shadow
    80. QRectF sceneRect = this->sceneRect();
    81. QRectF rightShadow(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height());
    82. QRectF bottomShadow(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5);
    83. if (rightShadow.intersects(rect) || rightShadow.contains(rect))
    84. painter->fillRect(rightShadow, Qt::darkGray);
    85. if (bottomShadow.intersects(rect) || bottomShadow.contains(rect))
    86. painter->fillRect(bottomShadow, Qt::darkGray);
    87.  
    88.  
    89. // painter->fillRect(rect.intersect(sceneRect), gradient);
    90. painter->setBrush(Qt::NoBrush);
    91. painter->setBrush(QBrush(Qt::black));
    92. painter->drawRect(sceneRect);
    93.  
    94.  
    95. }
    96.  
    97. void GraphWidget::scaleView(qreal scaleFactor)
    98. {
    99. qreal factor = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
    100. if (factor < 0.07 || factor > 100)
    101. return;
    102.  
    103. scale(scaleFactor, scaleFactor);
    104. }
    To copy to clipboard, switch view to plain text mode 

    class I'm trying to get out of this

    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(GraphWidget *graphWidget, int altNum);
    16.  
    17.  
    18. int type() const { return Type; }
    19.  
    20.  
    21. QRectF boundingRect() const;
    22. QPainterPath shape() const;
    23. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    24.  
    25.  
    26.  
    27. protected:
    28.  
    29. void mousePressEvent(QGraphicsSceneMouseEvent *event);
    30. void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    31.  
    32. private:
    33.  
    34.  
    35. int altN, x, y;
    36. QPointF newPos;
    37. GraphWidget *graph;
    38. };
    39.  
    40. #endif
    To copy to clipboard, switch view to plain text mode 

    node.cpp missing all the different symbols, just drawing one generic 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.  
    26. return QRectF(0, 0, 8, 16);
    27. }
    28.  
    29. QPainterPath Node::shape() const
    30. {
    31. path.addRect(0, 0, 8, 16);
    32. return path;
    33. }
    34.  
    35. void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
    36. {
    37. painter->setBrush(Qt::white);
    38. painter->setPen(QPen(Qt::white, 1, Qt::SolidLine));
    39.  
    40.  
    41. // generic x
    42.  
    43. painter->drawLine(0,0,8,16);
    44. painter->drawLine(8,0,0,16);
    45.  
    46. }
    47. void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
    48. {
    49. update();
    50. QGraphicsItem::mousePressEvent(event);
    51. }
    52.  
    53. void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    54. {
    55. update();
    56. QGraphicsItem::mouseReleaseEvent(event);
    57. }
    To copy to clipboard, switch view to plain text mode 


    that works fine........ as the pic demonstrates.... I want to add the node.h and node.cpp to my MainWindow and implement it like this


    Qt Code:
    1. void MainWindow::populateScene()
    2. {
    3. int heightH = (hexVec.size() * 16) + 8;
    4.  
    5. ui->graphicsView_3->setSceneRect(0,0,559, heightH);
    6. dataScene.setSceneRect(0,0,559, heightH);
    7.  
    8.  
    9.  
    10.  
    11. //call and set up alot of these nodes
    12. // just one for right now
    13.  
    14. QGraphicsItem itemA = new Node(itemA, 2);
    15. itemA->setPos(50,50);
    16. dataScene.addItem(itemA);
    17.  
    18.  
    19. ui->graphicsView_3->setScene(& dataScene);
    20. ui->graphicsView_3->show();
    21. }
    To copy to clipboard, switch view to plain text mode 

    dataScene is a public QGrahicsScene of MainWindow

    but the node class is somehow not able to namespace the mainwindow? or is passed the correct Scene, View, GraphicItem? I've ripped it up and tried many ways, but can't get one stinking class to work with my mainwindow.....

    I have the items drawing great in the desktop widget and most of the scenes are animated, but if I can't get this class to work right, so that I can draw them in a graphicsView, the whole program is for not.....

    Last edited by budda; 21st April 2010 at 01:24.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphics Item custom class not compiling

    What is the error you are getting? Also, please don't link to images from 3rd party sites unless necessary, it's against the rules of this site. Use attachments instead.

  3. The following user says thank you to wysota for this useful post:

    budda (21st April 2010)

  4. #3
    Join Date
    Mar 2010
    Posts
    86
    Thanks
    11
    Thanked 7 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphics Item custom class not compiling

    ooops sorry... just saw the add image icon....
    ahh I figured it out...

    I was passing an unnecessary QGraphicsItem *parent in the node constructor.
    good god that was a frustrating bug!
    But now I have 256 ascii chars as custom drawLine GraphicsItems and they draw blindingly fast.....
    GraphicsScene.addText and .addSimpleText are no longer needed... I have all those chars
    as individual items that have mouseEvents and interactivity....and switching the color is a breeze....
    I just need to rip out all the QStrings I am using for converting binary data and it will be very fast indeed.....
    QChar is slower than just an int? because it inherits from QString? That is actually ironic.... The first time I made
    all these characters in a Turbo C++ 3.2 graphics I was using ints, then switched to chars to because of stack consumption.
    now I'm back to the ints..... full circle.....


    I will try to edit out the pics.....

Similar Threads

  1. Not Scaling QBrush style fro qgraphics view item
    By dan in forum Qt Programming
    Replies: 8
    Last Post: 20th September 2013, 20:56
  2. QList class member and item operations
    By whites11 in forum Newbie
    Replies: 2
    Last Post: 25th March 2010, 18:05
  3. QGraphics*.*Item and __del__
    By prashant in forum Qt Programming
    Replies: 0
    Last Post: 31st October 2009, 16:11
  4. QGraphics Item Transformation inheritance
    By jsabater in forum Qt Programming
    Replies: 0
    Last Post: 14th August 2009, 13:26
  5. Replies: 5
    Last Post: 10th August 2009, 10:50

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.