[python] How do you get the current text contents of a QComboBox?

Using pyqt4 and python 2.6, I am using a qcombobox to provide a list of options. I am having problems with using the selected option. I have been able to use a signal to trigger a method when the option is selected, but the problem is that when the user clicks run, the contents of several of these comboboxes need to be taken into account. So basically I need to get the selected contents of a combobox as a string. Thus far I have only been able use this:

print combobox1.currentText()

to get this:

PyQt4.QtCore.QString(u'Test Selection2')

when all I really want is the 'Test Selection' bit, any ideas? My combo box was made like this:

combobox1 = qt.QComboBox()
combobox1.addItems(['Test Selection1', 'Test Selection2'])
mainLayout.addWidget(combobox1, 0, 0)

This question is related to python pyqt pyqt4 python-2.6 qcombobox

The answer is


You can convert the QString type to python string by just using the str function. Assuming you are not using any Unicode characters you can get a python string as below:

text = str(combobox1.currentText())

If you are using any unicode characters, you can do:

text = unicode(combobox1.currentText())

Getting the Text of ComboBox when the item is changed

     self.ui.comboBox.activated.connect(self.pass_Net_Adap)

  def pass_Net_Adap(self):
      print str(self.ui.comboBox.currentText())

If you want the text value of a QString object you can use the __str__ property, like this:

>>> a = QtCore.QString("Happy Happy, Joy Joy!")
>>> a
PyQt4.QtCore.QString(u'Happy Happy, Joy Joy!')
>>> a.__str__()
u'Happy Happy, Joy Joy!'

Hope that helps.


PyQt4 can be forced to use a new API in which QString is automatically converted to and from a Python object:

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

With this API, QtCore.QString class is no longer available and self.ui.comboBox.currentText() will return a Python string or unicode object.

See Selecting Incompatible APIs from the doc.


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to pyqt

How do I plot only a table in Matplotlib? How to install PyQt4 on Windows using pip? How to install PyQt4 in anaconda? Convert pyQt UI to python Background thread with QThread in PyQt How do you get the current text contents of a QComboBox? How to change the color of the axis, ticks and labels for a plot in matplotlib Linking a qtDesigner .ui file to python/pyqt?

Examples related to pyqt4

How to install PyQt4 on Windows using pip? How to install PyQt4 in anaconda? ImportError: No module named PyQt4 How to embed matplotlib in pyqt - for Dummies Background thread with QThread in PyQt How do you get the current text contents of a QComboBox? How to get text in QlineEdit when QpushButton is pressed in a string?

Examples related to python-2.6

Suppress InsecureRequestWarning: Unverified HTTPS request is being made in Python2.6 How to fix symbol lookup error: undefined symbol errors in a cluster environment Python readlines() usage and efficient practice for reading sort dict by value python Visibility of global variables in imported modules bash: pip: command not found How to make an unaware datetime timezone aware in python Get all object attributes in Python? How to convert a set to a list in python? How do you get the current text contents of a QComboBox?

Examples related to qcombobox

How do you get the current text contents of a QComboBox? QComboBox - set selected item based on the item's data How can I get the selected VALUE out of a QCombobox?