Adding Content to pyqt4 scroll area - python

How can I add content to a scroll area in pyqt4? DO i custom define a widget? For example,
if i had a array or a lista =[10,2,2,2,22,3,3,3]. How should I display teh variable in the scrollbar area?

If you want to add content to a scroll area, you need to define a new widget and add that to the scroll area - like you would add a widget to a frame. For example:
textEdit = QtGui.QTextEdit()
scrollArea = QtGui.QScrollArea(MainWindow)
scrollArea.setWidget(textEdit)
Then, you can use textEdit.append() or textEdit.setText() to add the data in the array to the text box in the scroll area. The documentation says it all, really, albeit in C rather than python, but its obvious what you need to do:
QLabel *imageLabel = new QLabel;
QImage image("happyguy.png");
imageLabel->setPixmap(QPixmap.fromImage(image));
scrollArea = new QScrollArea;
scrollArea->setBackgroundRole(QPalette.Dark);
scrollArea->setWidget(imageLabel);

Related

PyQt5: is there a signal when a QScrollBar visibility changes?

I am working in Maya with pyside2 (basically the same as PyQt5).
I have a gui with a scroll area in it... The scroll area has rows of some buttons and stuff that I want to expand horizontally if the user drags the window and expands it larger. Extra rows of these items can be added to it which makes the vertical scroll bar appear (obviously).
I'm trying to determine a way to prevent the slight resize of my rows of items when the scroll bar appears/disappears. Visually, I want the width of the items in each row to say the same if new rows get added and the scroll bar appears, or if the user expands the window some and the scroll bar disappears...
Is there a signal or something that gets sent if the scroll bar's visibility changes so I can adjust the margins of the layout for the items in the scroll area?
I know I can set the scroll bar to be visible or not, but I can't find anything that I can hook into for when this inherently changes as the size of the gui or contents in the scroll area is modified... If there's not already a signal, how could I go about creating one?
Ok, after searching around about the hideEvent like Aaron commented about, I've found something that seems to work...
I created an event filter for the vertical scroll bar in my scroll area... So, in the part of my code where the scroll area is defined, I have something like this:
scroll_area_widget = QtWidgets.QWidget()
self.scroll_area_layout = QtWidgets.QVBoxLayout()
scroll_area_widget.setLayout(self.scroll_area_layout)
scroll_area = QtWidgets.QScrollArea()
scroll_area.setWidget(scroll_area_widget)
self.scrollbar = scroll_area.verticalScrollBar()
self.scrollbar.installEventFilter(self)
Then, I define an 'eventFilter' method like this:
def eventFilter(self, o, e):
if o is self.scrollbar:
if e.type() == QtCore.QEvent.Show:
self.scroll_area_layout.setContentsMargins(5, 0, 3, 0)
elif e.type() == QtCore.QEvent.Hide:
self.scroll_area_layout.setContentsMargins(5, 0, 15, 0)
QtWidgets.QApplication.processEvents()
return False
I put the 'processEvents' in there because I'm getting a flickering when the scrollbar disappears where I think the widgets in the layout stretch out to fill the space before the contents margins gets updated... It doesn't help prevent the flickering every time, but it seems to be less than if I don't have it...
The question now is how do I avoid the flicker? Of course, another question is whether or not this is even a good way to solve this particular issue since I don't know enough about event filters... Is this something that will get triggered only when the scroll bar is shown/hidden, or does it apply to anything in the gui that gets shown/hidden? I ask that since the 'eventFilter' method doesn't seem specific to the scroll bar, and placing the if statement checking if it's the scrollbar seems like it might be a bit of overhead if every event in the whole gui is passing through....

ipywidgets use checkbox to display or hide other widgets

I am using ipywidgets to create a short form, showing two fields, the expected width and the height of an image.
I want to add a checkbox so that if the box is checked, the information is loaded from a file instead.
If the box is checked, a text area (or file selector) should appear and a button to load the file read it and fill the text boxes.
Should I do this by observing the checkbox event? is there a way to hide a widget?
I'm not sure there is a hide function for a widget. One way to do this would be to construct an VBox for your widgets, and add the widgets as children. Then create a function/method which reassigns the children of that VBox to which ever widgets you want to show.
from ipywidgets import Checkbox, VBox
cb1 = Checkbox(description='1')
cb2 = Checkbox(description='2')
cb3 = Checkbox(description='3')
vb = VBox(children = [cb1, cb2, cb3])
top_toggle = Checkbox(description='Remove 3')
def remove_3(button):
if button['new']:
vb.children = [cb1, cb2]
else:
vb.children = [cb1, cb2, cb3]
top_toggle.observe(remove_3, names='value')
display(top_toggle)
display(vb)

Show scrollbar in QTreeWidget with respect to the custom item widgets' size

I am using PyQt5 for my application and a QTreeWidget to display content. Since I need to display rich text (HTML) each item has its text property set to "" and I create individual QLabels with the desired text. I then use QTreeWidget.setItemWidget. My problem is that using that method, when the QTreeWidget is smaller (in width) than the items' width, the horizontal scrollbar is not displayed. Which is logical, since from the point of view of the QTreeWidget, each item has width 0 because its text is empty.
I tried using a custom helper method that I use instead of the QLabel.setText method by automatically calling QLabel.setFixedSize method afterwards, but it doesn't work very well (the size is off by 5 to 90 pixels each time).
How would it be possible to make the whole thing determine automatically when to show the scrollbar, and what width to use for them?
MCVE:
tree = QTreeWidget()
item = QTreeWidgetItem(tree)
label = QLabel()
label.setText("<b>some text here</b>")
tree.setItemWidget(item, 0, label)
I even struggled with the same problem and finally this solution works for me:
Set the treewidget header StretchLastSection to False
treeWidget.header()->setSectionResizeMode(column, QtWidgets.QHeaderView.ResizeToContents)

Kivy scrollbar scroll direction with mouse

Is there a way to change the scroll behavior of Kivy scrollbars when using a mouse? With a mousewheel, the contents of a DropDown or Spinner scroll up or down as expected. However, if you use a mouse to grab the scrollbar and slide it up, the direction is reversed - you have to drag the mouse pointer down to move the scrollbar and list up.
This can be fixed by modifying the DropDown from which Spinner inherits to change scroll_type to include 'bars' (just 'content' by default). I fixed this behaviour as follows:
from functools import partial
dropdownMod = partial(DropDown, bar_width = 10, scroll_type = ['bars','content'])
class SpinnerName(Spinner):
dropdown_cls = dropdownMod
values = ('1','2','3')

IconView with one scrollbar

Good day!
I need an IconView widget with vertical (only) scrollbar. I put my IconView into ScrolledWindow and turned off the horizontal scrollbar. This way:
liststore = gtk.ListStore(gtk.gdk.Pixbuf)
for item in gtk.stock_list_ids():
liststore.append([self.render_icon(item, gtk.ICON_SIZE_DIALOG)])
iconview = gtk.IconView(liststore)
iconview.set_pixbuf_column(0)
scrolledwindow = gtk.ScrolledWindow()
scrolledwindow.add_with_viewport(iconview)
scrolledwindow.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
And when I stretch the window out icons within the widget spread in width. However, when I shrink the window icons don't spread in height, they stay beyond the window's border instead and I cannot get them because there is no horizontal scrollbar.
It seems like I do something wrong, but how to do it right? :)
Should use
scrolledwindow.add(iconview)
instead of 'add_with_viewport'

Categories