<div dir="ltr"><div dir="ltr"><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><br><div class="gmail_quote"><div class="gmail_attr" dir="ltr">On Thu, 14 Feb 2019 at 17:48, J Barchan <<a href="mailto:jnbarchan@gmail.com">jnbarchan@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-left-style:solid"><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 style="font-family:tahoma,sans-serif"><br></div></div><br><div class="gmail_quote"><div class="gmail_attr" dir="ltr">On Thu, 14 Feb 2019 at 14:56, Vincent Vande Vyvre <<a href="mailto:vincent.vande.vyvre@telenet.be" target="_blank">vincent.vande.vyvre@telenet.be</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-left-style:solid">
  
    
  
  <div bgcolor="#FFFFFF">
    <div class="gmail-m_6276353647487498744gmail-m_7882909993121235286moz-cite-prefix">Le 14/02/19 à 14:56, J Barchan a
      écrit :<br>
    </div>
    <blockquote type="cite">
      
      <div dir="ltr">
        <div dir="ltr">
          <div style="font-family:tahoma,sans-serif"><br>
          </div>
        </div>
        <br>
        <div class="gmail_quote">
          <div class="gmail_attr" dir="ltr">On Thu, 14 Feb 2019 at
            13:34, J Barchan <<a href="mailto:jnbarchan@gmail.com" target="_blank">jnbarchan@gmail.com</a>> wrote:<br>
          </div>
          <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-left-style:solid">
            <div dir="ltr">
              <div dir="ltr">
                <div dir="ltr">
                  <div dir="ltr">
                    <div style="font-family:tahoma,sans-serif">This may
                      be as much a Python question as a PyQt one.  I
                      come from a C++ background.  I do not understand
                      the syntax/code I need in a class I am deriving
                      from a PyQt class to allow a new parameter to be
                      passed to the constructor.</div>
                    <div style="font-family:tahoma,sans-serif"><br>
                    </div>
                    <div style="font-family:tahoma,sans-serif">I see
                      that I asked this question a long time ago at <a href="https://stackoverflow.com/questions/45999732/python3-typing-overload-and-parameters" target="_blank">https://stackoverflow.com/questions/45999732/python3-typing-overload-and-parameters</a>
                      but never got an answer.</div>
                    <div style="font-family:tahoma,sans-serif"><br>
                    </div>
                    <div style="font-family:tahoma,sans-serif">I now
                      want to sub-class from <span style="font-family:monospace,monospace">QListWidgetItem</span>. 
                      That starts with these constructors:</div>
                    <div style="font-family:tahoma,sans-serif"><br>
                    </div>
                    <div><span style="font-family:monospace,monospace">QListWidgetItem(QListWidget
                        *parent = nullptr, int type = Type)<br>
                        QListWidgetItem(const QString &text,
                        QListWidget *parent = nullptr, int type = Type)<br>
                        QListWidgetItem(const QIcon &icon, const
                        QString &text, QListWidget *parent =
                        nullptr, int type = Type)<br>
                        QListWidgetItem(const QListWidgetItem
                        &other)</span></div>
                    <div style="font-family:tahoma,sans-serif"><br>
                    </div>
                    <div style="font-family:tahoma,sans-serif">My
                      sub-class should still support these
                      constructors.  In addition to the existing <span style="font-family:monospace,monospace">text</span>,
                      I want my sub-class to be able to store a new
                      optional <span style="font-family:monospace,monospace">value</span>.
                      At minimum/sufficient I want a new possible
                      constructor like one of the following:</div>
                    <div style="font-family:tahoma,sans-serif"><br>
                    </div>
                    <div style="font-family:tahoma,sans-serif"><span style="font-family:monospace,monospace">MyListWidgetItem(<span style="font-family:monospace,monospace">const
                          QString &text, <span style="font-family:monospace,monospace"><span style="color:rgb(255,0,255)">const
                              QVariant &value,</span> </span></span>QListWidget
                        *parent = nullptr, int type = Type)</span></div>
                    <div style="font-family:tahoma,sans-serif"><span style="font-family:monospace,monospace"># or<br>
                      </span></div>
                    <div style="font-family:tahoma,sans-serif">
                      <div style="font-family:tahoma,sans-serif"><span style="font-family:monospace,monospace">MyListWidgetItem(<span style="font-family:monospace,monospace">const
                            QString &text, <span style="font-family:monospace,monospace"><span style="color:rgb(255,0,255)">QVariant
                                value = QVariant(),</span> </span></span>QListWidget
                          *parent = nullptr, int type = Type)</span></div>
                      <div style="font-family:tahoma,sans-serif"><br>
                      </div>
                    </div>
                    <div style="font-family:tahoma,sans-serif">So for
                      Python I know I start with a <i>typing overload</i>
                      definition (for my editor) like <br>
                    </div>
                    <div style="font-family:tahoma,sans-serif"><br>
                    </div>
                    <div><span style="font-family:monospace,monospace">@typing.overload</span></div>
                    <div><span style="font-family:monospace,monospace">def
                        MyListWidgetItem(self, text: str, value:
                        typing.Any, parent: QListWidget=None, type:
                        int=Type)</span></div>
                    <div><span style="font-family:monospace,monospace">   
                        pass<br>
                      </span></div>
                    <div style="font-family:tahoma,sans-serif"><br>
                    </div>
                    <div style="font-family:tahoma,sans-serif">Then I
                      get to the <i>definition</i> bit.  To cater for
                      everything am I supposed to do:</div>
                    <div style="font-family:tahoma,sans-serif"><br>
                    </div>
                    <div style="font-family:tahoma,sans-serif">
                      <div><span style="font-family:monospace,monospace"><span class="gmail_default" style="font-family:tahoma,sans-serif"></span>def
                          __init__(self, *__args)</span></div>
                      <div><span style="font-family:monospace,monospace">   
                          # Now what??<br>
                        </span></div>
                      <div><span style="font-family:monospace,monospace">   
                          super().__init__(__args)<br>
                        </span></div>
                    </div>
                    <div style="font-family:tahoma,sans-serif"><br>
                    </div>
                    <div style="font-family:tahoma,sans-serif">Is that
                      how we do it?  Is it then my responsibility to
                      look at <span style="font-family:monospace,monospace">__args[1]</span>
                      to see if it's my <span style="font-family:monospace,monospace">value</span>
                      argument?  And remove it from <span style="font-family:monospace,monospace">__args</span>
                      before passing it onto <span style="font-family:monospace,monospace">super().__init__(__args)</span>?<br>
                    </div>
                    <div style="font-family:tahoma,sans-serif"><br>
                    </div>
                    <div style="font-family:tahoma,sans-serif">Or, am I
                      not supposed to deal with <span style="font-family:monospace,monospace">__args</span>,
                      and instead have some definition with all possible
                      parameters explicitly and deal with them like
                      that?</div>
                    <div style="font-family:tahoma,sans-serif"><br>
                    </div>
                    <div style="font-family:tahoma,sans-serif">Or what? 
                      This is pretty fundamental to sub-classing to add
                      parameters where you don't own the code of what
                      you're deriving from.  It's easy in C-type
                      languages; I'm finding it real to hard to
                      understand what I can/can't/am supposed to do for
                      this, I'd be really gratefully for a couple of
                      lines to show me, please...! :)<br clear="all">
                    </div>
                    <br>
                    -- <br>
                    <div class="gmail-m_6276353647487498744gmail-m_7882909993121235286gmail-m_-6986055192168723530gmail_signature" dir="ltr">
                      <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>
                </div>
              </div>
            </div>
          </blockquote>
        </div>
        <br clear="all">
        <div style="font-family:tahoma,sans-serif">P.S.</div>
        <div style="font-family:tahoma,sans-serif">I
          think I got my overload a bit mixed up.  I meant I (think I)
          will have:</div>
        <div style="font-family:tahoma,sans-serif"><br>
        </div>
        <div style="font-family:tahoma,sans-serif"><span style="font-family:monospace,monospace">class
            MyListWidgetItem(QListWidgetItem)</span><span style="font-family:monospace,monospace"><span class="gmail_default" style="font-family:tahoma,sans-serif"><br>
            </span></span></div>
        <div style="font-family:tahoma,sans-serif"><span style="font-family:monospace,monospace"><span class="gmail_default" style="font-family:tahoma,sans-serif"></span></span>
          <div>
            <div style="font-family:tahoma,sans-serif">
              <div>
                <div><span style="font-family:monospace,monospace">   
                    @typing.overload</span></div>
                <div><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace">    def
                      __init__(<span style="font-family:monospace,monospace">self,
                        text: str, value: typing.Any, parent:
                        QListWidget=None, type: int=Type)</span></span></span>
                  <div><span style="font-family:monospace,monospace">       
                      pass</span></div>
                  <div><span style="font-family:monospace,monospace"><br>
                    </span></div>
                  <span style="font-family:monospace,monospace">    def
                    __init__(self, *__args)</span></div>
              </div>
              <div><span style="font-family:monospace,monospace">       
                  # Now what??<br>
                </span></div>
              <div><span style="font-family:monospace,monospace">       
                  super().__init__(__args)<br>
                </span></div>
            </div>
            <div style="font-family:tahoma,sans-serif"><br>
            </div>
            <span style="font-family:monospace,monospace"></span></div>
        </div>
        <br>
        -- <br>
        <div class="gmail-m_6276353647487498744gmail-m_7882909993121235286gmail_signature" dir="ltr">
          <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>
      <br>
      <fieldset class="gmail-m_6276353647487498744gmail-m_7882909993121235286mimeAttachmentHeader"></fieldset>
      <pre class="gmail-m_6276353647487498744gmail-m_7882909993121235286moz-quote-pre">_______________________________________________
