Results 1 to 1 of 1

Thread: QGraphicsSvgItem cut in half vertically when view is scaled?

  1. #1
    Join Date
    Dec 2011
    Posts
    27
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsSvgItem cut in half vertically when view is scaled?

    Hi all,

    I'm having an issue which I can't seem to get to the bottom of. I have a an SVG image, in this case, having contents which are a rectangle that is 100x300 units (1:3 ratio). I have also drawn a grid every 100 units in my view. Whenever my view is scaled so that the scene displayed is below a certain size (700 vertical pixels), my SVG image gets cut in half vertically, even though the SVG is within the center of the scene. From this point on when scaling further out, the ratio of the SVG image stays 1:1.5 until I go above a scene display size of 700 px.

    Any ideas on what's causing this, and how to resolve it? I've included some screen captures and the code for the view below.

    Screen Shot 2013-10-14 at 4.06.07 PM.pngScreen Shot 2013-10-14 at 4.06.16 PM.png

    Qt Code:
    1. #ifndef COMPONENTEDITOR_H
    2. #define COMPONENTEDITOR_H
    3.  
    4. #include <QObject>
    5. #include <QPainter>
    6. #include <QColor>
    7. #include <QRectF>
    8. #include <QPainter>
    9. #include <QImage>
    10. #include <QGraphicsView>
    11. #include <QGraphicsScene>
    12. #include <QGraphicsSvgItem>
    13. #include <QWheelEvent>
    14. #include <QDebug>
    15. #include <qmath.h>
    16.  
    17.  
    18. const QString COMP_SVG = "/Users/foobarbaz/Source/ThisApp/gui/Component/Resources/SVG/dip-pkg.svg";
    19.  
    20. class ComponentEditor : public QGraphicsView {
    21.  
    22. Q_OBJECT
    23.  
    24. Q_PROPERTY(unsigned int gridSize READ gridSize WRITE setGridSize DESIGNABLE true)
    25. Q_PROPERTY(QColor gridColor READ gridColor WRITE setGridColor DESIGNABLE true)
    26.  
    27. public:
    28. explicit ComponentEditor(QWidget *parent = 0);
    29. ~ComponentEditor();
    30.  
    31. unsigned int gridSize();
    32. void setGridSize(unsigned int p_size);
    33.  
    34. QColor gridColor();
    35. void setGridColor(QColor p_color);
    36.  
    37. public slots:
    38.  
    39. void setDrag(DragMode p_mode);
    40. void wheelEvent(QWheelEvent *p_event);
    41.  
    42. protected:
    43.  
    44. void drawBackground(QPainter *p_painter, const QRectF &p_rect);
    45.  
    46. private:
    47.  
    48. QGraphicsScene* m_scene;
    49. QGraphicsSvgItem* m_compSvg;
    50. unsigned int m_grid;
    51. QColor m_color;
    52.  
    53.  
    54. };
    55.  
    56. #endif // COMPONENTEDITOR_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "componenteditor.h"
    2.  
    3. ComponentEditor::ComponentEditor(QWidget *parent) : QGraphicsView(parent) {
    4.  
    5. m_scene = new QGraphicsScene();
    6.  
    7. setDragMode(QGraphicsView::ScrollHandDrag);
    8.  
    9.  
    10. m_scene->setSceneRect(0, 0, 10000, 10000);
    11.  
    12. setScene(m_scene);
    13. scale(1, 1);
    14.  
    15. m_grid = 100;
    16. m_color = Qt::blue;
    17.  
    18.  
    19. QPointF center = m_scene->sceneRect().center();
    20.  
    21. m_compSvg = new QGraphicsSvgItem(COMP_SVG);
    22. m_compSvg->setPos(center);
    23. m_scene->addItem(m_compSvg);
    24. centerOn(m_compSvg);
    25. }
    26.  
    27. ComponentEditor::~ComponentEditor() {
    28.  
    29. }
    30.  
    31.  
    32. void ComponentEditor::setDrag(DragMode p_mode) {
    33. QGraphicsView::setDragMode(p_mode);
    34. }
    35.  
    36. void ComponentEditor::drawBackground(QPainter *p_painter, const QRectF &p_rect) {
    37.  
    38. QRectF rect = sceneRect();
    39.  
    40. // p_painter->setWorldMatrixEnabled(true);
    41. p_painter->setPen(m_color);
    42.  
    43. qreal left = int(rect.left());
    44. qreal top = int(rect.top());
    45.  
    46. QVarLengthArray<QLineF, 100> linesX;
    47.  
    48. for (qreal x = left; x < rect.right(); x += m_grid )
    49. linesX.append(QLineF(x, rect.top(), x, rect.bottom()));
    50.  
    51. QVarLengthArray<QLineF, 100> linesY;
    52.  
    53. for (qreal y = top; y < rect.bottom(); y += m_grid )
    54. linesY.append(QLineF(rect.left(), y, rect.right(), y));
    55.  
    56. p_painter->drawLines(linesX.data(), linesX.size());
    57. p_painter->drawLines(linesY.data(), linesY.size());
    58.  
    59. }
    60.  
    61. unsigned int ComponentEditor::gridSize() {
    62. return m_grid;
    63. }
    64.  
    65. void ComponentEditor::setGridSize(unsigned int p_size) {
    66. m_grid = p_size;
    67. }
    68.  
    69. QColor ComponentEditor::gridColor() {
    70. return m_color;
    71. }
    72.  
    73. void ComponentEditor::setGridColor(QColor p_color) {
    74. m_color = p_color;
    75. }
    76.  
    77. // this is run when the user uses the mouse wheel to scale up/down
    78. // the view
    79. void ComponentEditor::wheelEvent(QWheelEvent *p_event) {
    80.  
    81. qreal factor = qPow(1.2, p_event->delta() / 240.0);
    82. scale(factor, factor);
    83.  
    84. // debug: new scene size
    85. QRect sRect(0, 0, width(), height());
    86. QRectF realRect = mapFromScene(sRect).boundingRect();
    87.  
    88. qDebug() << "CE: Scene" << realRect.width() << realRect.height() << factor;
    89.  
    90. p_event->accept();
    91. }
    To copy to clipboard, switch view to plain text mode 


    Added after 1 50 minutes:


    The problem, it turns out, was that half of the rectangle in the svg image was located below the lower boundary of the SVG image (had a final height + y start position value > image height). For smaller scene resolutions, it wouldn't show content outside of the svg image boundaries, but for larger ones it would.

    If you find yourself here in the future, googling for an answer: fix your svg content =)
    Last edited by droneone; 15th October 2013 at 00:09.

Similar Threads

  1. QScrollArea that only scrolls vertically
    By stephelton in forum Qt Programming
    Replies: 1
    Last Post: 13th January 2013, 11:48
  2. Align vertically QwtPlot
    By Valsylver in forum Qwt
    Replies: 1
    Last Post: 4th August 2012, 12:44
  3. how to add QAction vertically and horizontally in QToolBar
    By sanjayshelke in forum Qt Programming
    Replies: 0
    Last Post: 10th November 2008, 09:56
  4. Rezizing the Qlabels vertically
    By anju123 in forum Qt Programming
    Replies: 3
    Last Post: 3rd January 2008, 15:35
  5. How can I write vertically in a QPushbutton ?
    By castorvert in forum Qt Programming
    Replies: 1
    Last Post: 2nd April 2006, 21:27

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
  •  
Qt is a trademark of The Qt Company.