Reputation: 71
I have a PyQt GUI using Python 2.7 and QT 4.7 that opens a dialog called by a pushbutton. I can pass values between the dialog and the main GUI just fine for the most part, until it comes to a QSpinBox in the dialog box.
The class that defines the dialog is like so:
class BuyDialog(QDialog):
def __init__(self):
QDialog.__init__(self)
global ci, space, cash, current, price, qtyToBuy
self.ui = Ui_buyDialog() # Set up the user interface from Designer.
self.ui.setupUi(self)
for i in range(0, len(ci)):
item = str(ci[i][0])
price = str(ci[i][1])
self.ui.itemsComboBox.addItem(item)
price = str(self.getPrice())
gPrice = "$" + price
print gPrice
self.ui.priceFieldLabel.setText(gPrice)
self.ui.itemsComboBox.currentIndexChanged['QString'].connect(self.updateItems)
self.ui.availableSpaceFieldLabel.setText(space)
canBuy = str(funcs.youCanAfford(cash, price))
self.ui.canAffordFieldLabel.setText(canBuy)
qtyToBuy = self.ui.buySpinBox.value()
The code that handles the dialog itself is
def buyDialog(self):
global current, price, qtyToBuy
dialog = BuyDialog()
result = dialog.exec_()
if result:
dialogResult = (current, price, qtyToBuy)
print dialogResult #debug
return dialogResult
current
comes from a combo box in the dialog, and price
comes from a list lookup against current
. I know the dialog updates correctly, as the values returned for current
and price
are correct. However, qtyToBuy
always returns 0. The only way I've gotten it to return anything different is by calling setValue()
on it when it's initiated. Everywhere I've looked, I get the impression I'm the only person that's had this problem, as I can find nothing else regarding this issue. Does anyone have any idea what the problem is?
Upvotes: 0
Views: 5158
Reputation: 3945
As @Frank pointed out that since the value()
of the spinBox is retrieved before the dialog is shown, the user input in the spinBox will not affect the value of qtyToBuy
, it will always give you the default value of spinBox (which is 0 in your case). To retrieve the user specified value from the spinBox, you should retrieve the value after the dialog is closed (i.e. the user presses Ok on the dialog)
def buyDialog(self):
global current, price, qtyToBuy
dialog = BuyDialog()
result = dialog.exec_()
qtyToBuy = dialog.ui.buySpinBox.value() # add this line here
if result:
dialogResult = (current, price, qtyToBuy)
print dialogResult #debug
return dialogResult
Now the print
statement will print the value which was the value in spinBox when the user pressed Ok on the dialog.
Upvotes: 1