cannot import name 'Qt' from 'PySide.QtGui'

Having trouble installing or compiling FreeCAD? Get help here.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
MrGadget49
Posts: 27
Joined: Mon Oct 03, 2022 6:31 am

cannot import name 'Qt' from 'PySide.QtGui'

Post by MrGadget49 »

Hello FreeCAD builders,

I'm not sure if this is the right forum, for this question, but I'm posting it here because I believe it is related to my build of FreeCad. I do not experience this problem when I run a recent appImg.

The problem is, I get the following error when I run a macro (widgetExample)

Code: Select all

02:09:43 Traceback (most recent call last): 
File "/home/jody/.local/share/FreeCAD/Macro/widgetExample.FCMacro", line 1, in <module> 
from PySide.QtGui import QWidget, QLabel, Qt, QVBoxLayout, QHBoxLayout, QPushButton, QListWidget, QListWidgetItem 
<class 'ImportError'>: cannot import name 'Qt' from 'PySide.QtGui' (/home/jody/freecad/freecad-build/Ext/PySide/QtGui.py) 
I thought perhaps I was missing a library or something, but I compared my list (show below) with the one from the appImage that works and everything related to PySide and QT seems to be there.

Code: Select all

This software uses open source components whose copyright and other proprietary rights belong to their respective owners: 
 
    Boost 1_67 
    https://www.boost.org 
 
    Coin3D 4.0.0a 
    https://coin3d.github.io 
 
    Eigen 3.3.7 
    https://eigen.tuxfamily.org 
 
    FreeType 2.9.1 
    https://freetype.org 
 
    KDL  
    https://www.orocos.org/kdl 
 
    libarea  
    https://github.com/danielfalck/libarea 
 
    Open CASCADE Technology 7.3.0 
    https://www.opencascade.com/open-cascade-technology/ 
 
    Point Cloud Library  
    https://www.pointclouds.org 
 
    PyCXX 7.1.7 
    http://cxx.sourceforge.net 
 
     Python 3.7.3 
     https://www.python.org 
 
    Qt for Python (PySide) 5.11.2 
    https://wiki.qt.io/Qt_for_Python 
 
    Qt 5.11.3 
    https://www.qt.io 
 
    Salome SMESH  
    https://salome-platform.org 
 
    Qt for Python (Shiboken) 5.11.2 
    https://wiki.qt.io/Qt_for_Python 
 
    vtk 6.3.0 
    https://www.vtk.org 
 
    Xerces-C 3.2.2 
    https://xerces.apache.org/xerces-c 
 
    Zipios++  
    http://zipios.sourceforge.net 
 
    zlib 1.2.11 
    https://zlib.net 
 
The script is presented below but like I said, it works on one of the recent app images that I was using before I made my build. It's failing on line one.

Code: Select all

from PySide.QtGui import QWidget, QLabel, Qt, QVBoxLayout, QHBoxLayout, QPushButton, QListWidget, QListWidgetItem

class MyWidget(QWidget):
	def __init__(self,text):
		super(MyWidget,self).__init__()
		label = QLabel(text)
		label.setAlignment(Qt.AlignCenter | Qt.AlignRight)
		button = QPushButton("push me!")
		layout = QHBoxLayout()
		layout.addWidget(label)
		layout.addWidget(button)
		self.setLayout(layout)

class MyWindow(QWidget):
	def __init__(self):
		super(MyWindow,self).__init__()
		list = QListWidget()
		listItem = QListWidgetItem(list)
		listItemWidget = MyWidget("listItem")
		listItem.setSizeHint(listItemWidget.sizeHint())

		list.addItem("Item one")
		list.addItem("Item two")
		list.addItem(listItem)
		list.setItemWidget(listItem,listItemWidget)

		myWidget1 = MyWidget("test1")
		layout = QVBoxLayout()
		layout.addWidget(myWidget1)
		layout.addWidget(list)
		self.setLayout(layout)
		mySize = QtCore.QSize(1000, 200)
		self.setFixedSize(mySize)
		self.show()

myWin=MyWindow()

Code: Select all

OS: Debian GNU/Linux 10 (buster) (MATE/mate)
Word size of FreeCAD: 64-bit
Version: 0.21.30623 (Git)
Build type: Unknown
Branch: myWorkBranch
Hash: 27f08f38b1658e419e20a153e13236eff1293b77
Python 3.7.3, Qt 5.11.3, Coin 4.0.0a, Vtk 6.3.0, OCC 7.3.0
Locale: English/UnitedStates (en_US)
Installed mods: 
  * lattice2 1.0.0
  * Assembly4 0.12.4
  * Curves 0.5.8
  * fasteners 0.4.13
  * sheetmetal 0.2.57

