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()[0]
    for workspace in combo_list:
    self.cmbWorkspaces.addItem(workspace)@

    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?

    Cheers,



  • 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:

    @
    combo_list=combo_list.split('\n')
    @

    A full working example using "QProcess":http://qt-project.org/doc/qt-4.8/qprocess.html rather than Python process is as follows:

    @
    import sip
    sip.setapi('QString',2)
    sip.setapi('QVariant',2)

    from PyQt4.QtCore import *
    from PyQt4.QtGui import *

    class Widget(QWidget):
    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))
    

    if name=="main":
    from sys import argv, exit

    a=QApplication(argv)
    w=Widget()
    w.show()
    w.raise_()
    exit(a.exec_())
    

    @

    'test.sh' consists of the following:

    @
    echo Item1
    echo Item2
    echo Item3
    @

    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.

    thanks,



  • Glad it worked. Can you mark the thread as [solved] if thats the case ;o)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.