How to properly display QChart using QGraphicsScene? - python

I would like to display a QChart in my GUI using QGraphicsScene and QGraphicsView. I tried to do it as follows:
chart = QChart()
chart.addSeries(series)
scene = QGraphicsScene()
scene.setSceneRect(MyGraphicsView.sceneRect())
scene.addItem(chart)
MyGraphicsView.setScene(scene)
The problem is that the chart is not resized to the size of the scene, but displayed in its corner. I know, that I could use the QChartView, but I already use this QGraphicsScene in many places of my application and would prefer to have it this way.
Thanks for any suggestions.
The scene takes almost the whole grey area in the middle and the chart takes only a fraction of it.

Related

Python Kivy how to add scale animation to grid layout

In my app I am easily able animate to move an image in kivy but not able to scale it in size using animate property. Can any one show an example how to do that. Please

pyqtgraph: how to prevent viewbox from stretching with view?

I'm trying to create a list of plots one below the other, on the same axes.
I want to be able to scroll through the plots, and when resizing the view I want to display more plots rather than enlarging the existing plots.
I subclasses PlotWidget and tried to manipulate resizeEvent and sizePolicy and nothing worked.
Any idea?
Thanks
Update:
This is the initial view. When changing the window's height I get this.
What I'm trying to achieve is this.
Also, I would like to add a vertical scrollbar instead of vertical panning.
Update:
Apparently pyqtgraph isn't a good fit for what I'm trying to do. I ended up implementing with QGraphicsScene and QGraphicsView.

Black boxes around slider in wxpython GUI

I imagine this is a simple question and was hoping it someone would be able to help me figure it out. I have multiple wxpython sliders in my GUI but some of them have black boxes around.
The sliders are placed inside a StaticBox which is placed inside ScrolledPanel. It seems that those that are on top (i.e. are shown without the need to scroll the panel) look normal, as for the 'Annotation font size' but the ones that were hidden have black background behind it. Anyone has any ideas?
I thought it was because I was not calling Layout() but it doesn't make any difference.
I managed to fix this problem on my side. Maybe it helps you too. In my case, I have a lot of text boxes and sliders on a scrolled panel (wx.lib.scrolledpanel.ScrolledPanel):
Black boxes around sliders:
I am fixing it by setting the background color of the panel after I have finished populating it with graphic elements:
self.SetBackgroundColour('WHITE')
Here is the result:
No more black boxes around sliders:

PyQt4: Graphics View and Pixmap Size

I'm developing a GUI using QTDesigner for some image processing tasks. I have two graphic views beneath each other in a grid layout. Both should display an image and later on I will add overlays.
I create my Pixmap img_pixmap and add it to my Scene. This scene is then added to my graphics view. Since my image is much larger than my screen size I apply fitInView(). In code:
self.img_pixmap_p = self.img_scene.addPixmap(img_pixmap)
self.img_view.setScene(self.img_scene)
self.img_scene.setSceneRect(QtCore.QRectF())
self.img_view.fitInView(self.img_scene.sceneRect(), QtCore.Qt.KeepAspectRatio)
So far, so good but how do i get rid of the white space around my image view? Ideally, I want my pixmap use the full width of the graphics view and to keep the aspect ratio, the graphics view should adjust its height accordingly. Any ideas on how to achieve that in a straight forward fashion?
Here an image to get a better idea of what I get:
As you can see, there are white borders, which I want to avoid.
Okay, I did as suggested by Pavel:
img_aspect_ratio = float(pixmap.size().width()) / pixmap.size().height()
width = img_view.size().width()
img_view.setFixedHeight( width / img_aspect_ratio )
img_view.fitInView(img_scene.sceneRect(), QtCore.Qt.KeepAspectRatio)
It works fine when you call this in each resizeEvent().

How can I prevent a graphic item from to be draw out the scene? and how to have different background colors for QGraphicsView and QGraphicsScene

I want to set up like the image bellow.
I want differents backgrounds color for QGraphicsView (say, same as window color) and QGraphicsScene (say, white). Also, I want that if some item is drawn out of the scene bounds that part is not rendered (the star 'any item' in img with legs cropped).
I have no clue how to set up this. I'm new on Qt.
PS: I'm using python, but you can examplify in c++ if you feel confortable.
Answering Jeremy Friesner
This is my code applying your tips:
scene = QtGui.QGraphicsScene(0, 0, 256, 256)
scene.setBackgroundBrush(QtGui.QBrush(scene.palette().color(QtGui.QPalette.Window)))
scene.addRect(scene.sceneRect(), QtGui.QPen(QtCore.Qt.NoPen), QtGui.QBrush(QtCore.Qt.white))
scene.addLine(0, 0, 356, 356)
view = QtGui.QGraphicsView(scene)
self.setCentralWidget(view) # we are in a QMainWindow
As you can see, I add a white QRect using scene bounds and a line a bit bigger than the scene bounds. This is the screenshot result of my app (the line is drawn out the scene too):
If the app size is lesser than scene bounds (ie, View <= Scene), the part out the scene is not rendered (bc scroll bars doesn't allow), but if the app is bigger (ie, View > Scene), then it is drawn. How to solve that?
I want different backgrounds color for QGraphicsView (say, same as
window color) and QGraphicsScene (say, white).
A QGraphicsScene object is never directly shown on the screen -- that is to say, it is not a subclass of QWidget and therefore there is no way to add it to your window's widget hierarchy. The only way to view the contents of a QGraphicsScene is by associating a QGraphicsView with the QGraphicsScene and adding the QGraphicsView to the widget hierarchy.
Given that, the solution to your problem should be to simply call setBackgroundBrush(window->palette().color(QPalette::Window)) on your QGraphicsScene object. The QGraphicsView will automatically reflect the background color of the QGraphicsScene.
If you then want the actual contents-area of the QGraphicsScene to be a different color (so that e.g. after you've zoomed out there is a window-background-colored border around a different background-color in the scene-area, as shown in your screenshot), you can get that effect by adding a QGraphicsRect item of the appropriate color and size (as given by QGraphicsScene::sceneRect()) to your scene. (Be sure to call setZValue() on it with a negative value so that it will remain behind all of the other objects in your scene!)
Also, I want that if some item is drawn out of the scene bounds that
part is not rendered (the star 'any item' in img with legs cropped).
AFAIK this is the usual behavior of the QGraphicsView -- any content that is outside the area defined by QGraphicsScene::sceneRect() is automatically clipped to that area. Are you seeing behavior that is different than that?

Categories

Resources