Lukis triya
Lukis triya

Reputation: 1

PyQt : get a value from combobox when a button pressed

I use qt designer and convert it from *.ui to *.py, I want to make application for send and receive serial data,,

i use combobox to alow user for setting serial comةunication

    self.ui.comboBox_2.addItems(['2400','4800','9600','19200'])

my question is how can i get value from combobo_2 to fill serial buadrate when I click a button

this is my code

    self.connect(self.ui.comboBox_2, QtCore.SIGNAL('activated(QString)'),ser.baudRate())

and get an error

File "mainw.py", line 18, in press_2 self.connect(self.ui.comboBox_2, QtCore.SIGNAL('activated(QString)'),ser.baudRate()) AttributeError: 'Serial' object has no attribute 'baudRate'

Upvotes: 0

Views: 5573

Answers (1)

scottiedoo
scottiedoo

Reputation: 309

Your question about using a button to get the value from the combo box is different than what you are currently doing which is using a signal directly from when an value in the combo box was selected.

Your error is related to something else, it looks like in your signal you are calling a function "ser.baudRate()" but you have to pass in a function object, as it will pass in whatever "ser.buadRate()" returns. Which probably isn't a function. I'm not sure what that function returns. In any case, here is some ideas:

Using a button If you want to use a button, then you would write something like this:

self.connect(self.ui.myButton, QtCore.SIGNAL('clicked()'), self.updateBaudRate)
def updateBaudRate(self):
    # get value from combo box
    rate = str(self.ui.comboBox_2.currentText()) # convert to string otherwise you will get a QString which is sometimes not friendly with other tools
    ser.baudRate(rate)

Using the combo box signal

self.connect(self.ui.comboBox_2, QtCore.SIGNAL('currentIndexChanged(QString)'), self.updateBaudRate)

def updateBaudRate(self, rate):
    ser.baudRate(str(rate)) # again convert to string as it my not accept a QString

You could use partial from the functools module or use a lambda instead of writing a function for the signal, but this is just for example.

You probably also want to use the "currentIndexChanged" signal instead of "activated" as "currentIndexChanged" will only emit when the value has changed, otherwise it will signal even if the user didn't select a different value in the combo box.

Upvotes: 4

Related Questions