Results 1 to 7 of 7

Thread: graphicsView.fitInView() :- one axis only ?

  1. #1
    Join Date
    Feb 2011
    Posts
    5
    Qt products
    Platforms
    Windows

    Default graphicsView.fitInView() :- one axis only ?

    HI all,

    This is my fist post here so iv a couple of questions,

    My first question is when using fitInView() is there a way to fit just the Y axis and leave the X axis as it is in the position it is ?

    Im wanting to have a graph that scrolls along the x axis but is always fit to the size of the graphicsView y axis.

    I can build a demo to show exactly what im after if required but im kinda hopping its a simple answer.

    Second, when posting code on this forum is it ok to post PyQt4 (Python) as thats what im using ?
    Thx all.

    Windows XPpSP3|Python 2.7.1|Qt 4.7.1|PyQt 4.8.3|sip 4.12.1|QScintilla 2.4.6

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: graphicsView.fitInView() :- one axis only ?

    I don't see this option with fitInView() but you can just use scale, and give 1.0 to the x axis.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2011
    Posts
    5
    Qt products
    Platforms
    Windows

    Default Re: graphicsView.fitInView() :- one axis only ?

    hmm, yea. but i would need to figure out the y axis scale every time the graphicsView or main window is resised ?

    I was trying with this kinda thing :-

    Qt Code:
    1. rr=ui.graphicsView.mapToScene(ui.graphicsView.frameRect())
    2. left=rr.boundingRect().left()
    3. width=rr.boundingRect().width()
    4. ui.graphicsView.fitInView(left,0,width,300)
    To copy to clipboard, switch view to plain text mode 

    but theres something wrong with it. Every time it runs this bit of code the x axis shrinks a tiny bit i think it is either mapToScene(ui.graphicsView.frameRect()) is not exact enough or theres a rounding problem with the floating value.

    I shall have a play with scaling, see what i can see.

    cheers.
    Thx all.

    Windows XPpSP3|Python 2.7.1|Qt 4.7.1|PyQt 4.8.3|sip 4.12.1|QScintilla 2.4.6

  4. #4
    Join Date
    Feb 2011
    Posts
    5
    Qt products
    Platforms
    Windows

    Default Re: graphicsView.fitInView() :- one axis only ?

    well iv tried everything my small amount of Qt4 knowledge will stretch to including the suggestion above but i just cant get this working.

    Iv made a proper demo so people can easily have a look.

    Qt Code:
    1. #-------------------------------------------------------------------------------
    2. # Name: module1
    3. # Purpose:
    4. #
    5. # Author: User
    6. #
    7. # Created: 20/02/2011
    8. # Copyright: (c) User 2011
    9. # Licence: <your licence>
    10. #-------------------------------------------------------------------------------
    11. #!/usr/bin/env python
    12. import sys
    13. from PyQt4 import QtCore, QtGui
    14.  
    15. class Ui_MainWindow(QtGui.QMainWindow):
    16. def setupUi(self, ui):
    17. ui.resize(600, 400)
    18. self.centralwidget = QtGui.QWidget(ui)
    19. self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
    20. self.horizontalLayout.setContentsMargins(20, 20, 20, 20)
    21. self.graphicsView = QtGui.QGraphicsView(self.centralwidget)
    22. self.horizontalLayout.addWidget(self.graphicsView)
    23. ui.setCentralWidget(self.centralwidget)
    24. self.graphicsView.setGeometry(QtCore.QRect(0, 0, 1000, 300))
    25. self.graphicscene=QtGui.QGraphicsScene(0,0,1000,300)
    26. self.graphicsView.setScene(self.graphicscene)
    27. self.graphicsView.centerOn(0,0)
    28. def resizeEvent (self, QResizeEvent):
    29. resize()
    30.  
    31. def keyPressEvent(self, event):
    32. if event.key() == QtCore.Qt.Key_Escape:
    33. self.close()
    34.  
    35. def resize():
    36. print "im resizing" # iv tried all sorts here , what am i not seeing ?
    37.  
    38. def draw():
    39. bru=QtGui.QBrush(QtCore.Qt.SolidPattern)
    40. bru.setColor(QtGui.QColor(255,000,000))
    41. pen=QtGui.QPen()
    42. pen.setColor(QtGui.QColor(255,255,000))
    43. pen.setWidth(5)
    44. font=QtGui.QFont('White Rabbit',16)
    45. dot1=QtGui.QGraphicsTextItem('Graph goes here.\n\nWhat im trying to get is :-\n\nscrolls along X axis with scroll bar, no resizing,\nResizes to fit graphicsView along Y axis.')
    46. dot1.setFont(font)
    47. dot1.setPos(5,5)
    48. ui.graphicscene.addItem(dot1)
    49. for x in range(10):
    50. ui.graphicscene.addRect(20+(x*100),200,50,80,pen,bru)
    51. ui.graphicscene.addRect(0,0,999,300,QtGui.QPen(QtGui.QColor(000,000,255),5,QtCore.Qt.DashLine))
    52. why=QtGui.QGraphicsTextItem("and why are these gaps here (top and bottom) when first run ?")
    53. why.setFont(QtGui.QFont('White Rabbit',15))
    54. why.setPos(400,-30)
    55. ui.graphicscene.addItem(why)
    56.  
    57. if __name__ == '__main__':
    58. app = QtGui.QApplication(sys.argv)
    59. ui = Ui_MainWindow()
    60. ui.setupUi(ui)
    61. ui.show()
    62. draw()
    63. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    When the window is resized I need the scene to remain 1:1 ratio on the X axis, it can be scrolled (this part works fine atm) & i need the Y axis to be resised to always fit in the graphicsView.


    Any help is gratefully appreciated.
    Thx all.

    Windows XPpSP3|Python 2.7.1|Qt 4.7.1|PyQt 4.8.3|sip 4.12.1|QScintilla 2.4.6

  5. #5
    Join Date
    Feb 2011
    Posts
    5
    Qt products
    Platforms
    Windows

    Default Re: graphicsView.fitInView() :- one axis only ?

    I got it

    Qt Code:
    1. def resize():
    2. width=ui.graphicsView.frameRect().width()
    3. rr=ui.graphicsView.mapToScene(ui.graphicsView.frameRect())
    4. ui.graphicsView.fitInView(0,0,width,300)
    5. ui.graphicsView.ensureVisible(rr.boundingRect(),-2,0)
    To copy to clipboard, switch view to plain text mode 

    only have to figure out why it wont initiate properly when first run, any ideas ?
    Thx all.

    Windows XPpSP3|Python 2.7.1|Qt 4.7.1|PyQt 4.8.3|sip 4.12.1|QScintilla 2.4.6

  6. #6
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: graphicsView.fitInView() :- one axis only ?

    If you're asking for the sizes of components in the constructor, that can cause problems. Components don't have a definite size until they're completely constructed. You'll want to do your resizing after the constructor has finished.

    QWidget::ensurePolished() may be helpful here, but a separate function called after construction works.

  7. #7
    Join Date
    Feb 2011
    Posts
    5
    Qt products
    Platforms
    Windows

    Default Re: graphicsView.fitInView() :- one axis only ?

    hmm i kinda suspected something like that but iv tried calling the resize() at the last possible point (just before the app loop) but that didn't work.

    So whats ensurePolished for then, i shall go have a look

    thx.
    Thx all.

    Windows XPpSP3|Python 2.7.1|Qt 4.7.1|PyQt 4.8.3|sip 4.12.1|QScintilla 2.4.6

Similar Threads

  1. Replies: 0
    Last Post: 9th August 2010, 10:46
  2. QGraphicsView::fitInView() problem on Windows
    By nileshsince1980 in forum Qt Programming
    Replies: 1
    Last Post: 18th November 2009, 04:52
  3. How to don't fitInView a QGraphicsItem
    By jano_alex_es in forum Newbie
    Replies: 2
    Last Post: 26th October 2009, 14:52
  4. fitInView problem at QGraphicsView
    By oguzy in forum Qt Programming
    Replies: 1
    Last Post: 20th November 2008, 23:36
  5. graphics view FitInView problem
    By aamer4yu in forum Qt Programming
    Replies: 6
    Last Post: 25th January 2007, 10:24

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.