PyQt mailing list    <a class="gmail-m_6276353647487498744gmail-m_7882909993121235286moz-txt-link-abbreviated" href="mailto:PyQt@riverbankcomputing.com" target="_blank">PyQt@riverbankcomputing.com</a>
<a class="gmail-m_6276353647487498744gmail-m_7882909993121235286moz-txt-link-freetext" href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt" target="_blank">https://www.riverbankcomputing.com/mailman/listinfo/pyqt</a>
</pre>
    </blockquote>
    <p>Hi,<br>
      <br>
      I use just that:<br>
      <br>
      class <span class="gmail_default" style="font-family:tahoma,sans-serif"></span>ListItem(<span class="gmail_default" style="font-family:tahoma,sans-serif"></span>QListWidgetItem):<br>
          def __init__(self, img, text, parent=None):<br>
              super().__init__(<span class="gmail_default" style="font-family:tahoma,sans-serif"></span>parent)<br>
              icon = QIcon()<br>
              icon.addPixmap(QPixmap(img), QIcon.Normal, QIcon.Off)<br>
              self.setIcon(icon)<br>
              self.setText(text)<br>
      <br>
      The arguments are examples, not mandatory.<br>
      <br>
      Vincent<br>
    </p>
  </div>

_______________________________________________<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" target="_blank" rel="noreferrer">https://www.riverbankcomputing.com/mailman/listinfo/pyqt</a><br>
</blockquote></div><div><br></div><div><div style="font-family:tahoma,sans-serif">Hello Vincent,</div><div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif">Thank you for replying.  I'm afraid the suggestion you give is so significantly different from what I am asking that I don't see how it addresses it.  I could write a lot about the differences between what you show and what I am asking.  Here are some:</div><ul><li><div style="font-family:tahoma,sans-serif">I don't see that you're adding any argument that <span class="gmail_default" style="font-family:tahoma,sans-serif"></span><font face="monospace,monospace">QListWidgetItem</font> does not already accept??  Please bear in mind there is already <font face="Courier New">QListWidgetItem(const QIcon &icon, const QString &text, QListWidget *parent = nullptr, int type = Type)</font>.  I want to add a new argument.</div></li><li><div style="font-family:tahoma,sans-serif">Your <font face="monospace,monospace">super().__init__()</font> is not passing anything other than <font face="monospace,monospace">parent</font> to the base constructor, yet the base constructor accepts more arguments than that, just like your derived class does.</div></li><li><div style="font-family:tahoma,sans-serif">(untested) it seems to me that your code will break the existing <font face="monospace,monospace">QListWidgetItem(const QIcon &icon, const QString &text, QListWidget *parent = nullptr, int type = Type)</font> constructor overload (i.e. what happens when I pass a <font face="monospace,monospace">QIcon() </font>as the <font face="monospace,monospace">img </font>parameter to your <font face="monospace,monospace">ListItem</font> constructor?)</div></li><li><div style="font-family:tahoma,sans-serif">how does your code allow for the existing <font face="monospace,monospace">QListWidgetItem(const QString &text, QListWidget *parent = nullptr, int type = Type)</font> overload</div></li><li><div style="font-family:tahoma,sans-serif">you are not using any <font face="monospace,monospace">typing</font> hints so this will lose my editor's context completion</div></li></ul><span class="gmail_default" style="font-family:tahoma,sans-serif">I don't know whether one of us is misunderstanding the other, or we're on different planes? :)</span><br clear="all"><br>-- <br></div><div class="gmail-m_6276353647487498744gmail_signature" dir="ltr"><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></div></div></div></div></div></div>
</blockquote></div><div><br></div><div><div class="gmail_default" style="font-family:tahoma,sans-serif">May I politely try bumping this question?  I have had one answer, which as far as I understand does not work.</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">How <em>exactly</em> could/would you derive from <font face="monospace,monospace">QListWidgetItem</font> from Python to add a "value" parameter in a constructor?   I could do it easily from C++.  It does seem to me this is at least partly a PyQt question, I've tried asking it at <a href="https://stackoverflow.com/questions/54746309/python-3-add-argument-when-subclassing-from-complex-arguments">https://stackoverflow.com/questions/54746309/python-3-add-argument-when-subclassing-from-complex-arguments</a> , I'm getting comments like "If PyQT makes it impossible to use common Python idioms then I can't do much about it actually <g>" and "The OP needs to have a re-think and accept that some compromises are inevitable when trying to fake c++ idioms in pure python."</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">This is a bit above my head.  I'm either getting a generic answer which is inadequate or no answer or told it may not be doable.  I don't understand this.  Can someone tell me how to add the argument I have in mind, or something similar (you do need to read through the precise example I am asking about), or explain why it can't be done, or something?  I should be so obliged!</div><br clear="all"><br>-- <br></div><div class="gmail_signature" dir="ltr"><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></div></div></div></div>