Direct stdio to combo box
I am using this;
@ def init_combo(self):
recent = ResDir + '/combo_list.sh'
process = subprocess.Popen([recent], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
combo_list = process.communicate()
for workspace in combo_list:
The script, recent_list.sh, returns several lines of text with no spaces. My intent is to add each line as an item to the combo box. What I am getting is each character added as a separate item.
How do I get a string into my combo box instead of characters?
I'm guessing that what you're getting back from communicate() is not a list but a single string containing the items from your bash script separated by '\n' characters. If you iterate over a string it will yield one character per iteration. If you want to break that string into a list just do the following:
A full working example using "QProcess":http://qt-project.org/doc/qt-4.8/qprocess.html rather than Python process is as follows:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
def init(self, parent=None, **kwargs):
QWidget.init(self, parent, **kwargs)
l=QVBoxLayout(self) self._combo=QComboBox(self) l.addWidget(self._combo) self._process=QProcess( self, readyReadStandardOutput=self.read ) self._process.start("./test.sh") @pyqtSlot() def read(self): for item in self._process.readAllStandardOutput().split("\n"): if not len(item): continue self._combo.addItem(str(item))
from sys import argv, exit
a=QApplication(argv) w=Widget() w.show() w.raise_() exit(a.exec_())
'test.sh' consists of the following:
Hope this helps ;o)
Split did it.
I use QProcess for external process that take a noticeable amount of time. Otherwise I use subprocess because the code is much simpler.
Glad it worked. Can you mark the thread as [solved] if thats the case ;o)