Answer the question
In order to leave comments, you need to log in
What could be causing the unstable behavior of QListWidget(Python3, PyQt5)?
In the code I'll leave below, there are three classes - ListWidget, Label and MyWindow. ListWidget creates a rubber band and Label creates a QLabel widget with a rounded image. You should not pay attention to these classes, they definitely perform their functions correctly. Problem(s) in MyWindow class.
from PyQt5 import QtCore, QtWidgets, QtGui
class ListWidget(QtWidgets.QListWidget):
def __init__(self, *args, **kwargs):
super(ListWidget, self).__init__(*args, **kwargs)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setEditTriggers(self.NoEditTriggers)
self.setDefaultDropAction(QtCore.Qt.IgnoreAction)
self.setSelectionMode(self.ContiguousSelection)
self.setFlow(self.LeftToRight)
self.setWrapping(True)
self.setResizeMode(self.Adjust)
self.setSpacing(10)
def makeItem(self, lb):
item = QtWidgets.QListWidgetItem(self)
item.setSizeHint(QtCore.QSize(140, 140))
self.setItemWidget(item, lb)
class Label(QtWidgets.QLabel):
clicked = QtCore.pyqtSignal()
def __init__(self, picture, *args, **kwargs):
super(Label, self).__init__(*args, **kwargs)
self.setMaximumSize(140, 140)
self.setMinimumSize(140, 140)
self.radius = 10
self.target = QtGui.QPixmap(self.size())
self.target.fill(QtCore.Qt.transparent)
p = QtGui.QPixmap(picture).scaled(140, 140, QtCore.Qt.KeepAspectRatioByExpanding, QtCore.Qt.SmoothTransformation)
painter = QtGui.QPainter(self.target)
painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
painter.setRenderHint(QtGui.QPainter.HighQualityAntialiasing, True)
painter.setRenderHint(QtGui.QPainter.SmoothPixmapTransform, True)
path = QtGui.QPainterPath()
path.addRoundedRect(0, 0, self.width(), self.height(), self.radius, self.radius)
painter.setClipPath(path)
painter.drawPixmap(0, 0, p)
self.setPixmap(self.target)
def mouseReleaseEvent(self, event):
self.clicked.emit()
class MyWindow(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.scrollArea = QtWidgets.QScrollArea()
self.content_widget = QtWidgets.QWidget()
self.scrollArea.setWidget(self.content_widget)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.simple_btn = QtWidgets.QPushButton('Button')
self.simple_btn.setFixedSize(100, 60)
self.listWidget = ListWidget()
self.label1 = Label('picture1.png')
self.listWidget.makeItem(self.label1)
self.label1.clicked.connect(lambda num=2: self.click(num))
self.label2 = Label('picture1.png')
self.listWidget.makeItem(self.label2)
self.label2.clicked.connect(lambda num=3: self.click(num))
self.label3 = Label('picture1.png')
self.listWidget.makeItem(self.label3)
self.label3.clicked.connect(lambda num=4: self.click(num))
self.box2 = QtWidgets.QVBoxLayout()
self.box2.addWidget(self.simple_btn)
self.box1 = QtWidgets.QGridLayout()
self.box1.addWidget(self.listWidget, 0, 0)
self.box1.setColumnStretch(0, 1)
self.box1.addLayout(self.box2, 0, 1)
self.setLayout(self.box1)
def click(self, num):
self.listWidget.hide()
if not hasattr(self, 'box3'):
self.box1.addWidget(self.scrollArea, 0, 0)
self.box3 = QtWidgets.QGridLayout(self.content_widget)
else:
self.scrollArea.show()
for n in range(1, num):
btn = QtWidgets.QPushButton(f'Button{n}')
self.box3.addWidget(btn, n-1, 0)
self.back_btn = QtWidgets.QPushButton('Back', clicked=self.on_button)
self.back_btn.setFixedSize(100, 60)
self.box2.addWidget(self.back_btn)
def on_button(self):
self.back_btn.deleteLater()
col = 0
for row in range(self.box3.rowCount()):
if self.box3.itemAtPosition(row, col) is not None:
w = self.box3.itemAtPosition(row, col).widget()
w.deleteLater()
self.scrollArea.hide()
self.listWidget.show()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.setWindowTitle(' ')
window.show()
sys.exit(app.exec_())
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question