Results 1 to 14 of 14

Thread: QGraphicsScene geometric centre

  1. #1
    Join Date
    Aug 2009
    Location
    Lviv
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsScene geometric centre

    Hi, all


    Understanding the coordinates concepts for QGraphicsScene is quite hart to me .

    Readin documentation I can clear understand QGraphicsView and Items coordinate but not scene. Now I write code not setting sceneRect and boundingRect for QGraphicsItem couse if I set them everething go wrong.


    What I missunderstand is where is geometric centre of QGraphicsScene regarding QGraphicsView (widget)?

    Thank you very much in advance.

  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: QGraphicsScene geometric centre

    The view can display any region of the scene so there is no constant place for the centre of the scene regarding the view. You can use the family of mapTo*() methods of different objects to move between coordinate spaces.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2009
    Location
    Lviv
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene geometric centre

    If view can show any region of the scene which one it shows by default and what the sceneRect property mean? We should specify х, y, width and heigh for sceneRect. So setting the point (x,y) I should understand where it will be placed.



  4. #4
    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: QGraphicsScene geometric centre

    As far as I remember by default (0,0) coordinates of the scene are in (0,0) coordinates of the viewport unless the scene is smaller than the viewport - in that case the scene is centred in the viewport.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Aug 2009
    Location
    Lviv
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene geometric centre

    It does not look so. Maybe small example . I have scene, view and item as hare



    Qt Code:
    1. #include "plot.h"
    2. #include "chart.h"
    3. #include <QPainter>
    4. #include <QColor>
    5. #include <QHBoxLayout>
    6. #include <QMenuBar>
    7. #include <QPushButton>
    8. #include <QDebug>
    9. #include <QDockWidget>
    10.  
    11. //just in shoret do not post .h file Plot inherits QMainWindow
    12. Plot::Plot()
    13. {
    14. exitAction = new QAction(tr("E&xit"), this);
    15.  
    16. fileMenu = menuBar()->addMenu(tr("&File"));
    17. fileMenu->addAction(exitAction);
    18.  
    19. scene = new PlotScene(QRectF());//[COLOR="red"][SIZE="6"][SIZE="7"]if I set any value here I my plot will be pained outsie view[/SIZE][/SIZE]
    20. [/COLOR]
    21.  
    22. scene->setBackgroundBrush(QColor(Qt::green));
    23. scene->addItem(new chart());//view call paint to do all work
    24.  
    25. view = new PlotView();
    26. //view->setFixedSize(500,500);
    27. view->setScene(scene);
    28. //view->setBackgroundBrush(QColor(Qt::red));
    29. setCentralWidget(view);
    30.  
    31. //add docWidget
    32. QString s("Dock Widget");
    33. QDockWidget *dockWidget = new QDockWidget(s);
    34. dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea |
    35. Qt::RightDockWidgetArea);
    36.  
    37. addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
    38. QPushButton* tbutton = new QPushButton("Clear");
    39. dockWidget->setWidget(tbutton);
    40. //dockWidget->
    41.  
    42. //
    43. connect( tbutton, SIGNAL(clicked()), scene, SLOT(clearPlot()) );
    44.  
    45. }
    46. //-----------------------------------------------------------
    47. #include "chart.h"
    48. #include <QPainter>
    49. #include <QWidget>
    50. #include <QPolygonF>
    51. #include <QStyleOptionGraphicsItem>
    52. #include <QDebug>
    53. #include "plot.h"
    54. #include <math.h>
    55.  
    56.  
    57. //chart inherits QGraphicsItem
    58. chart::chart()
    59. {
    60. }
    61.  
    62. void chart::draw(QPainter *painter, QWidget *widget)
    63. {
    64.  
    65. painter->drawLine(0,widget->height(), 0, 0-(widget->height()/2));
    66. painter->drawLine(0-(widget->width()/2),0, (widget->width()/2), 0);
    67.  
    68. int step = 20;
    69. int w = 5;
    70. //draw y markers
    71. for(int i =0; i<(widget->height()*2); i=i+step)
    72. {
    73. painter->drawLine(-w,widget->height()-i, w, widget->height()-i);
    74. }
    75.  
    76. //draw x marker
    77. for(int i =4; i<(widget->width()*2); i=i+step)
    78. {
    79. painter->drawLine(widget->width()-i, w, widget->width()-i, -w);
    80. }
    81. //draw sin()
    82. QPolygonF pol;
    83. for (double i=0; i<90; i=i+0.001)
    84. {
    85. pol.append(QPointF((i*60) - (widget->width()+155),sin(i)*100));
    86. }
    87.  
    88. painter->drawPolyline(pol);
    89.  
    90. }
    91. void chart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    92. {
    93.  
    94. // int side = qMin(widget->width(), widget->height());
    95. QColor color(255,255,255);
    96. QPen pen(color,1.5);
    97. pen.setColor(color);
    98. painter->setPen(pen);
    99.  
    100.  
    101. draw(painter, widget);
    102.  
    103. qDebug() <<"boundingRect ="<<this->boundingRect();
    104.  
    105. //seems depend on main window geometry QRect(0,0 798x576)-2,-4
    106. //The viewport represents the physical coordinates specifying an arbitrary rectangle.
    107. qDebug() << "Painter viewport="<< painter->viewport();
    108.  
    109. //By default the logical and physical coordinate systems coincide,
    110. //and are equivalent to the paint device's rectangle.
    111. qDebug() << "Painter window = "<< painter->window();
    112.  
    113. //scene
    114. //Returns the item's position in scene coordinates
    115. qDebug() << "scene coordinates="<< this->scenePos();
    116. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Nadia; 3rd September 2009 at 08:48.

  6. #6
    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: QGraphicsScene geometric centre

    The code is incomplete. What is the boundingRect of the item? Furthermore the scene size is null thus the scene takes the dimensions of the bounding rect of all its items so it's hard to deduce anything from this code.

    Also are you sure you actually want to use graphics view for something like that? If you make the chart size dependent on the widget size, it is likely graphics view will not do you much good.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Aug 2009
    Location
    Lviv
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene geometric centre

    The problem is then I set the boundingRect of the item the chart is painted outside the view.

    Why view do not much this aim as far as I know "View coordinates are the coordinates of the widget."(from QT doc)

    Qt Code:
    1. #ifndef CHART_H
    2. #define CHART_H
    3.  
    4. #include <QGraphicsItem>
    5.  
    6. class chart : public QGraphicsItem
    7. {
    8. public:
    9. chart();
    10.  
    11. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    12. QRectF boundingRect() const
    13. {
    14. //qreal penWidth = 1;
    15. return QRectF();
    16. }
    17.  
    18. private:
    19. //for internal use only shoul be called by paint
    20. void draw(QPainter *painter, QWidget *widget);
    21.  
    22.  
    23. };
    24.  
    25. #endif // CHART_H
    To copy to clipboard, switch view to plain text mode 

  8. #8
    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: QGraphicsScene geometric centre

    The bounding rect you return needs to be valid. If it's not, your item will not be drawn properly. You need to return a bounding rect that will determine the local coordinate space for the object. For example you can return QRectF(0,0,600,400) and then in the paint routine only draw within those coordinates. You can move your item around by using QGraphicsItem::setPos() and make it bigger or smaller (relative to the scene) using QGraphicsItem::scale(). You can also make the whole scene bigger or smaller by scaling the view.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Aug 2009
    Location
    Lviv
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene geometric centre

    I know that I should set vaild bounding rect but when I set
    Qt Code:
    1. QRectF boundingRect() const
    2. {
    3. //qreal penWidth = 1;
    4. return QRectF(-800, -600, 800, 600);
    5. }
    To copy to clipboard, switch view to plain text mode 

    It draw some where outside .....

  10. #10
    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: QGraphicsScene geometric centre

    Did you adjust your paint() accordingly?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Aug 2009
    Location
    Lviv
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene geometric centre

    It seems yes , e.g. lines.


    Qt Code:
    1. painter->drawLine(0,widget->height(), 0, 0-(widget->height()/2));
    2. painter->drawLine(0-(widget->width()/2),0, (widget->width()/2), 0);
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. Plot p;
    2. //set (x,y) w and h relative to screen
    3. p.setGeometry(100, 100, 800, 600);
    4.  
    5. p.show();
    To copy to clipboard, switch view to plain text mode 
    but result is that they are painted somewhere around.....

  12. #12
    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: QGraphicsScene geometric centre

    Well... this is not enough
    Get rid of this method:
    Qt Code:
    1. void chart::draw(QPainter *painter, QWidget *widget)
    To copy to clipboard, switch view to plain text mode 
    Don't access the widget anywhere.

    Use the options object in paint() to get information about how you should paint your item.

    Paint the item in absolute (-800, -600) to (0,0) coordinates.

    BTW. didn't you mean your bounding rect to return (-800, -600, 1600, 1200)?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Aug 2009
    Location
    Lviv
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene geometric centre

    Ok I just added chart::draw for future extension. Without it it work in the same way.

    BUT if I set bounding rect to return (-800, -600, 1600, 1200) it is centred in nice way...... why ? and I get scroll around the scene.....

  14. #14
    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: QGraphicsScene geometric centre

    I don't understand the question.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 12
    Last Post: 7th September 2011, 16:37
  2. Replies: 0
    Last Post: 5th March 2009, 06:54
  3. QPixmap display on QGraphicsScene
    By febil in forum Qt Programming
    Replies: 2
    Last Post: 26th February 2009, 09:27
  4. in QGraphicsScene matrix of other QGraphicsScene
    By Noxxik in forum Qt Programming
    Replies: 5
    Last Post: 15th February 2009, 17:27
  5. When to use QGraphicsScene or QWidget
    By fossill in forum Qt Programming
    Replies: 2
    Last Post: 9th February 2007, 23:58

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.