How to change property of a certain area on a widget? - python

Say I have an opaque window for the user to click and drag on, creating a selection rectangle. Is it possible to make just that rectangular area transparent, leaving the rest of the window opaque? Kind of like this image. The black being the opaque part and the white being the transparent part. All part of the same widget.
Edit: Transparent as in being able to see behind the window.

Related

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:

Opaque text with transparent background in wxpython

In wxpython, you can change the transparency of a frame using 'SetTransparent()' function.
There is a sample app that shows the use of transparent frames here. However, the text also fades as the transparency changes (predictably). Is it possible to make it so the text always remains opaque while the background changes transparency?

wxPython: Eliminate DC background flicker while moving shapes on a bitmap

I'm trying to create a panel on wxPython with a user-specified bitmap on the background, where a number of shapes can be dragged.
The expected behaviour is:
User selects an image file on an open file dialog before the panel is initialised;
The image becomes the background of the panel and is scaled to fit the panel while keeping an aspect ratio that depends on a previous user input;
A few circles appear over the image and can be dragged by the user.
I've been able to implement this with no functional problems, but I've been having some trouble with background flicker and so far, the solution I've found that results in the smallest amount of flickering is:
Create a BufferedDC from the loaded image when the panel is created;
Create a PaintDC inside the EVT_PAINT handler;
StretchBlit the BufferedDC into the PaintDC;
Draw the circles on the PaintDC;
Refresh the panel on any event that changes the circles' position or visibility.
Since the circles are draggable, one of these events is mouse motion, so the panel is refreshed every time the mouse moves over the panel, causing flickering.
How can I implement this behaviour in a way that eliminates background flicker?

wxPython Slider background colour incomplete change

wxPython 2.8.10, Python 2.6.2, Windows 7 x64
So I am having a bit of a wxPython issue I am hoping someone can say where I am going wrong. In my application I have Sliders where the background colour changes as you move the slider around. I do this via SetBackgroundColour from within a EVT_SLIDER handler. It mostly works, but I end up with a border of unchanged colour around the outer edge of the widget.
I have tried Refresh up the chain, RefreshRect on the top level Frame around the widget (and a few pixels further out), various combinations of Update and Layout, but nothing seems to erase it. The best I have managed is if I use something like:
frame = wx.GetTopLevelParent(slider)
rect = slider.GetRect()
slider.ClearBackground()
slider.SetBackgroundColour(wx.Colour(red,green,blue))
frame.RefreshRect(rect)
It will correctly draw the background box with the new colour without the border issue, but it will not repaint the slider itself.
However, if I click away so the focus is elsewhere, the border snaps to the new colour.
I could probably use an overlay or custom paint function but I am hoping there is something I am missing here that will get me the desired results.
Any suggestions?
(edited to add)
Looks like resizing the window manually corrects the issue, but Layout() does not.
It also functions correctly if you do the client size trick:
w,h = slider.GetClientSize()
slider.SetClientSize( (w+1,h) )
slider.SetClientSize( (w,h) )

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