Error: 'module' object has no attribute 'QVBoxLayout'

1,808 views
Skip to first unread message

jettam

unread,
Oct 17, 2017, 3:17:02 PM10/17/17
to Python Programming for Autodesk Maya

When I run this code I get this error. Any help please.    Yes I am running it from a module. 

# Error: 'module' object has no attribute 'QVBoxLayout'
# Traceback (most recent call last):
#   File "<maya console>", line 4, in <module>
#   File "E:\ProfessionalDevelopment\python\Introduction to Python Scripting in Maya\cgcircuitPython\wk6\geomGenerator.py", line 18, in run
#     win = GeometryGenerator(parent=getMayaWindow())
#   File "E:\ProfessionalDevelopment\python\Introduction to Python Scripting in Maya\cgcircuitPython\wk6\geomGenerator.py", line 36, in __init__
#     layout = QtGui.QVBoxLayout()
# AttributeError: 'module' object has no attribute 'QVBoxLayout' #


import os
from PySide2 import QtWidgets, QtCore, QtUiTools, QtGui
from shiboken2 import wrapInstance
import maya.cmds as mc
import maya.OpenMayaUI as omui

def getMayaWindow():
   
''' pointer to the maya main window '''
    ptr
= omui.MQtUtil.mainWindow()
   
if ptr:
       
return wrapInstance(long(ptr), QtWidgets.QMainWindow)

def run():
   
''' builds our UI '''
   
global win
    win
= GeometryGenerator(parent=getMayaWindow())
    win
.show()

uiFilePath
= os.path.join( os.path.dirname(__file__) , 'geomGenerator_v001.ui')

class GeometryGenerator(QtWidgets.QDialog):
   
   
def __init__(self,parent=None):
       
super(GeometryGenerator,self).__init__(parent)

        loader
= QtUiTools.QUiLoader()    
       
print('uiFilePath: {0}'.format(uiFilePath))  
        xfile
= QtCore.QFile(uiFilePath)        
        xfile
.open(QtCore.QFile.ReadOnly)        
       
self.ui = loader.load(xfile, parentWidget=self)        
        xfile
.close()

        layout
= QtGui.QVBoxLayout()
        layout
.addWidget(self.ui)
       
self.setLayout(layout)



Justin Israel

unread,
Oct 17, 2017, 3:34:56 PM10/17/17
to python_in...@googlegroups.com
When you see errors like this you need to think about what it is trying to tell you. It says the QtGui module has no QVBoxLayout. So what you want to do next is check the documentation to know where things are. 


Note that in Qt5, widgets have been moved into a QtWidgets namespace. So if you are following Qt4 tutorials then you will need to make that adjustment to your code. 

Here is the PyQt version of the difference between v4 and v5




--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/84ea6669-7cac-49d7-8a81-10d071830a0d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jettam

unread,
Oct 17, 2017, 5:12:27 PM10/17/17
to Python Programming for Autodesk Maya


The only way I was able to solve this was not from the documentation, but from an earlier post I sent out which told me Qt has a new module called QtWidgets it holds many of the attributes that were previously found in QtGui. 
There was nothing in the documentation that I could understand (as a freshman) That told me that QVBoxLayout is found in QtWidgets. And that the syntax should be written like this QtWidgets.QVBoxLayout(). So reading the documentation unfortunately didn't clue me into the answer. 

Ultimately the answer was
layout = QtGui.QVBoxLayout()       #  from this        
layout
= QtWidgets.QVBoxLayout()   #  into this

FYI; Sublime auto-fills in QtGui.QVBoxLayout and it also auto-filled in QtWidgets.QVBoxLayout which made me think it did exist in QtGui.









The only way I solved this was not from looking a the Qt Documentation, 

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Justin Israel

unread,
Oct 17, 2017, 5:16:52 PM10/17/17
to python_in...@googlegroups.com
On Wed, Oct 18, 2017 at 10:12 AM jettam <justin...@gmail.com> wrote:


The only way I was able to solve this was not from the documentation, but from an earlier post I sent out which told me Qt has a new module called QtWidgets it holds many of the attributes that were previously found in QtGui. 
There was nothing in the documentation that I could understand (as a freshman) That told me that QVBoxLayout is found in QtWidgets. And that the syntax should be written like this QtWidgets.QVBoxLayout(). So reading the documentation unfortunately didn't clue me into the answer. 

Ultimately the answer was
layout = QtGui.QVBoxLayout()       #  from this        
layout
= QtWidgets.QVBoxLayout()   #  into this

FYI; Sublime auto-fills in QtGui.QVBoxLayout and it also auto-filled in QtWidgets.QVBoxLayout which made me think it did exist in QtGui.

It would depend on what paths your IDE is set up with and looking at to provide completions. 

When you look at the Qt docs, you can look at the very top of the documentation for class, to see under which module/namespace it resides. Also there is documentation that breaks down classes by each module:



To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/3723cd75-3142-43a4-b94b-5f74d579e25e%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages