Home>

There is a program in PyQt5 , on the main screen of which there are two lists (QListWidget), which are loaded with data from the SQLite database.

Below the lists are the "Add", "Delete" and "Move" buttons. When you click on the "Add" button, a small window opens where you need to fill in the line (QLineEdit), and click on the "Add" button already in this window (small).

main.py has a functionup_list ()which cleansQListWidgetand adds data from the database to the list. This function works great if you call it from the same class in which it was declared (App), but if you call it from another class (Dialod_UI, a class with a second window), it doesn't work.
More precisely, the function itself is launched (this can be verified becauseprint ()from this function), but the list itself is not updated.

Tried to implement this withPyQtSignal, nothing changes. By the way, I bind the functionup_list ()to the first "Delete" button, everything works as needed, the list is updated after clicking on this button.

I need the list to update in exactly the same way, only after clicking on the "Add" button from an additional, small window.

The code is attached:

main.py

import sys
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import pyqtSignal, QObject, QThread
import time
import main_ui
import dialog_ui
from db import DataBase
class App (QtWidgets.QMainWindow):
    def __init __ (self):
        super (App, self) .__ init __ ()
        self.init_UI ()
    def init_UI (self):
        self.main_ui= main_ui.Ui_Form ()
        self.main_ui.setupUi (self)
        self.dialog_ui= Dialod_UI ()
        self.db= DataBase ()
        self.l_items= self.main_ui.listWidget
        self.l_items_2= self.main_ui.listWidget_2
        self.up_list ()
        self.main_ui.pushButton_2.clicked.connect (self.up_list)
        self.main_ui.pushButton.clicked.connect (self.dialog_ui.show)
        self.l_items.itemClicked.connect (self.item_delete)
        self.l_items_2.itemClicked.connect (self.item_delete_2)
        self.main_ui.pushButton_6.clicked.connect (lambda: self.l_items_2.addItem ('test'))
    def up_list (self):
        print ('there is a signal')
        self.l_items.clear ()
        self.l_items_2.clear ()
        for value_mto in self.db.sql.execute ('SELECT mark FROM cartridge_mto'):
            self.l_items.addItems (value_mto)
        for value in self.db.sql.execute ('SELECT mark FROM cartridge'):
            self.l_items_2.addItems (value)
    def item_delete (self, item):
        print (f'First list: {item.text ()} ')
    def item_delete_2 (self, item):
        print (f'Second list: {item.text ()} ')
class Dialod_UI (QtWidgets.QWidget):
    def __init __ (self):
        super (Dialod_UI, self) .__ init __ ()
        self.setup_dialod ()
    def setup_dialod (self):
        self.dialog_ui= dialog_ui.Ui_Form ()
        self.dialog_ui.setupUi (self)
        self.dialog_ui.pushButton.clicked.connect (self.add_in_db), self.dialog_ui.pushButton.clicked.connect (self.close)
    def add_in_db (self):
        self.db= DataBase ()self.i= self.dialog_ui.lineEdit.text () if self.i!= '':
            self.db.sql.execute (f'INSERT INTO cartridge_mto VALUES (?) ', (self.i,))
            self.db.db.commit ()
        self.app= App ()
        self.app.up_list ()
if __name__== '__main__':
    app= QtWidgets.QApplication (sys.argv)
    application= App ()
    application.show ()
    sys.exit (app.exec ())

db.py

import sqlite3
class DataBase ():
    db= sqlite3.connect ('D: \\ Python \\ Cartridges \\ server.db')
    sql= db.cursor ()
    sql.execute ('CREATE TABLE IF NOT EXISTS cartridge_mto (mark TEXT)')
    sql.execute ('CREATE TABLE IF NOT EXISTS cartridge (mark TEXT)')
    db.commit ()

main_ui.py