Any ideas or suggestions would be greatly appreciated! Also, if there isany other information I can post that would help, please let me know.

Jody
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: cannot import name 'Qt' from 'PySide.QtGui'

Post by onekk »

See maybe:

https://wiki.freecadweb.org/PySide

Probably Qt is not a Child of PySide.QtGui.

Why see linked text.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
chennes
Veteran
Posts: 3879
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: cannot import name 'Qt' from 'PySide.QtGui'

Post by chennes »

I am not at a computer to double check, but I think Qt is in QtCore.
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: cannot import name 'Qt' from 'PySide.QtGui'

Post by heda »

from appimage...

Code: Select all

>>> from PySide import QtCore, QtGui
>>> 'Qt' in dir(QtGui)
True
>>> 'Qt' in dir(QtCore)
True
>>> from PySide.QtCore import Qt
>>> from PySide.QtGui import Qt
>>> 
so you are probably right in that it has to do with your build,
what knob to turn is way out of my realm - you need some compilation flag whiz to get help for that...
MrGadget49
Posts: 27
Joined: Mon Oct 03, 2022 6:31 am

Re: cannot import name 'Qt' from 'PySide.QtGui'

Post by MrGadget49 »

onekk wrote: Mon Nov 28, 2022 9:43 am See maybe:

https://wiki.freecadweb.org/PySide

Probably Qt is not a Child of PySide.QtGui.

Why see linked text.

Regards

Carlo D.
Thanks, Carlo. I learned a lot from your link. It definitely helped!

I also learned that you are correct, Qt is not from QtGui. Not in my version anyway. In the appImage version, it seems to be in both. In my version it's only in QtCore. See the post below for more info.

Jody
MrGadget49
Posts: 27
Joined: Mon Oct 03, 2022 6:31 am

Re: cannot import name 'Qt' from 'PySide.QtGui'

Post by MrGadget49 »

chennes wrote: Mon Nov 28, 2022 1:35 pm I am not at a computer to double check, but I think Qt is in QtCore.
I found out that you are correct, In my version it is only in QtCore.
MrGadget49
Posts: 27
Joined: Mon Oct 03, 2022 6:31 am

Re: cannot import name 'Qt' from 'PySide.QtGui'

Post by MrGadget49 »

heda wrote: Mon Nov 28, 2022 4:32 pm from appimage...

Code: Select all

>>> from PySide import QtCore, QtGui
>>> 'Qt' in dir(QtGui)
True
>>> 'Qt' in dir(QtCore)
True
>>> from PySide.QtCore import Qt
>>> from PySide.QtGui import Qt
>>> 
so you are probably right in that it has to do with your build,
what knob to turn is way out of my realm - you need some compilation flag whiz to get help for that...
Thanks for the clue Heda,

I ran the same from my Python console and got... (Sorry it's so big)
GetImage.png
GetImage.png (93.89 KiB) Viewed 2138 times
So, based on that, I did a little playing around with the include on line 1 of my original file and replaced it with:

Code: Select all

from PySide import QtGui, QtCore
from PySide.QtGui import QWidget,QListWidget,QListWidgetItem,QLabel,QPushButton,QHBoxLayout,QVBoxLayout
I just deleted everything from my original line except QtGui & QtCore. Then I added them in one at a time as they came up missing. In the end, I didn't even need 'Qt' and the script runs.

I'm still certain that there is something about my build that isn't quite rite. The script did run on the appImages as it was. I would really like to understand why. If there are any compilation flag wizards out there who could shed any light on it, I would love to know.

Thanks again Heda, You really helped get me going in the rite direction.

Jody
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: cannot import name 'Qt' from 'PySide.QtGui'

Post by Kunda1 »

heda wrote: Mon Nov 28, 2022 4:32 pm

Code: Select all

>>> from PySide import QtCore, QtGui
>>> 'Qt' in dir(QtGui)
True
>>> 'Qt' in dir(QtCore)
True
>>> from PySide.QtCore import Qt
>>> from PySide.QtGui import Qt
>>> 
An aside: this is a great block of code to add to the wiki somewhere. Maybe in the poweruser section or scripting ? Maybe in FreeCAD_Scripting_Basics ?
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
MrGadget49
Posts: 27
Joined: Mon Oct 03, 2022 6:31 am

Re: cannot import name 'Qt' from 'PySide.QtGui'

Post by MrGadget49 »

Kunda1 wrote: Wed Nov 30, 2022 12:38 pm An aside: this is a great block of code to add to the wiki somewhere. Maybe in the poweruser section or scripting ? Maybe in FreeCAD_Scripting_Basics ?
I agree, I've already added it to my toolbox! :)

Jody
Post Reply