How to implement an own QScrollArea - python

I am trying to understand the way a QScrollArea is working to implement my own MyQScrollArea widget. MyQScrollArea should make use of setViewportMargins. For this I wrote a minimum working example shown below:
from PyQt5 import QtWidgets
import sys
class MyScrollArea(QtWidgets.QAbstractScrollArea):
def __init__(self):
super().__init__()
self.label = QtWidgets.QLabel(", ".join(map(str, range(100))), self)
hScrollBar = self.horizontalScrollBar()
hScrollBar.setRange(0, self.label.sizeHint().width() - self.sizeHint().width())
hScrollBar.valueChanged.connect(self._HScrollBarValueChanged)
self.setViewportMargins(100, 0, 0, 0)
self._HScrollBarValueChanged(0)
def _HScrollBarValueChanged(self, value):
self.label.move(-value + self.viewportMargins().left(), 0)
def main():
app = QtWidgets.QApplication(sys.argv)
scroll = MyScrollArea()
scroll.show()
app.exec_()
if __name__ == "__main__":
main()
The result of the code is shown below:
However, after I scroll the inner widget moves out of the viewport and paints itself in an area I do not want it to be painted:
What am I doing wrong and how can I get the setViewportMargins functionality working?

You have to set it the QLabel as a child of the viewport, in addition to modifying the properties of the QScrollBar every time the geometry of the widgets is modified:
import sys
from PyQt5 import QtWidgets
class MyScrollArea(QtWidgets.QAbstractScrollArea):
def __init__(self):
super().__init__()
self.label = QtWidgets.QLabel(", ".join(map(str, range(100))), self.viewport())
self.setViewportMargins(100, 0, 0, 0)
self.horizontalScrollBar().valueChanged.connect(
self.on_hscrollbar_value_changed
)
self.update_scrollbar()
def on_hscrollbar_value_changed(self, value):
self.label.move(-value, 0)
def update_scrollbar(self):
self.horizontalScrollBar().setRange(
0, self.label.sizeHint().width() - self.viewport().width()
)
self.horizontalScrollBar().setPageStep(self.viewport().width())
def resizeEvent(self, event):
self.update_scrollbar()
super().resizeEvent(event)
def main():
app = QtWidgets.QApplication(sys.argv)
scroll = MyScrollArea()
scroll.show()
app.exec_()
if __name__ == "__main__":
main()

Related

Adding animation to QPushbutton enterEvent and exitEvent

