<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Il giorno sab 4 set 2021 alle ore 16:46 paparucino <<a href="mailto:paparucino@gmail.com">paparucino@gmail.com</a>> ha scritto:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hello, from ScriptOne I would like to run ScriptTwo so as to get a <br>
scrollArea with all the widgets I need. I tried to read up but without <br>
success.<br>
If I run ScriptTwo as a single script the scrollArea is displayed correctly.<br>
If I call ScriptTwo from One I get<br>
================================<br>
Traceback (most recent call last):<br>
File "/root/PycharmProjects/ForTestOnly/index.py", line 763, in <br>
Data_Strip<br>
Ui_MainWindow.setupUi (self, self.Strip_Table, anno, month, cursor)<br>
File "/root/PycharmProjects/ForTestOnly/strips.py", line 37, in setupUi<br>
MainWindow.resize (900, 650) # 1700<br>
AttributeError: 'QObject' object has no attribute 'resize'<br>
===========================================<br></blockquote><div><br></div><div>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.<br><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><br><div>A QObject is *not* a widget, it is the *base* class of most Qt based objects, it's what QWidget (and therefore all subclasses) inherits from, but it can also be used for other purposes that are not strictly related to the UI, like an item model (not the view) or a network manager.</div><div><br></div><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><br></div><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><br></div><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><br></div><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>- function and attribute names should start with a lowercase letter, as only classes and constants are capitalized; this ensures that you can immediately tell them apart when reading code.</div><div>- always try to provide a minimal reproducible code, without removing parts that might be essential and important, so that we can better understand how your code works.</div><div><br></div><div>Regards,<br>Maurizio</div><div><br></div></div>-- <br><div dir="ltr" class="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></div></div></div></div>