O
O
Olddd2021-10-23 19:29:50
PyQt
Olddd, 2021-10-23 19:29:50

Why doesn't the custom widget show up in QListWidget?

I am writing a small messenger, I can not understand what is the reason for the absence of the widget.

Python code:

import sys
from datetime import datetime

from PyQt5 import uic
from PyQt5 import QtWidgets


class Message(QtWidgets.QWidget):
    def __init__(self, name, text):
        super().__init__()

        self.data = datetime.now().strftime('%H:%M' + ', ' + name + ':')  # получение строки с данными о сообщении

        # установка виджетов
        self.info = QtWidgets.QLabel(self.data)
        self.info.resize(20, 100)
        self.info.move(10, 5)

        self.info = QtWidgets.QLabel(text)
        self.info.resize(20, 100)
        self.info.move(35, 5)


class MyWidget(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        uic.loadUi('untitled.ui', self)
        self.send_btn.clicked.connect(self.run)


    def run(self):
        item = QtWidgets.QListWidgetItem()
        custom_item = Message('Name', self.mes_input.text())

        self.messages_panel.addItem(item)
        self.messages_panel.setItemWidget(item, custom_item)
        self.mes_input.setText('')


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ex = MyWidget()
    ex.show()
    sys.exit(app.exec_())


ui code:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>730</width>
    <height>516</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="send_btn">
    <property name="geometry">
     <rect>
      <x>630</x>
      <y>420</y>
      <width>91</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>PushButton</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="mes_input">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>420</y>
      <width>601</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QListWidget" name="messages_panel">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>10</y>
      <width>701</width>
      <height>401</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>730</width>
     <height>26</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question