Такая проблема возникла, делаю свой виджет, который будет добавляться динамически в QListWidget
, если сделать статический вывод:
import sys
from PyQt5 import QtGui, QtWidgets
class QCustomQWidget (QtWidgets.QWidget):
def __init__ (self, parent = None):
super(QCustomQWidget, self).__init__(parent)
self.textQVBoxLayout = QtWidgets.QVBoxLayout()
self.textUpQLabel = QtWidgets.QLabel()
self.textDownQLabel = QtWidgets.QLabel()
self.textQVBoxLayout.addWidget(self.textUpQLabel)
self.textQVBoxLayout.addWidget(self.textDownQLabel)
self.allQHBoxLayout = QtWidgets.QHBoxLayout()
self.iconQLabel = QtWidgets.QLabel()
self.allQHBoxLayout.addWidget(self.iconQLabel, 0)
self.allQHBoxLayout.addLayout(self.textQVBoxLayout, 1)
self.setLayout(self.allQHBoxLayout)
def setTextUp (self, text):
self.textUpQLabel.setText(text)
def setTextDown (self, text):
self.textDownQLabel.setText(text)
def setIcon (self, imagePath):
self.iconQLabel.setPixmap(QtGui.QPixmap(imagePath))
class exampleQMainWindow (QtWidgets.QMainWindow):
def __init__ (self):
super(exampleQMainWindow, self).__init__()
self.myQListWidget = QtWidgets.QListWidget(self)
for index, name, icon in [
('No.1', 'Андрей', 'icon.png'),
('No.2', 'Петя', 'icon.png'),
('No.3', 'Вася', 'icon.png')]:
myQCustomQWidget = QCustomQWidget()
myQCustomQWidget.setTextUp(index)
myQCustomQWidget.setTextDown(name)
myQCustomQWidget.setIcon(icon)
myQListWidgetItem = QtWidgets.QListWidgetItem(self.myQListWidget)
myQListWidgetItem.setSizeHint(myQCustomQWidget.sizeHint())
self.myQListWidget.addItem(myQListWidgetItem)
self.myQListWidget.setItemWidget(myQListWidgetItem, myQCustomQWidget)
self.setCentralWidget(self.myQListWidget)
app = QtWidgets.QApplication([])
window = exampleQMainWindow()
window.show()
sys.exit(app.exec_())
то всё прекрасно выводится:
но если попытаться допустим запустить цикл:
import sys
from PyQt5 import QtGui, QtWidgets
import time
class QCustomQWidget (QtWidgets.QWidget):
def __init__ (self, parent = None):
super(QCustomQWidget, self).__init__(parent)
self.textQVBoxLayout = QtWidgets.QVBoxLayout()
self.textUpQLabel = QtWidgets.QLabel()
self.textDownQLabel = QtWidgets.QLabel()
self.textQVBoxLayout.addWidget(self.textUpQLabel)
self.textQVBoxLayout.addWidget(self.textDownQLabel)
self.allQHBoxLayout = QtWidgets.QHBoxLayout()
self.file_iconQLabel = QtWidgets.QLabel()
self.capture_iconQLabel = QtWidgets.QLabel()
self.file_iconQLabel.setMinimumSize(80, 80)
self.file_iconQLabel.setMaximumSize(80, 80)
self.capture_iconQLabel.setMinimumSize(80, 80)
self.capture_iconQLabel.setMaximumSize(80, 80)
self.allQHBoxLayout.addWidget(self.file_iconQLabel, 0)
self.allQHBoxLayout.addWidget(self.capture_iconQLabel, 0)
self.allQHBoxLayout.addLayout(self.textQVBoxLayout, 1)
self.setLayout(self.allQHBoxLayout)
def setTextUp (self, text):
self.textUpQLabel.setText(text)
def setTextDown (self, text):
self.textDownQLabel.setText(text)
def setFileIcon (self, imagePath):
self.file_iconQLabel.setPixmap(QtGui.QPixmap(imagePath))
def setCaptureIcon (self, imagePath):
self.capture_iconQLabel.setPixmap(QtGui.QPixmap(imagePath))
class exampleQMainWindow (QtWidgets.QMainWindow):
def __init__ (self):
super(exampleQMainWindow, self).__init__()
self.myQListWidget = QtWidgets.QListWidget(self)
self.setCentralWidget(self.myQListWidget)
while True:
index = "1"
name = "Петя"
file_icon = "faces/924.jpg"
capture_icon = "faces/924.jpg"
myQCustomQWidget = QCustomQWidget()
myQCustomQWidget.setTextUp(index)
myQCustomQWidget.setTextDown(name)
myQCustomQWidget.setFileIcon(file_icon)
myQCustomQWidget.setCaptureIcon(capture_icon)
myQListWidgetItem = QtWidgets.QListWidgetItem(self.myQListWidget)
myQListWidgetItem.setSizeHint(myQCustomQWidget.sizeHint())
self.myQListWidget.addItem(myQListWidgetItem)
self.myQListWidget.setItemWidget(myQListWidgetItem, myQCustomQWidget)
time.sleep(2)
def run():
app = QtWidgets.QApplication([])
window = exampleQMainWindow()
window.show()
app.exec_()
if __name__ == "__main__":
exampleQMainWindow.run()
тогда программа просто не запускается, подскажите как всё таки добавлять динамически строки с моим виджетом в
QListWidget
?