Can't call another class

paparucino paparucino at gmail.com
Tue Sep 7 12:52:57 BST 2021


On 9/6/21 9:20 PM, Maurizio Berti wrote:
>
>
> 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.
>
>           self.Strip_Table = QObject(self.Strip_tab)
>           # ...
>           Ui_MainWindow.setupUi(self, self.Strip_Table, anno, month, 
> cursor)
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.
> 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).
>
> from PyQt5.QtWidgets import *
> from ui_mainWindow import Ui_MainWindow
>
> class SomeWindow(QMainWindow, Ui_MainWindow):
>     def __init__(self, anno, month, cursor):
>         super().__init__()
>         self.setupUi(self)
>         self.tableAnno = QTableWidget()
> self.layout.addWidget(self.tableAnno)
>         # ...

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 :(


> 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':
>
> from PyQt5.QtWidgets import *
> from PyQt5.uic import loadUi
>
> class SomeWindow(QMainWindow):
>     def __init__(self, anno, month, cursor):
> super().__init__()
> loadUi('somewindow.ui' self)
>         self.tableAnno = QTableWidget()
> self.layout.addWidget(self.tableAnno)
>         # ...
>
> This will give you exactly the same result.

Why should I use the ui file?

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.


> 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:
> - 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);
> - 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;
Understood
> Some other unrelated considerations:
> - 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.
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: 
https://stackoverflow.com/questions/65493443/qtablewidget-column-span-doesnt-resize-correctly)


Regards

paparucino


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210907/f9d1462f/attachment.htm>


More information about the PyQt mailing list