<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p><br>
    </p>
    <div class="moz-cite-prefix">On 9/6/21 9:20 PM, Maurizio Berti
      wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAPn+-XSXoGwEqFxcOFQJTT5sSGpEGJVdE-PAnt1oktpb=zVDKw@mail.gmail.com">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <div dir="ltr">
        <div dir="ltr">
          <div dir="ltr">
            <div dir="ltr">
              <div dir="ltr"><br>
                <div class="gmail_quote">
                  <div><br>
                    In this case, as Florian pointed out, the reason is
                    that the setupUi function expects a QWidget based
                    instance (specifically, a QMainWindow in this
                    example), while the passed object (self.Strip_Table)
                    is a QObject, for which the purpose is not really
                    clear.</div>
                  <div><br>
                  </div>
                  <font face="monospace">          self.Strip_Table =
                    QObject(self.Strip_tab)<br>
                              # ...<br>
                              Ui_MainWindow.setupUi(self,
                    self.Strip_Table, anno, month, cursor)</font><br>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </blockquote>
    <font face="monospace">One of the first attempts I made was just to
      insert QMainWindow but since there is already a QMainWindow (the
      general of the project) every time I selected a month to view the
      script it triggered a script that had nothing to do with what was
      requested. QtObject is just the last of my attempts and it stayed
      there when I wrote the email.</font><br>
    <blockquote type="cite"
cite="mid:CAPn+-XSXoGwEqFxcOFQJTT5sSGpEGJVdE-PAnt1oktpb=zVDKw@mail.gmail.com">
      <div dir="ltr">
        <div dir="ltr">
          <div dir="ltr">
            <div dir="ltr">
              <div dir="ltr">
                <div class="gmail_quote">
                  <div>If you need to customize an existing ui based on
                    some arguments, then do not implement them in the
                    setupUi, but create a basic UI on which elements
                    will be eventually added, and write a subclass that
                    *also* inherits from the form class, that is
                    imported from the pyuic generated file (exactly as
                    it was when it was created).</div>
                  <div><br>
                  </div>
                  <div><font face="monospace">from PyQt5.QtWidgets
                      import *</font></div>
                  <div><font face="monospace">from ui_mainWindow import
                      Ui_MainWindow</font></div>
                  <div><font face="monospace"><br>
                    </font></div>
                  <div><font face="monospace">class
                      SomeWindow(QMainWindow, Ui_MainWindow):</font></div>
                  <div><font face="monospace">    def __init__(self,
                      anno, month, cursor):</font></div>
                  <div><font face="monospace">        super().__init__()</font></div>
                  <div><font face="monospace">        self.setupUi(self)</font></div>
                  <div><font face="monospace">        self.tableAnno =
                      QTableWidget()</font></div>
                  <div><font face="monospace">       
                      self.layout.addWidget(self.tableAnno)</font></div>
                  <div><font face="monospace">        # ...</font></div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </blockquote>
    <p><font face="monospace">This sounds very interesting to me. I will
        study how to apply it to my needs and in case will try toprovide
        a minimal reproducible code :(</font></p>
    <p><font face="monospace"><br>
      </font></p>
    <blockquote type="cite"
cite="mid:CAPn+-XSXoGwEqFxcOFQJTT5sSGpEGJVdE-PAnt1oktpb=zVDKw@mail.gmail.com">
      <div dir="ltr">
        <div dir="ltr">
          <div dir="ltr">
            <div dir="ltr">
              <div dir="ltr">
                <div class="gmail_quote">
                  <div>An alternative approach is to use the loadUi
                    function of the uic module, so that you don't need
                    to generate the ui every time. Assuming that the ui
                    file is called 'somewindow.ui':</div>
                  <div><br>
                  </div>
                  <div>
                    <div><font face="monospace">from PyQt5.QtWidgets
                        import *</font></div>
                    <div><font face="monospace">from PyQt5.uic import
                        loadUi</font></div>
                    <div><font face="monospace"><br>
                      </font></div>
                    <div><font face="monospace">class
                        SomeWindow(QMainWindow):</font></div>
                    <div><font face="monospace">    def __init__(self,
                        anno, month, cursor):</font></div>
                    <div><font face="monospace">       
                        super().__init__()</font></div>
                    <div><font face="monospace">       
                        loadUi('somewindow.ui' self)</font></div>
                    <div><font face="monospace">        self.tableAnno =
                        QTableWidget()</font></div>
                    <div><font face="monospace">       
                        self.layout.addWidget(self.tableAnno)</font></div>
                    <div><font face="monospace">        # ...</font></div>
                  </div>
                  <div><br>
                  </div>
                  <div>This will give you exactly the same result.</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </blockquote>
    <p>Why should I use the ui file?</p>
    <p>You wrote "That's one of the many reasons for which files
      generated by pyuic should ** NEVER ** be modified, unless you *
      really * know what you're doing, which assumes you * do * know
      what their classes are and how they work. " Let's start with the
      fact that I struggle to work with the classes and therefore your
      reasoning is impeccable, but to verify if what I did with the
      designer was what I wanted I had to modify the original.
      Everything works as I wish but the problem remains of linking it
      to the rest of the project, just as I wrote above.<br>
    </p>
    <p><br>
    </p>
    <blockquote type="cite"
cite="mid:CAPn+-XSXoGwEqFxcOFQJTT5sSGpEGJVdE-PAnt1oktpb=zVDKw@mail.gmail.com">
      <div dir="ltr">
        <div dir="ltr">
          <div dir="ltr">
            <div dir="ltr">
              <div dir="ltr">
                <div class="gmail_quote">
                  <div>Also note that you should not call setupUi
                    arbitrarily like a class method using "self" for the
                    first argument. Not only it doesn't make a lot of
                    sense to create a class (that inherits from
                    QMainWindow) for which you're not actually creating
                    instances, but this is also wrong for two important
                    reasons:</div>
                  <div>- the "self" is assumed to be the ui class (the
                    "form class") *or* the widget used with multiple
                    inheritance (see the first example above), not
                    *another* instance (it's an instance method);</div>
                  <div>- setupUi  is intended to be called only once on
                    a "clean" widget instance, calling it on an widget
                    that has been already set up could potentially
                    overwrite some existing attributes and potentially
                    make the whole UI completely unusable, both for the
                    user and programmatically;</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </blockquote>
    Understood<br>
    <blockquote type="cite"
cite="mid:CAPn+-XSXoGwEqFxcOFQJTT5sSGpEGJVdE-PAnt1oktpb=zVDKw@mail.gmail.com">
      <div dir="ltr">
        <div dir="ltr">
          <div dir="ltr">
            <div dir="ltr">
              <div dir="ltr">
                <div class="gmail_quote">
                  <div>Some other unrelated considerations:</div>
                  <div>- changing the option text and using the delegate
                    to store data is usually not a good idea; if you
                    want to alter the way the data is displayed, then
                    you should properly set data using custom roles in
                    the model, which can be achieved by using
                    item.setData(customRole, value) for a table widget
                    item.</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </blockquote>
    A stackoverflow user advised me to use this system because I had
    problems in the first two rows of the tables that would then end up
    in the infamous scrollArea (see:
<a class="moz-txt-link-freetext" href="https://stackoverflow.com/questions/65493443/qtablewidget-column-span-doesnt-resize-correctly">https://stackoverflow.com/questions/65493443/qtablewidget-column-span-doesnt-resize-correctly</a>)<br>
    <p><br>
    </p>
    <p>Regards <br>
    </p>
    <p>paparucino</p>
    <p><br>
    </p>
  </body>
</html>