Porting from PyQt5 to PySide2, take two

Posted on Fri 10 January 2020 in Programming

Of course, porting from PyQt5 to PySide2 any not trivial codebase like Punteggi require some more work than what I needed to port Vtf.
The big problem, since this port was done under Archlinux, was to install PySide2. The package that come with the distro are missing the pyside2-uic and pyside2-rcc tools, which are necessary to build the .ui file for the gui and the resorces (icons, and so on). After digging some more, I just installed it using pip3 install pyside2 which, while not installing from a repository package, do the work well.

The next problem was that, apparently, pyside2-uic can generate .py files with contains some syntax error, this render the usage of it useless, at least in this case.
The error I got was very trivial:

File "MainWin.py", line 184
   if (self.TW_Results.columnCount() < 1)
                                          ^
SyntaxError: invalid syntax

derivating from this fragment of code:

if (self.TW_Results.columnCount() < 1)
    self.TW_Results.setColumnCount(1)

TW_Results is a QTableWidget which is just created empty. While this happen in a couple of places where there is a QTableWidget, I still don't understand the reason.

So the next step was to use directly the .ui file using the QUiLoader which work reasonably well, but it implies some more changes to the code, since this way you have not access to the Gui element directly.
So when you need to access the gui objects, like a QPushButton to connect a slot to the clicked() signal, you need to go from this code:

self.connect(self.B_ImpostaDati,  SIGNAL("clicked()"),  self.ImpostaDati)

to this code:

self.B_ImpostaDati = self.window.findChild(QPushButton, 'B_ImpostaDati')
self.B_ImpostaDati.clicked.connect(self.ImpostaDati)

which is tedious if there are a lot of object you need to interact with.
On the other hand this force you to set explicitly the items you want to use so, for example, if you don't need to use a QLabel you just don't instantiate a class member for it and the label remains somewhat "hidden" to the class code while still visible.

This was a more challenging port than the first one, adding also the fact that the code base was ported from Python2 to Python2.Nevertheless it is done and now it just need to be tested.