While coding a medium to large scale program for the first time I tried different ways of handling a lot of variables. As I find that examples on this are quite rare on the interwebs I will try to give some mini-examples on things that work quite well.

What I use: Ubuntu 14.04, Python 3.4, PyQt5, pyserial

What I did so far:

So far I implemented multiple classes of which some run in separate QThreads in order to avoid that my hardware read and writes mess up the GUI by e.g. freezing it. The Qthreads get their required variables once they are initialized as shown in the following example:

class ThreadObject(QObject):
    started = pyqtSignal()
    finished = pyqtSignal()

    def __init__(self, var1, var2):
        super(ThreadObject, self).__init__()
        self.var1 = var1
        self.var2 = var2

        self._isRunning = True

    def stop(self):
        _isRunning = False

    def run(self):
        # do something with the var's and send the results to where they are needed while _isRunning is True

class UIelement(QWidget):
    # initialize, create UI and so on
    def clicksomebutton(self):
        self.obj = ThreadObject(self.var1, self.var2) # handle copies of vars to object
        self.thread = = QThread(self, objectName='workerThread')
        self.obj.moveToThread(self.thread)
        # connect start, stop, run, etc.
        self.thread.start()

This part works so far. Now, if I want to share variables from one class to another I tend to also use signals, that are mostly emitted after some QSpinBox or QDoubleSpinBox is editingFinished or some QPushButton is clicked. But I find it rather fiddly for more then a few dozen variables. On the other hand, I don't have to worry a lot about synchronization as long as I'm careful while creating them.

So I started exploring my options and came up with basically three alternatives on handling variable 'workspaces' across a module. As I cannot grasp which way 'is the best' I would like to ask for the advice/opinion of people who are more experienced.

  • Signal/Slots
    • Pro(s): almost no issues with not-synchronized, ?
    • Cons: lot of code, bad readability when getting complex, ?
  • globals
    • Pro(s): ?
    • Cons: often told that they are bad, ?
  • use dedicated Qobject as container for variables and make each class inhere it, ?
    • Pro(s): seems very clear to read, ?
    • Cons: possible issues with synchronization?, ?
  • ?
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.