# -* -coding: utf-8 -* -
# Form implementation generated from reading ui file 'main_ui.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form (object):
    def setupUi (self, Form):
        Form.setObjectName ("Form")
        Form.resize (800, 598)
        self.listWidget= QtWidgets.QListWidget (Form)
        self.listWidget.setGeometry (QtCore.QRect (50, 30, 311, 431))
        self.listWidget.setObjectName ("listWidget")
        self.listWidget_2= QtWidgets.QListWidget (Form)
        self.listWidget_2.setGeometry (QtCore.QRect (420, 30, 311, 431))
        self.listWidget_2.setObjectName ("listWidget_2")
        self.pushButton= QtWidgets.QPushButton (Form)
        self.pushButton.setGeometry (QtCore.QRect (50, 470, 91, 31))
        self.pushButton.setObjectName ("pushButton")
        self.pushButton_2= QtWidgets.QPushButton (Form)
        self.pushButton_2.setGeometry (QtCore.QRect (150, 470, 91, 31))
        self.pushButton_2.setObjectName ("pushButton_2")
        self.pushButton_3= QtWidgets.QPushButton (Form)
        self.pushButton_3.setGeometry (QtCore.QRect (250, 470, 111, 31))
        self.pushButton_3.setObjectName ("pushButton_3")
        self.pushButton_4= QtWidgets.QPushButton (Form)
        self.pushButton_4.setGeometry (QtCore.QRect (620, 470, 111, 31))
        self.pushButton_4.setObjectName ("pushButton_4")
        self.pushButton_5= QtWidgets.QPushButton (Form)
        self.pushButton_5.setGeometry (QtCore.QRect (520, 470, 91, 31))
        self.pushButton_5.setObjectName ("pushButton_5")
        self.pushButton_6= QtWidgets.QPushButton (Form)
        self.pushButton_6.setGeometry (QtCore.QRect (420, 470, 91, 31))
        self.pushButton_6.setObjectName ("pushButton_6")
        self.retranslateUi (Form)
        QtCore.QMetaObject.connectSlotsByName (Form)
    def retranslateUi (self, Form):
        _translate= QtCore.QCoreApplication.translate
        Form.setWindowTitle (_translate ("Form", "Form"))
        self.pushButton.setText (_translate ("Form", "ADD"))
        self.pushButton_2.setText (_translate ("Form", "DELETE"))
        self.pushButton_3.setText (_translate ("Form", "PUSH"))
        self.pushButton_4.setText (_translate ("Form", "PUSH"))
        self.pushButton_5.setText (_translate ("Form", "DELETE"))
        self.pushButton_6.setText (_translate ("Form", "ADD"))

dialog_ui.py

