0

Am having much trouble splitting PyQt code:

main.py

(PyQt modules)
from titles import *
appl = QApplication(sys.argv)
from main import Ui_MainWindow

class Main(QMainWindow):
    def __init__(self, parent=None):
        super(Main, self).__init__(parent)
        QMainWindow.__init__(self)

        self.u = Ui_MainWindow()
        self.u.setupUi(self)     

        Titles(self)                   

titles.py

import sys
(PyQt modules)
(dbconnections)        

class Titles():
    def __init__(self, a):         #<-- APP IS PASSED AS ARGUMENT AND NOW CALLED 'A'

    a.u.table.setModel(titles)
    a.u.lineEdit.setText("Titles Init")
    a.u.add.clicked.connect(titles.insertRow)

class TitlesTableModel(QSqlTableModel):
    def __init__(self):
        QSqlTableModel.__init__(self)

        self.setTable("titles")
        self.setEditStrategy(self.OnFieldChange)
        self.select()   

    def insertRow(self):
        return self.insertRecord(-1, self.record())
        a.u.lineEdit.setText("Insert Title")  

titles = Titles()     

Running main.py loads all data. QPushButton inserts a row, but doesn't set lineEdit to "Insert Title", because "a" isn't defined globally. Mostly tried creating a function in titles.py, triggered when main.py loads, looking like:

a = 0                           #<-- THIS WAS A LAST STRAW AS WARNED BY RESEARCHING OTHERS, BUT AM LOST
def start(app): 
    global a
    a = app
    Titles(a); TitlesTableModel(a)   #<-- EVEN THOUGH TITLES.PY IS IMPORTED, IT DIDN'T INCLUDE THE APP REFERENCE, SO AM TRYING TO 'REFRESH' THE TITLESTABLEMODEL

...with Titles & TitlesTableModel requiring an extra argument (self, a)

This loads data & functions, but again, insertRow doesn't update lineEdit.

Other attempt change Songs class to

class Songs():
    def __init__(self, a):        

    titles = Titles(a)
    ...(rest the same)

...and removing titles=Titles() from below the model definition. This again, shows data, but doesn't update lineEdit when pressing 'Add'.

Ultimately, it feels titles.py needs to have 'from main import *', but the main applications instance is defined after titles.py is called, and importing main.Main creates a recursion. Have tried inheriting multiple times via 'from main import Main', & writing 'class Songs(Main)' (so Songs can use the UI without passing a reference), but again, recursion occurs. Nine hours today plus three weeks prior looking at others, so am really stumped. Others somewhat recommended using a config file of even 'builtin', but that looks very bad.

Regards

user2422819
  • 177
  • 13

2 Answers2

0

In PyQt, classes generally use Signals to communicate between one another, especially when one class inherits from QWidget and the other does not inherit from that, as you've demonstrated by connecting signals (albeit wrongly, or at least you're missing bits and pieces of your code here on SO).

However, your insertRow() -> lineEdit method as it stands will never be called because it follows a return statement, meaning that the lineEdit part will never be hit. But I would be surprised if this fixed the problem.

Also, I would consider redesigning (refactoring) your code from the grounds up. Is there really a reason you have a different Titles() class?

While this is shameless self-promotion, I think you might benefit from my course on YouTube that deals with building Python applications using PySide (which is nearly identical to PyQt) - I discuss cross-thread (cross-class) communication a fair bit - link is http://youtube.com/Deusdies2

Bo Milanovich
  • 7,995
  • 9
  • 44
  • 61
0

Your code has several issues, but the main problem is the snippet:

def insertRow(self):
     return self.insertRecord(-1, self.record())
     a.u.lineEdit.setText("Insert Title") 

as you can see you're returning from the function before the line a.u.lineEdit.setText("Insert Title") get excecuted. Hence, this function willl never change the text of your QLineEdit.

Change your code b

 def insertRow(self):
     a.u.lineEdit.setText("Insert Title")        # First change text.
     return self.insertRecord(-1, self.record()) # Then insert record and return.

On the other hand: If you are working with global variables (a bad practice, I have to say) why are you passing it as arguments? Try to not use global variables at least is absolutly necesary.

Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60