How to setup 2 QDoubleSpinBox depending on each other? - python

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.

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);

Related

Launchpad Mini Mk3—Adding a fourth mode to session that's more than one row tall

What I'm trying to do:
Add a fourth mode, let's call it SendsMode, to the Session page of my Launchpad—AKA when hitting the "Stop-Solo-Mute" button a fourth time, it would cycle into SendsMode.
In this mode, the bottom row(s) of buttons would turn the sends on/off for each track within the Session Ring.
Where I am now:
I have functioning code that adds SendsMode, and works for one row of sends perfectly. It changes with movements of the Session Ring.
What I need help with:
I can't get it to work for more than one send at a time (the buttons go blank, but don't do anything when pressed, not reflect changes in the values done in Ableton).
Full code is here: https://github.com/jonniepeller/launchpad-mini-mk3-augmented
The relevant bit is adding the following to _create_stop_solo_mute_modes:
self._stop_solo_mute_modes.add_mode(
u"send_controls",
AddLayerMode(self._mixer, Layer(send_controls=bottom_x_rows)),
cycle_mode_button_color=u"Mixer.SendControls",
)
self._stop_solo_mute_modes.selected_mode = u"send_controls"
self._stop_solo_mute_modes.set_enabled(True)
_set_send_controls in Novation's mixer component wasn't appropriate for multiple sends—it was built and used for one thing per track, like mute, solo, stop.
I implemented my own MixerComponent and NovationBase with an adapted version of what they have for the Launch Control XL.
This is the key bit that fixed my issue:
def set_send_controls(self, controls):
num_sends = len(self._song.return_tracks)
for channel_idx, channel_strip in enumerate(self._channel_strips):
send_controls = []
for send_idx in range(num_sends):
if controls:
try:
button = controls.get_button(send_idx, channel_idx)
except:
logger.info("Tried getting a button out of range")
send_controls.append(button)
channel_strip.set_send_controls(send_controls)

How do I update the data associated with a QListWidgetItem from the QListWidget in PyQt?

I have a QListWidget that has 10 QListWidgetItems. When each of those QListWidgetItems is created I do something like this:
item = QtGui.QListWidgetItem("Text to Show")
item.setData(36, "A specific value that is used later")
self.ui.my_list.addItem(item)
Now, later in the application, after a user clicks a button, I want to update the text "A specific value that is used later", for the item that is selected. I have attempted to do this
ndx = self.ui.my_list.currentRow()
self.ui.my_list.item(ndx).setData(36, "Updated!")
The problem is, this doesn't work. It doesn't throw any errors, but the data is just gone. In my button press signal I have this code to see the value before and after the reassignment:
ndx = self.ui.my_list.currentRow()
print "Before:", self.ui.my_list.item(ndx).data(36).toPyObject()
self.ui.my_list.item(ndx).setData(36, "Updated!")
print "After:", self.ui.my_list.item(ndx).data(36).toPyObject()
This outputs:
Before: A specific value that is used later
After:
How can I properly change the data so that it is saved back to the QListWidgetItem?
You may want to check that the role value your using is a valid user role, otherwise it may be altered internally. I write in c++, but I use the QListWidget and QListWidgetItem(s) frequently, and this was a problem I encountered early on. I imagen that the functionality is similar regardless of the language. According to the documentation here, the first user role that can be used is 0x0010. So to tie it to your example, you might try the following:
item.setData(0x0010, "A specific value that is used later")
For additional item roles you would use 0x0011, 0x0012, 0x0013, etc.

How to get a value from QSpinBox, how to disable NextButton in QWizard

Python 3.3, PyQt 4.8.4. My knowledge of PyQt and Python (and English, I think;)) isn't good, so there are many questions in my studies. I made a draft in Qt Designer, still I can't understand how to make it work properly. I have wizard, on the first page - 2 QSpinBoxes, second - generated quantity of spinboxes, equals to the entered values in boxes on the first page. Tried to search about signals and slots, but still can't understand.
How to get this value inside my program, to use it for generation of spinboxes on the 2nd page?
Of course, I need to keep other pages out of reach while this first value isn't correct. I think that can be realized by keeping NextButton unavailable, but how - I don't know. Need kind of help!)
My code itself
http://pastebin.com/UxvzFvJR
Launcher was made separately http://pastebin.com/2sYtyg9z
1: Move your spinboxes generate code(line list_x = [] to f += 1) to page2's initializePage. Dirty but works:
class Ui_Wizard(object):
def addSpins(self):
list_x = []
...
...
self.wizardPage2.initializePage = self.addSpins
2: Register them as mandatory field:
self.wizardPage1.registerField("cols*", self.spinBox_col)

PyQt QSpinBox update range depending on the value of other spinbox

i am using pyqt4 to develop a GUI for the first time;
I have a spinbox, and I would like the range of values allowed in it to be dependant on the value another spinbox. for example, the maximum value allowed in the first spinbox should be equal to the value of the second spinbox.
I thought this may be possible using a valueChanged() signal to call a method that is something like:
def function
max = spinbox2.value()
spinbox1.setMaximum(max)
but that didnt work, does anyone know how to do this?
thank you
You have not shown your code where you make the connection between the 'valueChanged' signal of spinbox2 with the function. Are you making that connection ?. Also the code you have given for function seems to be incomplete.
You can try something like this:
spinbbox2.valueChanged.connect(handler)
# Or this which works for Python 3.5
spinbbox2.valueChanged[int].connect(handler)
# The function handler called is called whenever
# value in spinbox2 is changed and parameter to
# handler is the new value of spinbox2
def handler(value):
spinbox1.setMaximum(value)

Creating a GUI Calculator in python similar to MS Calculator

I need to write a code that runs similar to normal calculators in such a way that it displays the first number I type in, when i press the operand, the entry widget still displays the first number, but when i press the numbers for my second number, the first one gets replaced. I'm not to the point in writing the whole code yet, but I'm stuck at the point where when I press the 2nd number(s), the first set gets replaced. I was thinking about if key == one of the operands, than I set the num on the entry as variable first, then I do ent.delete(0,end) to clear the screen and ent.insert(0,first) to display the first num in the entry widget. Now I don't know what to do to clear the entry widget when the 2nd number(s) is pressed.
What you need here is a concept of state. Each time a key is pressed, you check the state and determine what action to take.
In the initial state, you take input of numbers.
When an operand button is pressed, you store the operand, and change the state.
When another number is pressed, you store the number, clear the numeric input, and start the number input again.
Then when the equals button is pressed, you perform the operation, using your stored number and operand with the current number in the numeric input.
Note that with a dynamic language like Python, instead of using a variable and if statements to check the state, you can just change the function that handles key/button pressed depending on what the state is.

Categories

Resources