Grumeau
Grumeau

Reputation: 11

How to setup 2 QDoubleSpinBox depending on each other?

The first combobox holds a number in a currency A and the second one holds the same amount but in currency B. What I want : When I enter a number in the 1st one, the 2nd one must be set automatically to the number converted in the other currency and vice-versa. I use Ubuntu 16.04, Python 3.5.1, Qt 4.8.7. The 2 QComboBox are created with Qt Designer Code :

@pyqtSignature("double")
def on_dspPuAchEur_valueChanged(self, double):
    """ updaye prAchatDT according to prAchatEur"""
    self.dspPuAchDT.setValue(2.2*self.dspPuAchEur.value())

@pyqtSignature("double")
def on_dspPuAchDT_valueChanged(self, double):
    """ update prAchatEur according to prAchatDT"""
    self.dspPuAchEur.setValue(self.dspPuAchDT.value()/2.2)

2.2 is the conversion factor which will be a variable in the future. min is 0 for the 2 combobox, max is 50000 (far above the real max), step is 1.0. It works fine from dspPuAchEur to dspPuAchDT but it does not work in the opposite sense : the step is 0.99 in place of 1. When I try to edit manually the field, the displayed digit is not the one I've just entered. It's always minus 1 (If I press the '5' key, I get a '4').

Does somebody have any idea about this behavior ? Thanks.

Upvotes: 1

Views: 85

Answers (1)

Martin Höher
Martin Höher

Reputation: 735

I think this could be due to both spin boxes playing "ping pong". A change of value of dspPuAchDT causes it's valueChanged() singal to be emitted, which in turn updates dspPuAchEur, which in turn emits it's valueChanged() signal with in turn leads to an update of dspPuAchDT. Due to rounding, the value you enter seems to change immediately.

As a workaround, you could block emitting signals while updating the peer widget:

@pyqtSignature("double")
def on_dspPuAchEur_valueChanged(self, double):
    """ updaye prAchatDT according to prAchatEur"""
    self.dspPuAchDT.blockSignals(True);
    self.dspPuAchDT.setValue(2.2*self.dspPuAchEur.value())
    self.dspPuAchDT.blockSignals(False);

@pyqtSignature("double")
def on_dspPuAchDT_valueChanged(self, double):
    """ update prAchatEur according to prAchatDT"""
    self.dspPuAchEur.blockSignals(True);
    self.dspPuAchEur.setValue(self.dspPuAchDT.value()/2.2)
    self.dspPuAchEur.blockSignals(False);

Upvotes: 1

Related Questions