I'm trying to add custom animation to QPushbutton without making a custom QPushbutton and overriding its enterEvent() and leaveEvent().
So far I've tried this,
#staticmethod
def addButtonHoverAnimation(button:QPushButton,currentPos:QPoint):
'''
Method to:
=> Add hover animation for provided button
'''
enterShift = QPropertyAnimation(button,b'pos',button)
exitShift = QPropertyAnimation(button,b'pos',button)
def enterEvent(e):
pos=button.pos()
enterShift.setStartValue(pos)
enterShift.setEndValue(QPoint(pos.x()+3,pos.y()+3))
enterShift.setDuration(100)
enterShift.start()
Effects.dropShadow(button,1,2)
def leaveEvent(e):
pos=button.pos()
exitShift.setStartValue(pos)
exitShift.setEndValue(QPoint(pos.x()-3,pos.y()-3))
exitShift.setDuration(100)
exitShift.start()
Effects.dropShadow(button)
button.enterEvent=enterEvent
button.leaveEvent=leaveEvent
But when I move the mouse very quickly in and out of the button before the animation finishes, The button starts to move wierdly towards the North-West direction.
Button Animation Using Dynamic Positions
I figured out this was due to the leaveEvent() being triggered before enterEvent() even finishes and also because the start and end values are dynamic. So, I tried providing currentPos as a static position and using it instead,
#staticmethod
def addButtonHoverAnimation(button:QPushButton,currentPos:QPoint):
'''
Method to:
=> Add hover animation for provided button
'''
enterShift = QPropertyAnimation(button,b'pos',button)
enterShift.setStartValue(currentPos)
enterShift.setEndValue(QPoint(currentPos.x()+3,currentPos.y()+3))
enterShift.setDuration(100)
exitShift = QPropertyAnimation(button,b'pos',button)
exitShift.setStartValue(QPoint(currentPos.x()-3,currentPos.y()-3))
exitShift.setEndValue(currentPos)
exitShift.setDuration(100)
def enterEvent(e):
button.setProperty(b'pos',exitShift.endValue())
enterShift.start()
Effects.dropShadow(button,1,2)
def leaveEvent(e):
exitShift.start()
Effects.dropShadow(button)
button.enterEvent=enterEvent
button.leaveEvent=leaveEvent
On running, as soon as the mouse enters the QPushbutton, it moves to the top-left of its parent widget and the animation starts working fine. I can't figure out why this is happening. But I was able to get that, it only happened when I used any static value in the animation.
Button Animation with Static Position:
Here is an example:
import sys
from PyQt5.QtCore import QEvent, QPoint, QObject, QPropertyAnimation
from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget
# This is the same method mentioned above
from styling import addButtonHoverAnimation
class Widget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
layout=QVBoxLayout()
button1 = QPushButton("Proceed1", self)
layout.addWidget(button1)
button2 = QPushButton("Proceed2", self)
layout.addWidget(button2)
self.setLayout(layout)
self.resize(640, 480)
addButtonHoverAnimation(button1)
addButtonHoverAnimation(button2)
def main():
app = QApplication(sys.argv)
view = Widget()
view.show()
ret = app.exec_()
sys.exit(ret)
if __name__ == "__main__":
main()
The problem is that probably when the state is changed from enter to leave (or vice versa) the previous animation still does not end so the position of the widget is not the initial or final position, so when starting the new animation there is a deviation that accumulates. One possible solution is to initialize the position and keep it as a reference.
On the other hand you should not do x.fooMethod = foo_callable since many can fail, in this case it is better to use an eventfilter.
import sys
from dataclasses import dataclass
from functools import cached_property
from PyQt5.QtCore import QEvent, QPoint, QObject, QPropertyAnimation
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget
#dataclass
class AnimationManager(QObject):
widget: QWidget
delta: QPoint = QPoint(3, 3)
duration: int = 100
def __post_init__(self):
super().__init__(self.widget)
self._start_value = QPoint()
self._end_value = QPoint()
self.widget.installEventFilter(self)
self.animation.setTargetObject(self.widget)
self.animation.setPropertyName(b"pos")
self.reset()
def reset(self):
self._start_value = self.widget.pos()
self._end_value = self._start_value + self.delta
self.animation.setDuration(self.duration)
#cached_property
def animation(self):
return QPropertyAnimation(self)
def eventFilter(self, obj, event):
if obj is self.widget:
if event.type() == QEvent.Enter:
self.start_enter_animation()
elif event.type() == QEvent.Leave:
self.start_leave_animation()
return super().eventFilter(obj, event)
def start_enter_animation(self):
self.animation.stop()
self.animation.setStartValue(self.widget.pos())
self.animation.setEndValue(self._end_value)
self.animation.start()
def start_leave_animation(self):
self.animation.stop()
self.animation.setStartValue(self.widget.pos())
self.animation.setEndValue(self._start_value)
self.animation.start()
class Widget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
button1 = QPushButton("Proceed1", self)
button1.move(100, 100)
button2 = QPushButton("Proceed2", self)
button2.move(200, 200)
self.resize(640, 480)
animation_manager1 = AnimationManager(widget=button1)
animation_manager2 = AnimationManager(widget=button2)
def main():
app = QApplication(sys.argv)
view = Widget()
view.show()
ret = app.exec_()
sys.exit(ret)
if __name__ == "__main__":
main()
184 / 5000
Resultados de traducción
If you are using a layout then you must reset the position since the layout does not apply the position change immediately but only when the parent widget applies the changes.
class Widget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
button1 = QPushButton("Proceed1")
button2 = QPushButton("Proceed2")
lay = QVBoxLayout(self)
lay.addWidget(button1)
lay.addWidget(button2)
self.resize(640, 480)
self.animation_manager1 = AnimationManager(widget=button1)
self.animation_manager2 = AnimationManager(widget=button2)
def resizeEvent(self, event):
super().resizeEvent(event)
self.animation_manager1.reset()
self.animation_manager2.reset()

How do I get a signal from a QWiget to change get object?

