import erron on QtWidgets

Need help, or want to share a macro? Post here!
Jee-Bee
Posts: 2021
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

import erron on QtWidgets

Postby Jee-Bee » Fri Dec 21, 2018 1:59 pm

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
Posts: 9464
Joined: Mon Dec 12, 2011 4:45 pm

Re: import erron on QtWidgets

Postby triplus » Sat Dec 22, 2018 5:09 pm

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
Posts: 2021
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

Re: import erron on QtWidgets

Postby Jee-Bee » Mon Jan 14, 2019 3:46 pm

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
Posts: 9464
Joined: Mon Dec 12, 2011 4:45 pm

Re: import erron on QtWidgets

Postby triplus » Thu Jan 24, 2019 7:24 pm

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
Posts: 2021
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

Re: import erron on QtWidgets

Postby Jee-Bee » Thu Jan 24, 2019 7:54 pm

Thanks i check it out