<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>First of all, if anyone here has access to the documentation pages of PyQt, please read this (expecially the third point).</div><div><br></div><div><br></div><div dir="ltr">I do not know your exact user case, so it's hard to give you specific suggestions, but here's what I'd do.<div><br></div><div>1. do not give any signature to the signal at all:</div><div><br></div><div><div class="gmail_default" style="font-size:12.8px;font-family:tahoma,sans-serif"><span style="font-family:monospace,monospace"> notifyProgress = QtCore.pyqtSignal(object, object)</span><br></div></div><br>This allows you to use None for both arguments, but I'm not familiar with typing in methods for python 3 (to be fair, I didn't even know about it until yesterday, as I mainly use Python2). I did a quick test and it seems to work fine, though.</div><div dir="ltr"><br></div><div dir="ltr"><div dir="ltr">2. use a single parameter that will be sent as a tuple, and unpack it within the method:</div><div dir="ltr"><br></div><div dir="ltr"><span style="font-family:monospace,monospace;font-size:12.8px"> notifyProgress = QtCore.pyqtSignal(object)</span><br></div><div dir="ltr"><span style="font-family:monospace,monospace;font-size:12.8px"> </span><span style="font-family:monospace,monospace;font-size:12.8px"> </span><span style="font-size:12.8px"><font face="monospace, monospace">def updateProgress(self, *args):</font></span><br></div><div><span style="font-size:12.8px"><font face="monospace, monospace"> val, text = args</font></span></div><div><span style="font-size:12.8px"><font face="monospace, monospace"><br></font></span></div><div><span style="font-size:12.8px"><font face="monospace, monospace"> [...]</font></span></div><div><span style="font-family:monospace,monospace;font-size:12.8px"><br></span></div><div><span style="font-family:monospace,monospace;font-size:12.8px"> </span><span style="font-family:monospace,monospace;font-size:12.8px"> self.</span><span style="font-family:monospace,monospace;font-size:12.8px">notifyProgress.emit((None, "Some text"))</span><br></div><div><span style="font-family:monospace,monospace;font-size:12.8px"> self.</span><span style="font-family:monospace,monospace;font-size:12.8px">notifyProgress.emit((i / 2, None))</span><br></div><div><span style="font-family:monospace,monospace;font-size:12.8px"><br></span></div></div><div dir="ltr">3. use an overloaded signal and two separate slots. This is something I was struggling for a lot of time and (thanks to you! :-D) I finally found out how it works, as it is *not* explained exhaustively in the documentation. Also, I think that's what you were referring to.</div><div dir="ltr">As we know, signals can be connected "in two ways" (I'm referring to new-style connections). Normally, you can go by simply connecting the slot and eventually decorating that slot with the signature you are interested in; decoration is usually not strictly required, as slot that are processing invalid data are somewhat "ignored": I always thought it as some kind of try/except mechanism, if at some point within the method something wrong happens, the error is ignored but no exception is thrown; of course, using a decorated slot avoids unnecessary computation for wrong signature arguments.</div><div dir="ltr">Sometimes, though, it's better if not necessary to connect the slot to the specific signature: <font face="monospace, monospace">self.somesignal[signature].connect(slot)</font>.</div><div dir="ltr">What the documentation forgets to explain is that the same _has_ to be done when emitting custom overloaded signals if you are not using the default signature (the first argument).</div><div dir="ltr"><br></div><div><span style="font-family:monospace,monospace;font-size:12.8px"> #in this case, "str" is the default signature</span></div><div dir="ltr"><span style="font-family:monospace,monospace;font-size:12.8px"> notifyProgress = QtCore.pyqtSignal([str], [int])</span><br></div><div dir="ltr"><br></div><div><span style="font-family:monospace,monospace;font-size:12.8px"> [...]</span></div><div><span style="font-family:monospace,monospace;font-size:12.8px"> #the default signature can be omitted</span></div><div><span style="font-family:monospace,monospace;font-size:12.8px"></span><span style="font-size:12.8px"><font face="monospace, monospace"> self.notifyProgress.connect(self.updateProgressString)</font></span></div><div><span style="font-family:monospace,monospace;font-size:12.8px"> self.notifyProgress[int].connect(self.updateProgressInt)</span><span style="font-size:12.8px"><font face="monospace, monospace"><br class="gmail-Apple-interchange-newline"> [...]</font></span></div><div><span style="font-family:monospace,monospace;font-size:12.8px"><br></span></div><div dir="ltr"><span style="font-family:monospace,monospace;font-size:12.8px"> </span><span style="font-family:monospace,monospace;font-size:12.8px"> </span><font face="monospace, monospace">@QtCore.pyqtSlot(str)</font><br></div><div dir="ltr"><div><div dir="ltr"><span style="font-family:monospace,monospace;font-size:12.8px"> </span><span style="font-family:monospace,monospace;font-size:12.8px"> </span><span style="font-size:12.8px"><font face="monospace, monospace">def updateProgressString(self, text):</font></span></div></div><div dir="ltr"><span style="font-family:monospace,monospace;font-size:12.8px"> </span><span style="font-family:monospace,monospace;font-size:12.8px"> </span><span style="font-size:12.8px"><font face="monospace, monospace"> [...]</font></span></div><div dir="ltr"><span style="font-size:12.8px"><font face="monospace, monospace"><br></font></span></div></div><div dir="ltr"><div dir="ltr"><span style="font-family:monospace,monospace;font-size:12.8px"> </span><span style="font-family:monospace,monospace;font-size:12.8px"> </span><font face="monospace, monospace">@QtCore.pyqtSlot(int)</font></div><div dir="ltr"><span style="font-family:monospace,monospace;font-size:12.8px"> </span><span style="font-family:monospace,monospace;font-size:12.8px"> </span><span style="font-size:12.8px"><font face="monospace, monospace">def updateProgressInt(self, val):</font></span></div><div dir="ltr"><span style="font-family:monospace,monospace;font-size:12.8px"> </span><span style="font-family:monospace,monospace;font-size:12.8px"> </span><span style="font-family:monospace,monospace;font-size:12.8px"> [...]</span><font face="monospace, monospace"><span style="font-size:12.8px"><br></span></font><div><span style="font-family:monospace,monospace"><br></span></div><div></div></div><div><span style="font-size:12.8px"><font face="monospace, monospace"> [...]</font></span></div><div><span style="font-family:monospace,monospace;font-size:12.8px"><br></span></div><div><span style="font-family:monospace,monospace;font-size:12.8px"> #again, the default signature can be omitted</span></div><div><span style="font-family:monospace,monospace;font-size:12.8px"> </span><span style="font-family:monospace,monospace;font-size:12.8px"> self.</span><span style="font-family:monospace,monospace;font-size:12.8px">notifyProgress.emit("Some text")</span><br></div><div><span style="font-family:monospace,monospace;font-size:12.8px"> #now, <b>THIS</b>!</span></div><div><span style="font-family:monospace,monospace;font-size:12.8px"> </span><span style="font-family:monospace,monospace;font-size:12.8px"> self.</span><span style="font-family:monospace,monospace;font-size:12.8px">notifyProgress[int].emit(5)</span><br></div><div dir="ltr"><br></div><div>I really think that this is something the documentation should *not* miss. I understand that from the C++ point of view it's pretty obvious that a signal is emitted according to its argument type signature, but this is not that obvious for a common Python programmer.<br></div><div><br></div><div dir="ltr"><div>Obviously, in the last case there's a small drawback: you can't pass None as argument, as it will be converted to the typed signature, giving you an empty string or that infamous random 32bit integer. I don't think it would be an issue in your case, but it is something to keep in mind anyway.</div><div><br></div></div><br>I hope this helps... it helped me :-)<br><br>Maurizio</div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">2018-09-05 12:33 GMT+02:00 J Barchan <span dir="ltr"><<a href="mailto:jnbarchan@gmail.com" target="_blank">jnbarchan@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_extra"><div><div class="h5"><br><div class="gmail_quote">On 5 September 2018 at 09:11, J Barchan <span dir="ltr"><<a href="mailto:jnbarchan@gmail.com" target="_blank">jnbarchan@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div style="font-family:tahoma,sans-serif"><br></div><div class="gmail_extra"><div><div class="m_-306729045778188742gmail-h5"><br><div class="gmail_quote">On 4 September 2018 at 18:08, Maurizio Berti <span dir="ltr"><<a href="mailto:maurizio.berti@gmail.com" target="_blank">maurizio.berti@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">You are defining a specific signature in the signal:</div><div dir="ltr"><span style="font-family:monospace,monospace;font-size:12.8px"><br></span></div><div dir="ltr"><span style="font-family:monospace,monospace;font-size:12.8px"><div style="font-family:tahoma,sans-serif;display:inline" class="gmail_default"></div>QtCore.pyqtSignal(int, str)</span><br></div><br>this means that, despite the types you set in the method <div style="font-family:tahoma,sans-serif;display:inline"></div>(which Qt doesn't know anything of also, as you didn't use the Slot decorator), whenever you emit a None (which is a NoneType, not int, nor str) Qt will try to "translate" it to the valid signature you assigned.</div><div><br></div><div>I don't know exactly why the int is that a high number (always high and always random), but this probably makes sense for some C++ type signature, as it seems to me that the number is always 32bit long and, in my case, always negative.</div><div><br></div><div>Anyway, if you really need to send None, you can use the generic "object" signature in the signal definition, or, in your case, just go with this, assuming the progress will never use negative numbers.</div><div><br></div><div><span style="font-family:monospace,monospace;font-size:12.8px">def updateProgress(self, <div style="font-family:tahoma,sans-serif;display:inline"></div><div style="font-family:tahoma,sans-serif;display:inline"></div>val: int=-1, <div style="font-family:tahoma,sans-serif;display:inline"></div>text: str=''):</span><br></div><span style="font-family:monospace,monospace;font-size:12.8px"> </span><span style="font-family:monospace,monospace;font-size:12.8px">if val is >= 0:</span><br style="font-family:monospace,monospace;font-size:12.8px"><span style="font-family:monospace,monospace;font-size:12.8px"> </span><span style="font-family:monospace,monospace;font-size:12.8px"> self.progressBar.pb.setValue(</span><span style="font-family:monospace,monospace;font-size:12.8px">v<wbr>al)</span><br style="font-family:monospace,monospace;font-size:12.8px"><span style="font-family:monospace,monospace;font-size:12.8px"> </span><span style="font-family:monospace,monospace;font-size:12.8px">if text:</span><br style="font-family:monospace,monospace;font-size:12.8px"><div><span style="font-family:monospace,monospace;font-size:12.8px"> </span><span style="font-family:monospace,monospace;font-size:12.8px"> </span><span style="font-family:monospace,monospace;font-size:12.8px"> self.progressBar.label.</span><span style="font-family:monospace,monospace;font-size:12.8px">setText<wbr>(text)</span> </div><div dir="ltr"><br></div><div>and then emit the text signal using -1 for val.</div><div dir="ltr"><br></div><div>Maurizio</div><div dir="ltr"><br></div></div></div></div></div></div></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="m_-306729045778188742gmail-m_1169979402024537463h5">2018-09-04 18:16 GMT+02:00 J Barchan <span dir="ltr"><<a href="mailto:jnbarchan@gmail.com" target="_blank">jnbarchan@gmail.com</a>></span>:<br></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div class="m_-306729045778188742gmail-m_1169979402024537463h5"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div style="font-family:tahoma,sans-serif">PyQt5.7. I am having trouble `emit()`ing a signal and receiving its arguments correctly. I have read<div style="font-family:tahoma,sans-serif;display:inline"></div> <a href="http://pyqt.sourceforge.net/Docs/PyQt5/signals_slots.html" target="_blank">http://pyqt.sourceforge.net/Do<wbr>cs/PyQt5/signals_slots.html</a> carefully.<br></div><div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif"><b>Declaration</b>:</div><br><div style="font-family:tahoma,sans-serif"><span style="font-family:monospace,monospace"> # class variable for "notifyProgress" signal, for displaying a progressbar<br> notifyProgress = QtCore.pyqtSignal(int, str)</span><br></div><div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif"><b>Initialisation</b>:</div><div style="font-family:tahoma,sans-serif"><br></div><div><span style="font-family:monospace,monospace"> self.notifyProgress.connect(se<wbr>lf.updateProgress)</span></div><div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif"><b>Slot</b>:</div><div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif"><span style="font-family:monospace,monospace"> def updateProgress(self, val: int, text: str):<br> # slot for self.notifyProgress<br> # eprpayrequestfunctions.runEpr(<wbr>) calls this to indicate progress<br> # if it passes an integer it's the desired value for the progressbar<br> # if it passes a string it's the desired value for the label<br> if val is not None:<br> self.progressBar.pb.setValue(v<wbr>al)<br> if text is not None:<br> self.progressBar.label.setText<wbr>(text)</span><br><br></div><div style="font-family:tahoma,sans-serif"><b>Signals</b>:</div><div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif"><span style="font-family:monospace,monospace">1. notifyProgress.emit(None, "Some text")<br><br>2. notifyProgress.emit(i / 2, None)</span></div><br><div style="font-family:tahoma,sans-serif"><b>Behaviour in slot</b>:<br></div><div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif">The problem is the passing of <span style="font-family:monospace,monospace">None</span> from <span style="font-family:monospace,monospace">emit()</span>:</div><div style="font-family:tahoma,sans-serif"><br></div>1. <span style="font-family:monospace,monospace">val</span> arrives in slot as <span style="font-family:monospace,monospace">1261196128</span>.<div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif"><div>2. <span style="font-family:monospace,monospace">text</span> arrives in slot as <span style="font-family:monospace,monospace">''</span> (empty string).</div><div><br></div><div></div><b>Questions</b>:</div><div style="font-family:tahoma,sans-serif"><ul><li>Where is this behaviour for <span style="font-family:monospace,monospace">None</span> as an <span style="font-family:monospace,monospace">emit()</span> parameter documented?</li><li>What is the correct/best way for handling this correctly/easily?<span class="m_-306729045778188742gmail-m_1169979402024537463m_-2805916086382413410HOEnZb"><font color="#888888"><br></font></span></li></ul></div></div><span class="m_-306729045778188742gmail-m_1169979402024537463m_-2805916086382413410HOEnZb"><font color="#888888"><div dir="ltr"><br>-- <br><div class="m_-306729045778188742gmail-m_1169979402024537463m_-2805916086382413410m_6813107041399019673gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><span style="font-family:tahoma,sans-serif">Kindest,</span></div><div><span style="font-family:tahoma,sans-serif">Jonathan</span></div></div></div></div></div>
</div></font></span></div></div></div></div></div></div>
<br></div></div>______________________________<wbr>_________________<br>
PyQt mailing list <a href="mailto:PyQt@riverbankcomputing.com" target="_blank">PyQt@riverbankcomputing.com</a><br>
<a href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt" rel="noreferrer" target="_blank">https://www.riverbankcomputing<wbr>.com/mailman/listinfo/pyqt</a><span class="m_-306729045778188742gmail-m_1169979402024537463HOEnZb"><font color="#888888"><br></font></span></blockquote></div><span class="m_-306729045778188742gmail-m_1169979402024537463HOEnZb"><font color="#888888"><br><br clear="all"><div><br></div>-- <br><div class="m_-306729045778188742gmail-m_1169979402024537463m_-2805916086382413410gmail_signature">È difficile avere una convinzione precisa quando si parla delle ragioni del cuore. - "Sostiene Pereira", Antonio Tabucchi<br><a href="http://www.jidesk.net" target="_blank">http://www.jidesk.net</a></div>
</font></span></div>
</blockquote></div><br></div></div><div style="font-family:tahoma,sans-serif">I'm OK with the <span style="font-family:monospace,monospace;font-size:12.8px"><div style="font-family:tahoma,sans-serif;display:inline"></div>val: int=-1</span>, but not with the <span style="font-family:monospace,monospace;font-size:12.8px"><div style="font-family:tahoma,sans-serif;display:inline"></div>text: str=''</span>. An <span style="font-family:monospace,monospace">int</span> of <span style="font-family:monospace,monospace">-1</span> is happily an invalid value for me, butt the trouble is a <span style="font-family:monospace,monospace">str</span> of <span style="font-family:monospace,monospace">''</span> is perfectly legal, and will be used :(</div><span class="m_-306729045778188742gmail-"><div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif"><div style="display:inline"></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">(which Qt doesn't know anything of also, as you didn't use the Slot decorator),<br></blockquote></div><div style="font-family:tahoma,sans-serif"><br></div></span><div style="font-family:tahoma,sans-serif">I don't mind adding a slot decorator if that would help. But presumably it would not here, as you're saying my <span style="font-family:monospace,monospace">None</span> value does not match the type correctly anyway, right?</div><div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif">I did consider making it so there is only one parameter, and the type (<span style="font-family:monospace,monospace">int</span> or <span style="font-family:monospace,monospace">str</span>) indicates which of the two paths to follow in the slot. I followed <div style="display:inline"></div> <a href="http://pyqt.sourceforge.net/Docs/PyQt5/signals_slots.html" target="_blank">http://pyqt.sourceforge.net/Do<wbr>cs/PyQt5/signals_slots.html</a> carefully where it describes"The following code demonstrates the connection of overloaded signals:" but I couldn't get it to work: it <i>always</i> called the first/default overload, regardless of the parameter type ... :(</div><div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif">I'm now thinking: could I cut my losses and pass (and receive?) a single parameter as an explicit <span style="font-family:monospace,monospace">QVariant</span> (which we don't usually use from PyQt), so that a single receiver slot can correctly see the original type --- would that work/be advisable?<span class="m_-306729045778188742gmail-HOEnZb"><font color="#888888"><br></font></span></div><span class="m_-306729045778188742gmail-HOEnZb"><font color="#888888"><br clear="all"><br>-- <br><div class="m_-306729045778188742gmail-m_1169979402024537463gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><span style="font-family:tahoma,sans-serif">Kindest,</span></div><div><span style="font-family:tahoma,sans-serif">Jonathan</span></div></div></div></div></div>
</font></span></div></div>
</blockquote></div><br></div></div><div style="font-family:tahoma,sans-serif" class="gmail_default">OK, I gave <span style="font-family:monospace,monospace">QVariant</span> (single parameter) a try. <i>All</i> I had to was:</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">1. Declare signal as:</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default"> <span style="font-family:monospace,monospace">QtCore.pyqtSignal(QVariant)</span></div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">2. Write slot like:</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div class="gmail_default"><span style="font-family:monospace,monospace">def updateProgress(self, val):<br> if type(val) is int or type(val) is float:<br> self.progressBar.pb.setValue(<wbr>val)<br> elif type(val) is str:<br> self.progressBar.label.<wbr>setText(val)</span></div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">3. And then it works correctly with both emitter types (without caller bothering with <span style="font-family:monospace,monospace">QVariant</span>), like:</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div class="gmail_default"><span style="font-family:monospace,monospace">notifyProgress.emit(50)<br>notifyProgress.emit("<wbr>Finishing...")</span></div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">Is there any reason I should <i>not</i> be doing this, because it seems perfect to me?<span class="HOEnZb"><font color="#888888"><br></font></span></div><span class="HOEnZb"><font color="#888888"><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><br clear="all"><br>-- <br><div class="m_-306729045778188742gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><span style="font-family:tahoma,sans-serif">Kindest,</span></div><div><span style="font-family:tahoma,sans-serif">Jonathan</span></div></div></div></div></div>
</font></span></div></div></div></div></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">È difficile avere una convinzione precisa quando si parla delle ragioni del cuore. - "Sostiene Pereira", Antonio Tabucchi<br><a href="http://www.jidesk.net" target="_blank">http://www.jidesk.net</a></div>
</div>