Hello I have a QWidget and If I click on it, I want to get the object (the QWidget Element I clicked on) is there anyway to do that?
I already found some code but I only get the MouseClickEvent from
self.widget_34.mouseReleaseEvent = lambda event: self.myfunction(event)
Although the solution offered by #Cin is interesting, it has a serious problem: it cancels the mousePressEvent of the widget, so the widget loses the behavior it could have when the widget is pressed, for example the button no longer emits the clicked signal, other widget also They will have the same problem.
A less intrusive solution is to use eventFilter:
import sys
import weakref
from PyQt5 import QtCore, QtWidgets
class ClickListener(QtCore.QObject):
clicked = QtCore.pyqtSignal(QtWidgets.QWidget)
def addWidget(self, widget, other_widget=None):
if not hasattr(self, "_widgets"):
self._widgets = {}
widget.installEventFilter(self)
self._widgets[widget] = widget if other_widget is None else other_widget
weakref.ref(widget, self.removeWidget)
def eventFilter(self, obj, event):
if (
obj in self._widgets
and event.type() == QtCore.QEvent.MouseButtonPress
):
self.clicked.emit(self._widgets[obj])
return super(ClickListener, self).eventFilter(obj, event)
def removeWidget(self, widget):
if hasattr(self, "_widgets"):
if widget in self._widgets:
del self._widgets[widget]
class App(QtWidgets.QWidget):
def __init__(self):
super().__init__()
button = QtWidgets.QPushButton("Press Me")
label = QtWidgets.QLabel("Stack Overflow")
spinBox = QtWidgets.QSpinBox()
te = QtWidgets.QTextEdit()
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(button)
lay.addWidget(label)
lay.addWidget(spinBox)
lay.addWidget(te)
listener = ClickListener(self)
listener.clicked.connect(self.onClicked)
listener.addWidget(button)
listener.addWidget(label)
listener.addWidget(spinBox.lineEdit(), spinBox)
listener.addWidget(te.viewport(), te)
def onClicked(self, obj):
print("Clicked, from", obj)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = App()
ex.show()
sys.exit(app.exec_())
I am not sure this will be a proper solution or not but I think, you can use the partial method of functools module. A collable object can be treated as a function for the purposes of this module. Here you can see my example,
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel
import functools
class App(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(200,200,200,200)
self.button = QPushButton('Button', self)
self.button.move(50,50)
self.label = QLabel(self)
self.label.setText("Label")
self.label.move(100,100)
self.items = [self.button, self.label]
for i in self.items:
i.mousePressEvent = functools.partial(self.getClickedItem, source_object=i)
self.show()
def getClickedItem(self, event, source_object=None):
print("Clicked, from", source_object)
#item text
#print(source_object.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())

QCheckbox to check all other QCheckBoxes

My question is very similar to this post, Python PyQt - Checkbox to uncheck all other checkboxes. However, I am trying to check all other boxes when main checkbox is selected and at the same time, if any of the other boxes are selected independently, then I would like to deselect the main checkbox. I tried modifying the answer provided, but not able to put my head around the 'self.sender' signal. I am not able to change the selection when I deselect a checkbox. Here is the code that I modified using the solution provided by # eyllanesc.Any help is greatly appreciated, thanks!
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Test(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.checkBoxAll = QCheckBox("Select All")
self.checkBoxA = QCheckBox("Select A")
self.checkBoxB = QCheckBox("Select B")
self.checkBoxAll.setChecked(False)
self.checkBoxAll.stateChanged.connect(self.onStateChange)
self.checkBoxA.stateChanged.connect(self.onStateChange)
self.checkBoxB.stateChanged.connect(self.onStateChange)
grid = QGridLayout(self)
grid.addWidget(self.checkBoxAll, 1, 0)
grid.addWidget(self.checkBoxA, 2, 0)
grid.addWidget(self.checkBoxB, 3, 0)
self.setWindowTitle('Test')
self.show()
#pyqtSlot(int)
def onStateChange(self, state):
if state == Qt.Checked:
if self.sender() == self.checkBoxAll:
self.checkBoxA.setChecked(True)
self.checkBoxB.setChecked(True)
elif self.sender() in (self.checkBoxA, self.checkBoxB):
self.checkBoxAll.setChecked(False)
With the logic that you have you are creating a loop since the change of state of any element to change the state of another element, the idea is to block the emission of signals when the change of state is implemented in the slot with blockSignals():
from PyQt5 import QtCore, QtGui, QtWidgets
class Test(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.checkBoxAll = QtWidgets.QCheckBox("Select All")
self.checkBoxAll.setChecked(False)
self.checkBoxAll.stateChanged.connect(self.onStateChangePrincipal)
self.checkBoxA = QtWidgets.QCheckBox("Select A")
self.checkBoxB = QtWidgets.QCheckBox("Select B")
self.checkboxes = [self.checkBoxA, self.checkBoxB]
for checkbox in self.checkboxes:
checkbox.stateChanged.connect(self.onStateChange)
grid = QtWidgets.QGridLayout(self)
grid.addWidget(self.checkBoxAll, 1, 0)
grid.addWidget(self.checkBoxA, 2, 0)
grid.addWidget(self.checkBoxB, 3, 0)
self.setWindowTitle('Test')
#QtCore.pyqtSlot(int)
def onStateChangePrincipal(self, state):
if state == QtCore.Qt.Checked:
for checkbox in self.checkboxes:
checkbox.blockSignals(True)
checkbox.setCheckState(state)
checkbox.blockSignals(False)
#QtCore.pyqtSlot(int)
def onStateChange(self, state):
self.checkBoxAll.blockSignals(True)
self.checkBoxAll.setChecked(QtCore.Qt.Unchecked)
self.checkBoxAll.blockSignals(False)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Test()
w.show()
sys.exit(app.exec_())

pyqt5 widget does not show up in desired size

I would like to implement a class to create a simple widget of fixed size with a scrollbar to display one or more (that's crucial to the problem) images at the same time. Here is the (yet complete but working) code:
from PyQt5 import QtCore, QtWidgets, QtGui
class ImageViewWidget(QtWidgets.QScrollArea):
def __init__(self, parent = None):
super(ImageViewWidget, self).__init__(parent)
self.w = QtWidgets.QFrame()
self.l = QtWidgets.QVBoxLayout()
self.w.setLayout(self.l)
self.setWidget(self.w)
def setImages(self, *images):
self.imageLabel = QtWidgets.QLabel()
self.imageLabel.setScaledContents(True)
self.l.addWidget(self.imageLabel)
if not images[0].isNull():
self.imageLabel.setPixmap(QtGui.QPixmap.fromImage(images[0]))
self.normalSize()
## event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, QtCore.Qt.Key_N, QtCore.Qt.NoModifier)
## QtWidgets.QApplication.sendEvent(self, event)
def normalSize(self):
self.w.adjustSize()
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_N:
self.normalSize()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
imageViewer = ImageViewWidget()
imageViewer.resize(800, 600)
imageViewer.show()
image1 = QtGui.QImage('test.png')
imageViewer.setImages(image1)
sys.exit(app.exec_())
The problem is, that the image does not show up at startup resp does not have the desired size. One has to press "n" first, then the image is displayed with its natural size. And of course I would like to have its natural size from the beginning on without the need to press "n" first.
It seems strange to me that pressing "n" and calling self.normalSize() do not have the same effect, and even simulation the key event by the two commented outlines in setImages do not have the same effect as pressing "n" physically.
There are two "solutions":
Show the widget after setting image, that is, move the line imageViewer.show() 2 lines down.
Moving the first 3 lines of the method setImages to the __init__ method.
Both are no reasonable option, since I want to add and remove dynamically QLabels(which is not implemented yet) to display different images, and also the number of images (which are displayed at the same time) can change.
Any suggestions?
Hi I have modified your code.
Added this 2 lines.
self.timerSingleShot = QtCore.QTimer()
self.timerSingleShot.singleShot(1, self.normalSize)
Use with PyQt5 syntax. This syntax is for PyQt4
from PyQt5 import QtCore, QtWidgets, QtGui
class ImageViewWidget(QtWidgets.QScrollArea):
def __init__(self, parent = None):
super(ImageViewWidget, self).__init__(parent)
self.w = QtWidgets.QFrame()
self.l = QtWidgets.QVBoxLayout()
self.w.setLayout(self.l)
self.setWidget(self.w)
# Added this lines
self.timerSingleShot = QtCore.QTimer()
self.timerSingleShot.singleShot(1, self.normalSize)
def setImages(self, *images):
self.imageLabel = QtWidgets.QLabel()
self.imageLabel.setScaledContents(True)
self.l.addWidget(self.imageLabel)
if not images[0].isNull():
self.imageLabel.setPixmap(QtGui.QPixmap.fromImage(images[0]))
#self.normalSize()
## event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, QtCore.Qt.Key_N, QtCore.Qt.NoModifier)
## QtWidgets.QApplication.sendEvent(self, event)
def normalSize(self):
self.w.adjustSize()
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_N:
self.normalSize()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
imageViewer = ImageViewWidget()
imageViewer.resize(800, 600)
imageViewer.show()
image1 = QtGui.QImage('test.png')
imageViewer.setImages(image1)
sys.exit(app.exec_())
This will also work. Shifted line :
imageViewer.show()
in your code.
from PyQt5 import QtCore, QtWidgets, QtGui
class ImageViewWidget(QtWidgets.QScrollArea):
def __init__(self, parent = None):
super(ImageViewWidget, self).__init__(parent)
self.w = QtWidgets.QFrame()
self.l = QtWidgets.QVBoxLayout()
self.w.setLayout(self.l)
self.setWidget(self.w)
def setImages(self, *images):
self.imageLabel = QtWidgets.QLabel()
self.imageLabel.setScaledContents(True)
self.l.addWidget(self.imageLabel)
if not images[0].isNull():
self.imageLabel.setPixmap(QtGui.QPixmap.fromImage(images[0]))
#self.normalSize()
## event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, QtCore.Qt.Key_N, QtCore.Qt.NoModifier)
## QtWidgets.QApplication.sendEvent(self, event)
def normalSize(self):
self.w.adjustSize()
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_N:
self.normalSize()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
imageViewer = ImageViewWidget()
imageViewer.resize(800, 600)
image1 = QtGui.QImage('test.png')
imageViewer.setImages(image1)
imageViewer.show()
sys.exit(app.exec_())

How can I dynamically add/remove elements in an element?

Here's my code. I'm trying to make it such that as you change the dropdown box, it will dynamically show more or less QLineEdits for input. This is just the latest iteration of testing
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit,
QInputDialog, QApplication, QComboBox, QFrame)
import numpy as np
class GUI(QWidget):
def __init__(self):
super().__init__()
self.initgui()
def initgui(self):
#
# Set up GUI
#
self.setGeometry(100, 100, 400, 400)
self.move(300, 300)
combobox = QComboBox(self)
for i in range(1, 10, 1):
combobox.addItem(str(i + 1))
combobox.activated[str].connect(self.comboboxchanged)
self.setWindowTitle("Testing Easy Setup")
self.show()
def comboboxchanged(self, text):
frame = QWidget(self)
frame.hide()
for num in range(0, int(text), 1):
QLineEdit(frame).move(60, num * 19)
frame.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
gui = GUI()
sys.exit(app.exec_())
The problem is that when you pass a parent to a widget it is placed in the 0, 0 position with respect to the parent, in your case QFrame is on top of QComboBox since both are in the 0, 0 position. The proper thing is to use layouts. On the other hand you have to eliminate the widgets before adding new ones for it, we create a function that eliminates those items.
import sys
from PyQt5.QtWidgets import *
def clearLayout(lay):
while lay.count() > 0:
item = lay.takeAt(0)
widget = item.widget()
if widget:
widget.deleteLater()
del item
class GUI(QWidget):
def __init__(self):
super().__init__()
self.initgui()
def initgui(self):
lay = QHBoxLayout(self)
vlay1 = QVBoxLayout()
combobox = QComboBox(self)
combobox.addItems([str(i) for i in range(2, 11)])
vlay1.addWidget(combobox)
vlay1.addItem(QSpacerItem(20, 245, QSizePolicy.Minimum, QSizePolicy.Expanding))
self.vlay2 = QVBoxLayout()
lay.addLayout(vlay1)
lay.addLayout(self.vlay2)
self.comboboxchanged(combobox.currentText())
combobox.activated[str].connect(self.comboboxchanged)
self.setWindowTitle("Testing Easy Setup")
self.show()
def comboboxchanged(self, text):
clearLayout(self.vlay2)
for num in range(0, int(text)):
self.vlay2.addWidget(QLineEdit(self))
self.vlay2.addItem(QSpacerItem(20, 245, QSizePolicy.Minimum, QSizePolicy.Expanding))
if __name__ == '__main__':
app = QApplication(sys.argv)
gui = GUI()
sys.exit(app.exec_())

Categories

Resources