PDA

View Full Version : graphicsView.fitInView() :- one axis only ?



JackDinn
21st February 2011, 14:46
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 ?

high_flyer
21st February 2011, 14:57
I don't see this option with fitInView() but you can just use scale, and give 1.0 to the x axis.

JackDinn
21st February 2011, 15:07
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 :-


rr=ui.graphicsView.mapToScene(ui.graphicsView.fram eRect())
left=rr.boundingRect().left()
width=rr.boundingRect().width()
ui.graphicsView.fitInView(left,0,width,300)

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.

JackDinn
21st February 2011, 18:57
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.



#-------------------------------------------------------------------------------
# Name: module1
# Purpose:
#
# Author: User
#
# Created: 20/02/2011
# Copyright: (c) User 2011
# Licence: <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import sys
from PyQt4 import QtCore, QtGui

class Ui_MainWindow(QtGui.QMainWindow):
def setupUi(self, ui):
ui.resize(600, 400)
self.centralwidget = QtGui.QWidget(ui)
self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setContentsMargins(20, 20, 20, 20)
self.graphicsView = QtGui.QGraphicsView(self.centralwidget)
self.horizontalLayout.addWidget(self.graphicsView)
ui.setCentralWidget(self.centralwidget)
self.graphicsView.setGeometry(QtCore.QRect(0, 0, 1000, 300))
self.graphicscene=QtGui.QGraphicsScene(0,0,1000,30 0)
self.graphicsView.setScene(self.graphicscene)
self.graphicsView.centerOn(0,0)
def resizeEvent (self, QResizeEvent):
resize()

def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Escape:
self.close()

def resize():
print "im resizing" # iv tried all sorts here , what am i not seeing ?

def draw():
bru=QtGui.QBrush(QtCore.Qt.SolidPattern)
bru.setColor(QtGui.QColor(255,000,000))
pen=QtGui.QPen()
pen.setColor(QtGui.QColor(255,255,000))
pen.setWidth(5)
font=QtGui.QFont('White Rabbit',16)
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.')
dot1.setFont(font)
dot1.setPos(5,5)
ui.graphicscene.addItem(dot1)
for x in range(10):
ui.graphicscene.addRect(20+(x*100),200,50,80,pen,b ru)
ui.graphicscene.addRect(0,0,999,300,QtGui.QPen(QtG ui.QColor(000,000,255),5,QtCore.Qt.DashLine))
why=QtGui.QGraphicsTextItem("and why are these gaps here (top and bottom) when first run ?")
why.setFont(QtGui.QFont('White Rabbit',15))
why.setPos(400,-30)
ui.graphicscene.addItem(why)

if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ui = Ui_MainWindow()
ui.setupUi(ui)
ui.show()
draw()
sys.exit(app.exec_())

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.

JackDinn
21st February 2011, 21:50
I got it :D



def resize():
width=ui.graphicsView.frameRect().width()
rr=ui.graphicsView.mapToScene(ui.graphicsView.fram eRect())
ui.graphicsView.fitInView(0,0,width,300)
ui.graphicsView.ensureVisible(rr.boundingRect(),-2,0)

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

SixDegrees
21st February 2011, 22:20
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.

JackDinn
21st February 2011, 22:24
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.