0

I am working on the Pyqt5 application related to python. I am new to python. I am not able to understand the usage of pyqtSignal, how to use and where to use it. I tried searching youtube https://www.youtube.com/watch?v=LfztdwaGOjs&list=LL&index=1. Got a small video in spanish explaining the pyqtsignal. I typed the code. I am not able to execute it. I am getting the error- TypeError: ('Wrong base class of toplevel widget', (<class 'dialog.Dialog'>, 'QMainWindow'))Please explain me the pyqtsignal usage in python. The code is as below:

dialog.py

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5 import uic

class Dialog(QDialog):
    listWidgetTieneMultiploDe5= pyqtSignal()

    def __init__(self):
        QDialog.__init__(self)
        uic.loadUi("dialog.ui",self)

        self.agregarButton.clicked.connect(self.onAgregarButtonClicked)

        self.listWidgetTieneMultiploDe5.connect(self.onListWidgetTienemultiploDe5)


    def onAgregarButtonClicked(self):
        self.listWidget.addItem("Hola, mundo")

        if self.listWidget.Count()%5==0:
            self.listWidgetTieneMultiploDe5.emit()


    def onListWidgetTienemultiploDe5(self):
        QMessageBox.information(self,"OK","Muliple de 5")

main.py

from PyQt5.QtWidgets import QApplication
from dialog import Dialog
import sys

app= QApplication(sys.argv)
app.setStyle("fusion")
w=Dialog()
w.show()
sys.exit(app.exec_())

dialog.ui

<?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>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="0" column="0">
     <widget class="QPushButton" name="agregarButton">
      <property name="text">
       <string>Agregar</string>
      </property>
     </widget>
    </item>
    <item row="1" column="0">
     <widget class="QListView" name="listView"/>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • change `class Dialog(QDialog):` to `class Dialog(QMainWindow):` and `QDialog.__init__(self)` to `QMainWindow.__init__(self)` – eyllanesc Oct 21 '20 at 15:49
  • I tested it. I get the error: AttributeError: 'Dialog' object has no attribute 'listWidget' – Manu Chaudhary Oct 21 '20 at 16:01
  • And that's correct, in your .ui there is no declared any QListWidget, I only see a QListView: `` – eyllanesc Oct 21 '20 at 16:02
  • If you have other problems then create a new post, those are the SO rules. With your edit I only make my closing vote useless which obviously doesn't make sense. – eyllanesc Oct 21 '20 at 16:51
  • Thank you @eyllanesc for correcting the code. I was doing error while using Qt Designer. I have edited the question. Please give me some link to understand pyqtsignal usage if possible. – Manu Chaudhary Oct 21 '20 at 16:51
  • What should i do? Should i edit the question as it was earlier and ask a new question? Please guide. I am new to SO. – Manu Chaudhary Oct 21 '20 at 16:54
  • 1) Read [ask] and pass the [tour] if you haven't already, 2) I've already done the reversion so as I pointed out: create a new post. – eyllanesc Oct 21 '20 at 16:56
  • Thank you eyllanesc – Manu Chaudhary Oct 21 '20 at 16:58

0 Answers0