# -* -coding: utf-8 -* -
# Form implementation generated from reading ui file 'dialog_ui.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form (object):
    def setupUi (self, Form):
        Form.setObjectName ("Form")
        Form.resize (393, 200)
        Form.setStyleSheet ("background-color: # F0F0F0;")
        self.lineEdit= QtWidgets.QLineEdit (Form)
        self.lineEdit.setGeometry (QtCore.QRect (50, 80, 291, 41))
        font= QtGui.QFont ()
        font.setFamily ("Golos Text")
        font.setPointSize (10)
        font.setBold (True)
        font.setWeight (75)
        self.lineEdit.setFont (font)
        self.lineEdit.setStyleSheet ("padding: 5px; \ n"
"border: 2px solid black; \ n"
"border-radius: 5px;")self.lineEdit.setObjectName ("lineEdit") self.pushButton= QtWidgets.QPushButton (Form)
        self.pushButton.setGeometry (QtCore.QRect (110, 140, 171, 41))
        font= QtGui.QFont ()
        font.setFamily ("Golos Text")
        font.setBold (True)
        font.setWeight (75)
        self.pushButton.setFont (font)
        self.pushButton.setStyleSheet ("border: 2px solid black; \ n"
"border-radius: 5px;")
        self.pushButton.setObjectName ("pushButton")
        self.label= QtWidgets.QLabel (Form)
        self.label.setGeometry (QtCore.QRect (60, 10, 281, 31))
        font= QtGui.QFont ()
        font.setFamily ("Golos Text")
        font.setPointSize (10)
        font.setBold (True)
        font.setWeight (75)
        self.label.setFont (font)
        self.label.setAlignment (QtCore.Qt.AlignCenter)
        self.label.setObjectName ("label")
        self.label_2= QtWidgets.QLabel (Form)
        self.label_2.setGeometry (QtCore.QRect (60, 40, 281, 21))
        font= QtGui.QFont ()
        font.setFamily ("Golos Text")
        font.setPointSize (10)
        font.setBold (True)
        font.setWeight (75)
        self.label_2.setFont (font)
        self.label_2.setAlignment (QtCore.Qt.AlignCenter)
        self.label_2.setObjectName ("label_2")
        self.retranslateUi (Form)
        QtCore.QMetaObject.connectSlotsByName (Form)
    def retranslateUi (self, Form):
        _translate= QtCore.QCoreApplication.translate
        Form.setWindowTitle (_translate ("Form", "Form"))
        self.pushButton.setText (_translate ("Form", "ADD"))
        self.label.setText (_translate ("Form", "Write label on cartridge"))
        self.label_2.setText (_translate ("Form", "and printer name"))
  • Answer # 1

    Try this:

    main.py

    import sys
    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtCore import pyqtSignal, QObject, QThread
    # ??? import time
    from main_ui import Ui_Form as Ui_Form_main
    from dialog_ui import Ui_Form as Ui_Form_dialog
    from db import DataBase
    class Dialod_UI (QtWidgets.QWidget):
    # !!! vvvvvv
        def __init __ (self, parent): # +++ parent
            super (Dialod_UI, self) .__ init __ ()
            self.parent= parent
            self.setup_dialod ()
        def setup_dialod (self):
            self.dialog_ui= Ui_Form_dialog () # dialog_ui.Ui_Form ()
            self.dialog_ui.setupUi (self)
            self.dialog_ui.pushButton.clicked.connect (
                self.add_in_db), self.dialog_ui.pushButton.clicked.connect (self.close)
        def add_in_db (self):
            self.db= DataBase ()
            self.i= self.dialog_ui.lineEdit.text ()
            if self.i!= '':
                self.db.sql.execute (f'INSERT INTO cartridge_mto VALUES (?) ', (self.i,))
                self.db.db.commit ()
    # self.app= App ()
    # self.app.up_list ()
    # !!! vvvvvv
            self.parent.up_list () # +++
    # !!! vvvvvvv
    class App (QtWidgets.QWidget): # -(QtWidgets.QMainWindow): no
        def __init __ (self):
            super (App, self) .__ init __ ()
            self.init_UI ()
        def init_UI (self):
            self.main_ui= Ui_Form_main () # main_ui.Ui_Form ()
            self.main_ui.setupUi (self)
            self.dialog_ui= Dialod_UI (self) # +++ self
            self.db= DataBase ()
            self.l_items= self.main_ui.listWidget
            self.l_items_2= self.main_ui.listWidget_2
            self.up_list ()
            self.main_ui.pushButton_2.clicked.connect (self.up_list)
            self.main_ui.pushButton.clicked.connect (self.dialog_ui.show)
            self.l_items.itemClicked.connect (self.item_delete)
            self.l_items_2.itemClicked.connect (self.item_delete_2)
            self.main_ui.pushButton_6.clicked.connect (lambda: self.l_items_2.addItem ('test'))
        def up_list (self):
            print ('there is a signal')
            self.l_items.clear ()
            self.l_items_2.clear ()
            for value_mto in self.db.sql.execute ('SELECT mark FROM cartridge_mto'):
                self.l_items.addItems (value_mto)
            for value in self.db.sql.execute ('SELECT mark FROM cartridge'):
                self.l_items_2.addItems (value)
        def item_delete (self, item):
            print (f'First list: {item.text ()} ')
        def item_delete_2 (self, item):
            print (f'Second list: {item.text ()} ')
    if __name__== '__main__':
        app= QtWidgets.QApplication (sys.argv)
        application= App ()
        application.show ()
        sys.exit (app.exec ())