Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Direct stdio to combo box
Forum Updated to NodeBB v4.3 + New Features

Direct stdio to combo box

Scheduled Pinned Locked Moved Language Bindings
4 Posts 2 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Blackbear
    wrote on last edited by
    #1

    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,

    1 Reply Last reply
    0
    • jazzycamelJ Offline
      jazzycamelJ Offline
      jazzycamel
      wrote on last edited by
      #2

      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)

      For the avoidance of doubt:

      1. All my code samples (C++ or Python) are tested before posting
      2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
      1 Reply Last reply
      0
      • B Offline
        B Offline
        Blackbear
        wrote on last edited by
        #3

        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,

        1 Reply Last reply
        0
        • jazzycamelJ Offline
          jazzycamelJ Offline
          jazzycamel
          wrote on last edited by
          #4

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

          For the avoidance of doubt:

          1. All my code samples (C++ or Python) are tested before posting
          2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved