Am struggling to comprehend how to split code in (Py)Qt. The aim is to have the design & navigation tabs in QMainWindow, each tab triggering code in other files. So far it only launches with the ActionClass in the same document / putting in an external document causes 'app not defined' when clicking the tab. The below works without errors, but is clunky.
class Main(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.u = Ui_MainWindow()
self.u.setupUi(self)
self.u.tabs.currentChanged.connect(self.TabsChanged)
def TabsChanged(self, i):
if i == self.u.tabs.indexOf(self.u.tabFirst): ActionClass.__init__
class ActionClass(Main):
def __init__(self):
app.u.lineEdit.setText("test")
app = Main()
app.show()
sys.exit(app.exec_())
The examples I keep seeing have all code in one document. Is there another way to do this e.g. where the ActionClass is in another file/writing u.lineEdit.setText instead of app.u.lineEdit.setText. It seems inheritance & an instance of Main can't be accessed from the ActionClasses doc, so I can't see how they would communicate back to the Main?
Much appreciated
As suggest #M4rtini you can separate your code into python modules. And then import them (use them) in your main module.
For instance the code you posted can be separated in to files:
# actions_class.py
class ActionClass(Main):
def __init__(self):
app.u.lineEdit.setText("test")
and
# main.py
from action_class import ActionClass # This line no need much explanation ;)
class Main(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.u = Ui_MainWindow()
self.u.setupUi(self)
self.u.tabs.currentChanged.connect(self.TabsChanged)
def TabsChanged(self, i):
if i == self.u.tabs.indexOf(self.u.tabFirst): ActionClass.__init__
app = Main()
app.show()
sys.exit(app.exec_())
In order to understand how import works see the link I left you above.
More explanation
Lest's see:
The correct way of executin code inside a __init__ method is creating an instance. See the example below.
class A:
def __init__(self):
print("Executing A.__init__")
print("Doing things wrong")
A.__init__ # This don't print enything
print("Doing things well")
A() # This works as expected.
So, you line reads:
if i == self.u.tabs.indexOf(self.u.tabFirst): ActionClass.__init__
and should reads:
if i == self.u.tabs.indexOf(self.u.tabFirst): ActionClass()
On the other hand, is a bad practice put code that's not for initialize the instance inside the __init__ methods.
If you don't need the instance but yet you want to store the functions inside a class (something like a c++ namespace) you creating
use #staticmethod decorator.
class A:
#staticmethod
def foo():
print("Oh, wow, a static method in Python!")
A.foo()
So, your ActionClass could be rewritten as:
class ActionClass(Main):
#staticmethod
def do_action:
app.u.lineEdit.setText("test")
ans then you can use it like this:
if i == self.u.tabs.indexOf(self.u.tabFirst): ActionClass.do_action()
Related
I have an existing class which inherits from the PyQT5 classes QWidget and Ui_Dialog.
I want to use some functionality of the class which is not at all related to the GUI. I have therefore tried to mock out the relevant bits, such as the __init__ function which has a lot of GUI-specific code. There is also a specific method in the class which needs overriding (_getSqlQuery).
class TestPyQgs(unittest.TestCase):
def test_comment_parsing(self):
query = "SELECT * FROM test"
with patch.object(DlgSqlWindow, "__init__", lambda x, y, z: None):
with patch.object(DlgSqlWindow, "_getSqlQuery", return_value=query):
a = DlgSqlWindow(None,None)
self.assertEqual(a.parseSQL(), query)
Unfortunately this doesn't work because anything that inherits from QtWidget (as DlgSqlWindow does) has to call init on the Base Class, so I get super-class __init__() of type DlgSqlWindow was never called. Is there no sane way of doing this? Otherwise should I try and refactor the code to split GUI from functionality, or do a unit test on the GUI as well, which I would rather not do as I want the unit test to be as tight as possible.
One possible solution is to just call the super QWidget who doesn't have much logic:
import unittest
from unittest.mock import patch
from PyQt5 import QtWidgets
class Ui_Dialog(object):
def setupUi(self, dialog):
pass
class DlgSqlWindow(QtWidgets.QWidget, Ui_Dialog):
def _getSqlQuery(self):
pass
def parseSQL(self):
return "SELECT * FROM test"
def init(self, *args):
QtWidgets.QWidget.__init__(self)
# self.setupUi(self)
class TestPyQgs(unittest.TestCase):
def test_comment_parsing(self):
app = QtWidgets.QApplication([])
query = "SELECT * FROM test"
with patch.object(DlgSqlWindow, "__init__", init):
with patch.object(DlgSqlWindow, "_getSqlQuery", return_value=query):
a = DlgSqlWindow(None, None)
self.assertEqual(a.parseSQL(), query)
if __name__ == "__main__":
unittest.main()
But as you point out, the optimal solution is to refactor your code since it seems that you are mixing business logic with the GUI which, as you can see, causes several inconveniences and is not maintainable.
I've implemented a timer using QTimer inside a Singleton. The Singleton is implemented using the Borg pattern. If I start a QTimer with single shot inside a function of the Singleton it won't be executed. The same call in a function outside the Singleton works well.
This is the code:
#!/usr/bin/env python
import sys
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QApplication
class Borg():
_shared_state = {}
def __init__(self):
self.__dict__ = self._shared_state
class Timers(Borg):
def __init__(self):
Borg.__init__(self)
def update_not_working(self):
QTimer().singleShot(2000, Timers().update_not_working)
print('update not working')
def update_working():
QTimer().singleShot(2000, update_working)
print('update working')
if __name__ == '__main__':
app = QApplication(sys.argv)
print('start timer')
Timers().update_not_working()
update_working()
sys.exit(app.exec_())
The output is (no error, no exception):
start timer
update not working
update working
update working
....
Why is one call working and the other not? Is there something wrong with my implementation of the Borg or with the usage of QTimer?
print self in update_not_working and print Timers() in update working show that the Timers object before the event loop started is different from the one within:
update not working
<__main__.Timers instance at 0xb52162cc>
update working
<__main__.Timers instance at 0xb52162cc>
update working
<__main__.Timers instance at 0xb521650c>
update working
<__main__.Timers instance at 0xb521650c>
update working
<__main__.Timers instance at 0xb521650c>
update working
<__main__.Timers instance at 0xb521650c>
#classmethod should help here because it allows to call the method on an instance OR on the class as you do in the single shot statement.
Compare: When should I use #classmethod and when def method(self)?
This is actually just a matter of normal garbage-collection.
If you add some debugging code to your example like this:
class Timers(Borg):
def __init__(self):
Borg.__init__(self)
print('init:', self)
def update_not_working(self):
QTimer().singleShot(1, Timers().update_not_working)
print('update not working')
def __del__(self):
print('deleted:', self)
it will produce output like this:
start timer
init: <__main__.Timers object at 0x7f194bf53eb8>
init: <__main__.Timers object at 0x7f1940cfdb00>
deleted: <__main__.Timers object at 0x7f1940cfdb00>
update not working
deleted: <__main__.Timers object at 0x7f194bf53eb8>
update working
update working
As you can see, both of the Timers instances get deleted long before the single-shot timer sends its timeout() signal. And when they deleted, their instance-methods get deleted as well, which will automatically disconnect them from the signal. This shows that the Borg pattern does not produce a true singleton: it just mimics some of the behaviour of one.
If you use a real singleton class, like this:
class Timers2(object):
_instance = None
def __new__(cls):
if Timers2._instance is None:
Timers2._instance = object.__new__(cls)
return Timers2._instance
def update_not_working(self):
QTimer().singleShot(2000, Timers2().update_not_working)
print('update not working')
your example will work as expected. This is because there is only ever one instance, and it is kept alive by being cached as a class attribute.
Finally, the reason why the update_working() succeeds, is because it is a globally defined function. As with the class attribute, this ensures that it won't get garbage-collected until the script completes.
class Borg():
_shared_state = {}
def __init__(self):
self.__dict__ = self._shared_state
class Timers(Borg):
def __init__(self):
Borg.__init__(self)
#classmethod
def update_not_working(cls):
QTimer().singleShot(2000, Timers().update_not_working)
print('update not working')
def update_working():
QTimer().singleShot(2000, update_working)
print('update working')
if __name__ == '__main__':
app = QApplication(sys.argv)
print('start timer')
Timers().update_not_working()
update_working()
sys.exit(app.exec_())
I think my answer is related to this answer but can't quite get my head around it.
I am now realising that my code isn't structured very well, however the current setup is:
main_run.py
app = QtGui.QApplication(sys.argv)
app.processEvents()
ui1 = new_main_ui.Ui_Form_Main()
ui1.show()
sys.exit(app.exec_())
new_main_ui
class Ui_Form_Main(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.setupUi(self)
...etc...
within this class are Qlabel objects which have their text updated as buttons are pressed on the UI.
within new_main_ui there is a call to another module (sqr_pull.py) which does some stuff. Halfway through sqr_pull.py I want to update a Qlabel in my UI, but I can't figure out how to reference the UI instance (ui1) without getting:
NameError: name 'ui1' is not defined
I've tried trying to pass variables through as I go using sys.modules[__name__] as follows:
in main_run: new_main_ui.parent1 = sys.modules[__name__]
in new_main_ui: sqr_pull.parent2 = sys.modules[__name__]
and then in sqr_pull trying to change using `parent2.QLabel.setText("blahblahblah")
but it again doesn't recognise the instance name. What's the best way to do this?
The clean way to give a function access to an object is to pass this object to the function...
# worker.py
def work(ui):
some_result = do_something()
ui.update(some_result)
other_result = do_something_else()
# ui.py
import worker
class Ui(object):
def some_action(self):
worker.work(self)
def update(self, data):
self.show_data_somewhere(data)
Hacking with sys.modules or whatever will only result in some unmaintainable mess.
I have the following code:
class Functions(QObject):
mysig = Signal(filename)
def __init__(self, parent=None):
super(Functions, self).__init__(parent)
self.result = None
def showDialog(self, filename):
self.mysig.emit(filename)
def grabResult(self):
while not self.result:
time.sleep(5)
return result #this is the question
def setResult(self, result):
self.result = result
The other part of the code has this:
class Dialog(QDialog):
anotherSig = Signal(str)
fun = Functions()
def __init__(self, parent=None, filename=filename):
self.filename = filename
#Here it displays a picture based on the filename parameter
def okButtonClicked(self):
text = self.lineedit.text()
fun.setResult(text)
#Tried also this:
self.anotherSig.emit(text)
The Functions() class is called from a worker QThread (not shown here).
I guess my question is this: how do I tell my Functions class that the user has entered the the text and clicked the OK button? I tried connecting that anotherSig Signal, but when I try to do so, Qt complains about QPixmaps not being safe to be set from a different thread, and it doesn't work.
The method that I am using here "works", but I feel it's not very reliable. Plus, it only works when all of the relevant methods in the Functions class are #classmethod - this way, for some reason, it doesn't work. The setResult is called (I added a print statement to make sure), but the grabResult still shows self.result as None.
This code is not working because the call to showDialog is happening on the instantiation of a Functions object that is an attribute of what ever object is off on the other thread. Your fun in Dialog, which you set the result on, is a different instantiation.
To move the results back to the original Functions object I think you need to connect anotherSig of the Dialog object to the setResult function on the Functions object you want to get the results back.
Does something like this work (hard to test this with out a good bit of boiler plate).
class Functions(QObject):
mysig = Signal(filename,Functions)
def __init__(self, parent=None):
super(Functions, self).__init__(parent)
self.result = None
def showDialog(self, filename):
self.mysig.emit(filename,self)
def grabResult(self):
while not self.result:
time.sleep(5)
return result #this is the question
#QtCore.Slot(str)
def setResult(self, result):
self.result = result
def connection_fun(filename,fun):
d = Dialog(filename)
# what ever else you do in here
d.anotherSig.connect(fun.setResult))
Using time.sleep causes your application to freeze. One method for making your class wait is using QEventLoop like this:
loop = QEventLoop()
myDialog.mySignal.connect(loop.quit)
loop.exec_()
The code is along the lines of this:
class Solver(QDialog):
def __init__(self, parent=None):
blabla
def solver(self):
return qlineedit1.text()
class access(QMainWindow):
prda = Solver().solver()
print prda
The problem is that prda is an empty string. If i put "print qlineedit1.text()" in the Solver class, the text is displayed as it should be. However, when "transferred" to a different class, the string is empty. The weirdest part that it is there - if I do type(prda), I get QString type output.
So, how would I get prda to assume the value of qlineedit1.text() ? I was thinking of writing the text to file in Solver class, and then reading it from access class, but there has to be another solution. By the way, the Solver class and the access class are two dialogs.
Help?
Since next code works as expected, all i can think out without seeing more of your code is that your qlineedit1 is empty.
from PyQt4 import QtGui
class Solver(QtGui.QDialog):
def __init__(self, parent=None):
print "in Solver init"
def solver(self):
return "in solver() method"
class access(QtGui.QMainWindow):
prda = Solver()
print prda.solver()
#this will work too:
#prda = Solver().solver()
#print prda
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
acc=access()
acc.show()
sys.exit(app.exec_())