2

I wrote an application with Pyside(QtPy). The application allows the user to: 1. Scan a path - Application will scan a default path and display the existing folders in that path as a list of check boxes. 2. User is able to select multiple folders and than execute a process(button) on all of those folders.

In the background: when the user hit 'process', I scan the check boxes list and create a new list only for the selected folder (the 'checked' check boxes or 'user selected folders').

The actual process to those folders will be running in a loop in a QThread class.

The challenge is - How can my QThread use the 'selected folders list' from the main UI method???? At this point the only way I was able to make it work, is by making this list a global parameter. Main UI method set values to the global list, QThread can read that global list.

I've read all possible documentation on signals and slots, and I have no problem sending anything I want from the Qthread to the main UI, but I couldn't find a decent explanation for sending data TO a thread.

Thank you.

Arnon_Af
  • 21
  • 2
  • 1
    You should be a le to extend [this](http://stackoverflow.com/a/35534047/1994235) answer so that data is included with the signal from the main thread to the qthread (it works exactly the same as the reverse direction you've said you know how to do) – three_pineapples Oct 29 '16 at 00:51
  • Hey, Thank you for your reply, so just to make sure I understand, based on this: "...put your code in a subclass of QObject and move that object to a standard QThread instance..." I need to change my current code that uses "run" method to a QObject that later I will moveToThread. – Arnon_Af Oct 30 '16 at 07:15
  • Ok. I moved my entire QThread "Run" method content to a new run method in myWorker and connected it to the 'started' signal of the thread and it works like a charm. But than I noticed I can do the same list assign with my current implementation. Quite embarrassing how simple it is... I could simply do something like: self.myQThread.list = self.myUIlist and now my the run method in the thread sees that list. – Arnon_Af Oct 30 '16 at 10:30
  • Yes that's also an option. Just make sure you aren't modifying the list from the main thread while the thread works on it. http://stackoverflow.com/q/6319207 for details on lists and thread safety. Alternatively, you could make a copy of the list in your run method before using it if that works for your app. – three_pineapples Oct 30 '16 at 19:56
  • Hey @Arnon_Af how exactly were you able to send your list to the thread itself. I am trying to update my thread with some list values from lineEdit but I am not having any luck, I am getting confused on how to go about this. – Aboogie Mar 22 '17 at 20:58
  • Hi @Aboogie, below – Arnon_Af Mar 27 '17 at 12:54

1 Answers1

0

Actually, I ended up with a very simple solution. I set my list, in a new list, in the thread object.

self.runningProcess = clMyThread()  #This is my thread object
self.runningProcess.ThreadList = self.MainList

self.MainList will be whatever list you got in the UI as the input while the ThreadList is the same list in the thread itself.

So when you what to do something to that list in the thread run function, you can:

for item in self.ThreadList:
    do_something()
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Arnon_Af
  • 21
  • 2
  • Hey, thanks, I took sort of the same approach, but it seems that because I am running a loop in the background thread, my computer is heating up really fast, is there a way to handle this, without running the processor crazy? – Aboogie Mar 28 '17 at 14:15
  • What kind of process are you running there? – Arnon_Af Sep 27 '17 at 18:20