Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) 
like image 375
Ben Avatar asked May 19 '11 16:05

Ben


People also ask

How do I get text from QComboBox?

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

What is a QComboBox?

A QComboBox provides a means of presenting a list of options to the user in a way that takes up the minimum amount of screen space. A combobox is a selection widget that displays the current item, and can pop up a list of selectable items. A combobox may be editable, allowing the user to modify each item in the list.


1 Answers

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()) 
like image 163
sateesh Avatar answered Oct 06 '22 15:10

sateesh