import erron on QtWidgets

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Jee-Bee
Veteran
Posts: 2566
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

import erron on QtWidgets

Post by Jee-Bee »

I'm trying to build a mini wb according to: https://freecadweb.org/wiki/Workbench_creation
but i'm stuck with importing QtWidgets. I had found a nice method somewhere but can't find it back (below my result).

Code: Select all

def Activated(self):
    #"Do something here"
    #"for examples checkmanipulator wb"
    from PySide2 import QtUiTools
    from PySide2 import QtGui
    from PySide2 import QtCore
    from PySide2 import QtWidgets
    # from PySide import QtWidgets
    w = QtWidgets.QWidget()
    ui = QtUiTools.QUiLoader()
    # ui = FreeCADGui.UiLoader()  # change later on. First swap spinbox to quantity spinbox
    ui.load(os.path.join(ui_path, 'ui_widget.ui'), w)
    return
The problem i encounter now is that QtWidgets gives an error by importing it.
I got this error message.

Code: Select all

Running the Python command 'command_1' failed:
Traceback (most recent call last):
  File "C:\Users\USER\AppData\Roaming\FreeCAD\Mod\custom_workbench\command_1.py", line 31, in Activated
    from PySide2 import QtWidgets

cannot import name 'QtWidgets'
When i type from PySide2 import QtWidgets directly in the python console in FC i don't get an error message.
Can anybody explain what happens?
triplus
Veteran
Posts: 9471
Joined: Mon Dec 12, 2011 4:45 pm

Re: import erron on QtWidgets

Post by triplus »

We use a shim in FreeCAD to mitigate different imports between PySide/PySide2 (Qt4/Qt5). Therefore you can use:

Code: Select all

from PySide import QtGui
w = QtGui.QWidget()
And that should work in both Qt4/Qt5 builds.
Jee-Bee
Veteran
Posts: 2566
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

Re: import erron on QtWidgets

Post by Jee-Bee »

No that doesn't work out. I have changed teh code to what you said. but that result in anouther error...

Code: Select all

def Activated(self):
    #"Do something here"
    #"for examples checkmanipulator wb"
    from PySide import QtUiTools
    from PySide import QtGui
    from PySide import QtCore
    # from PySide import QtWidgets
    w = QtGui.QWidget()
    ui = QtUiTools.QUiLoader()
    # ui = FreeCADGui.UiLoader()  # change later on. First swap spinbox to quantity spinbox
    ui.load(os.path.join(ui_path, 'ui_widget.ui'), w)
    return

Code: Select all

Running the Python command 'command_1' failed:
Traceback (most recent call last):
  File "C:\Users\USER\AppData\Roaming\FreeCAD\Mod\custom_workbench\command_1.py", line 33, in Activated
    w = QtGui.QWidget()

module 'PySide2.QtGui' has no attribute 'QWidget'
triplus
Veteran
Posts: 9471
Joined: Mon Dec 12, 2011 4:45 pm

Re: import erron on QtWidgets

Post by triplus »

I can confirm this minimal example doesn't work on PySide2/Qt5 builds:

Code: Select all

def Activated():
    from PySide import QtUiTools
    from PySide import QtGui
    from PySide import QtCore
    w = QtGui.QWidget()

Activated()
Hopefully the following PR provides a fix:

https://github.com/FreeCAD/FreeCAD/pull/1929
Jee-Bee
Veteran
Posts: 2566
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

Re: import erron on QtWidgets

Post by Jee-Bee »

Thanks i check it out
Post Reply