question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Infinite loop on uic.loadUi() with PySide2

See original GitHub issue

I’m trying to use qtpy so I can support both PyQt5 and PySide2. I have a part in my program when I load a different INI file that has a different ui file that updates my GUI. This works fine with PyQt5, but I get an access violation and the program dies when I try the same thing with PySide2.

from qtpy import uic, QtNetwork
from qtpy.QtCore import Slot, QTimer
import pyqtgraph as pg

The offending line is self.FileNamingWidget = uic.loadUi(uifilename)

I have two different conda environments, one with PyQt5, and the other with PySide2. In the PyQt5 environment, everything works as expected. In the PySide2 environment, I get

Windows fatal exception: access violation

Current thread 0x00003428 (most recent call first):
  File "C:\Software\envs\fieldcapenv_pyside\lib\site-packages\qtpy\uic.py", line 146 in createWidget
  File "C:\Software\envs\fieldcapenv_pyside\lib\site-packages\qtpy\uic.py", line 232 in loadUi
  File "C:\Users\timothy.j.williams1\PycharmProjects\NVFieldCap\nvfieldcap\application.py", line 1089 in Load_Profile
  File "C:\Users\timothy.j.williams1\PycharmProjects\NVFieldCap\nvfieldcap\application.py", line 2100 in run
  File "C:/Users/timothy.j.williams1/PycharmProjects/NVFieldCap/bin/nvfieldcap.py", line 102 in _run_nvfieldcap
  File "C:/Users/timothy.j.williams1/PycharmProjects/NVFieldCap/bin/nvfieldcap.py", line 130 in <module>

Process finished with exit code -1073741819 (0xC0000005)

If I step through the debugger, I can see that a widget gets created in uic.UiLoader.createWidget, but when it returns the widget back to uic.loadUi, widget = loader.load(uifile) loader.load is just called again. I’m in an infinite loop until I disable my breakpoint.

I should note that when I to step into uic.loadUi in my PyQt5 environment, I’m not taken to pyqt.uic.loadUi, but PyQt5\uic\__init__.py .

    from .Loader.loader import DynamicUILoader

While in the debugger console, I can do

uic.loadUi(uifilename)
PyDev console: starting.
<PyQt5.QtWidgets.QTabWidget object at 0x0000020F94B68318>

If I try that on the PySide2 environment, I get the access violation.

Thanks for any help.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tjwilli58commented, May 27, 2022

Went to revisit this again. I’m not having the same problem. With PySide2 I’m getting this

Traceback (most recent call last):
  File "C:\Software\envs\fieldcapenv_pyside\lib\site-packages\pyqtgraph\parametertree\ParameterTree.py", line 181, in sizeHint
    for x in self.listAllItems():
  File "C:\Software\envs\fieldcapenv_pyside\lib\site-packages\pyqtgraph\widgets\TreeWidget.py", line 138, in listAllItems
    for cindex in range(item.childCount()):
RuntimeError: Internal C++ object (PySide2.QtWidgets.QTreeWidgetItem) already deleted.

I’m not getting this error with PyQt5, but my GUI is also not updating as it should. I’m convinced now that part of this is due to my incorrect mixing of qtpy and pyqtgraph widgets.

If I take out this code completely

        uiFileName = self.get_uiFileName(config)
        # self.FileNamingWidget.close()
        with importlib.resources.path('nvfieldcap.resources.ui', uiFileName) as _uiFileName:
            self.FileNamingWidget = uic.loadUi(_uiFileName.as_posix())
        self.ui.PluginLayout.addWidget(self.FileNamingWidget)

things work just fine. I’m not doing something right with the LayoutWidget. I really don’t need to be doing this anyway. I’m closing this.

1reaction
tjwilli58commented, May 23, 2022

Hi @dalthviz . It does seem so. I fixed the bug of my own making referenced above. I ended up taking out some code that was loading a UI file basically when I shouldn’t so the problem in effect went away. I had code calling uic.loadUi() somewhere else as part of a dialog, but it was using PyQt5.uic. I changed it to using qtpy.uic and that worked fine.

PyQt5 code:

if 'PyQt5' in sys.modules:
    from PyQt5 import QtNetwork
    from PyQt5 import uic
    from PyQt5.QtCore import pyqtSlot as Slot
    from PyQt5.QtCore import QTimer
...
        self.deleteDialog = QtWidgets.QDialog()
        uic.loadUi(importlib.resources.open_text('nvfieldcap.resources.ui', 'deleteDialog.ui'), self.deleteDialog)

qtpy code:

from qtpy import uic, QtNetwork
from qtpy.QtCore import Slot, QTimer
...
        self.deleteDialog = QtWidgets.QDialog()
        with importlib.resources.path('nvfieldcap.resources.ui', 'deleteDialog.ui') as _ctx:
            _uifilename = _ctx.as_posix()
            uic.loadUi(_uifilename, self.deleteDialog)

Given that this works, and I believe my other bug was the real problem, I’m tempted to close this issue. I’ll try first to see if I can replicate it with a smaller example.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PySide2 UI stops responding when entering a while loop after ...
The problem here is that you have an infinite loop before you start the Qt event loop. if __name__ == "__main__": app ...
Read more >
loadUiType — Qt for Python
This function generates and loads a .ui file at runtime, and it returns a tuple containing the reference to the Python class, and...
Read more >
Use pyside2-uic instead of uic for the loadUiType (I2c801a16)
Use pyside2-uic instead of uic for the loadUiType Since we deploy the pyside2-uic wrapper inside the bin/ directory of virtual environments, that takes...
Read more >
Python and PyQt: Building a GUI Desktop Calculator
An event loop is an infinite loop in which all events from the user, the window system, and any other sources are processed...
Read more >
Is PyQt free for commercial use? - Quora
A possible alternative is to switch to PySide2, also known as Qt for Python. ... like event loop and event driven programming quite...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found