Python Forum

Full Version: Why QListWidget doesn't show new line?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In Qt Designer, I created a form, on which I placed a QlistWidget to display the processing progress:

from PyQt5 import QtWidgets, uic
from Database import Manager
from MyLib import Events, GUI
import Forms, General

class Form(QtWidgets.QWidget):

    def __init__(self, parent: QtWidgets.QMainWindow):
        QtWidgets.QWidget.__init__(self)
        
        ClassForm, _ = uic.loadUiType(Forms.Directory + 'CreateDatabase.ui')    
        self.ui = ClassForm()
        self.ui.setupUi(self)
        self.parent = parent
        
        #name of form
        self.setWindowTitle('Form for creating database')
        self.widget = QtWidgets.QWidget(self)
         
        layoutV = QtWidgets.QVBoxLayout()
        layoutV.addWidget(self.ui.label)
        layoutV.addWidget(self.ui.ButtonCreateDatabase)
        layoutV.addWidget(self.ui.MessageList)
        
        self.widget.setLayout(layoutV)
        self.setLayout(layoutV)
           
        self.ui.ButtonCreateDatabase.clicked.connect(self.CreateDatabase)
  
    ...
        
    def event(self, current_event: Events.EventForMessageList) -> bool:
        if current_event.type() !=  General.type_event:
            return False
         
        current_object = self.ui.MessageList
        current_object.addItem(current_event.message)
        return True
1. The form is perfectly created.
2. During execution of my procedure, events arrive to form.
3. They are processed without errors, including line: current_object.addItem(current_event.message)

, but no new lines appear on dialog form, in QListWidget. It always remains completely empty.
Why?
I animate the interface with this code (executed after receiving the event):
q_app = QtWidgets.QApplication.instance()
q_app.processEvents();
Moreover, the processing is finished and the interface should be rendered without any additional actions, but the list field remains empty.

The funny thing is that the number of invisible items in the list is growing! This code returns an ever-increasing value:
quantity = self.ui.MessageList.count()
self.ui.MessageList.item(quantity-2).text()
returns the correct value! That is, the list actually stores valid data!


I found a bug!!!

It is necessary to change the shape a little bit in size and the elements begin to appear :) But I "revive" the form with:
q_app = QtWidgets.QApplication.instance()
q_app.processEvents();
How to revive the form?
This is the correct code:
class MyThread(QtCore.QThread):
    def __init__(self, parent=None):
        QtCore.QThread.__init__(self, parent)
        
    def run(self):
        for i in range(1, 11):
            self.sleep(1)
            
            # Передача данных из потока через событие
            Events.message_error(str(i) + " Hop, Hey! La-la-ley!")

class MyList(QtWidgets.QListWidget):
    def __init__(self, parent=None):
        QtWidgets.QListWidget.__init__(self, parent)

    def customEvent(self, current_event: Events.EventForMessageList):
        if current_event.type() != General.type_event:
            return False
        
        self.addItem(current_event.message)
                            
class Form(QtWidgets.QWidget):
    def __init__(self, parent):
        QtWidgets.QWidget.__init__(self)
         
        #name of form
        self.setWindowTitle('Form for creating database')
        
        self.label = QtWidgets.QLabel('Обработка создания базы данных')
        self.ButtonCreateDatabase = QtWidgets.QPushButton("Создать")
        self.MessageList = MyList()
          
        self.layoutV = QtWidgets.QVBoxLayout()
        self.layoutV.addWidget(self.label)
        self.layoutV.addWidget(self.ButtonCreateDatabase)
        self.layoutV.addWidget(self.MessageList)
          
        self.setLayout(self.layoutV)
            
        self.thread = MyThread()
        
        self.ButtonCreateDatabase.clicked.connect(self.on_clicked)

    def on_clicked(self):
        General.generate_type_event()
        General.event_receiver = self.MessageList
        
        self.thread.start()