Hello everybody.
I’m currently working in Maya 2014 and I’ve got a following issue.
Suppose you have a QTableWidget. QTableWidget has a method horizontalHeader() which returns a QHeaderView instance.
You can ask this QHeaderView with sortIndicatorSection() to retrieve it’s current sorted column.
So, the whole statement should looks like this:
[PRE]sortedSection = self.myTableWidget.horizontalHeader().sortIndicatorSection()[/PRE]
Now suppose you want to remember this QHeaderView in some instance variable and access this QHeaderView without querying QTableWidget in your other methods:
[PRE]self.headerView = self.myTableWidget.horizontalHeader()[/PRE]
and later:
[PRE]sortedSection = self.headerView.sortIndicatorSection()[/PRE]
This is the problem if you using PySide.
Please run a code below in Maya and press a button, then look a Script Editor.
from PySide import QtGui, QtCore
import shiboken
import maya.OpenMayaUI as omui
mainWin = None
class TestUI(QtGui.QMainWindow):
def __init__(self, parent):
super(TestUI, self).__init__(parent=parent)
self.centralWidget = QtGui.QWidget()
self.setCentralWidget(self.centralWidget)
self.layout = QtGui.QVBoxLayout()
self.centralWidget.setLayout(self.layout)
self.lineEdit = QtGui.QLineEdit('defaultText')
self.layout.addWidget(self.lineEdit)
self.font = self.lineEdit.font()
self.tableWidget = QtGui.QTableWidget()
self.layout.addWidget(self.tableWidget)
self.horHeader = self.tableWidget.horizontalHeader()
self.button = QtGui.QPushButton('Click Me')
self.layout.addWidget(self.button)
self.button.connect(self.button, QtCore.SIGNAL('clicked()'), self.onButtonClicked)
print 'try to access variables right after creation:'
self.onButtonClicked()
print
def onButtonClicked(self):
print 'QTableWidget -> QHeaderView: runtime: sortSection =', self.tableWidget.horizontalHeader().sortIndicatorSection()
try:
print 'QTableWidget -> QHeaderView: cached: sortSection =', self.horHeader.sortIndicatorSection()
except Exception as e:
print 'C++ object is dead. Exception:', str(e)
print 'QLineEdit -> QFont: runtime: font is italic =', self.lineEdit.font().italic()
try:
print 'QLineEdit -> QFont: cached: font is italic =', self.font.italic()
except Exception as e:
print 'C++ object is dead. Exception:', str(e)
def run():
global mainWin
if not mainWin:
ptr = omui.MQtUtil.mainWindow()
if ptr:
mayaQMainWindow = shiboken.wrapInstance(long(ptr), QtGui.QMainWindow)
else:
raise Exception('Cannot find Maya main window.')
mainWin = TestUI(parent=mayaQMainWindow)
# mainWin = TestUI(parent=None)
mainWin.show()
mainWin.raise_()
It will raise an exception when you will try to access some methods of this “cached” object.
What I’ve found out:
[ul]
[li]Variables are alive just after creation.[/li][li]If you change parent of TestUI to None, instead of Maya main window, everything will be fine.[/li][li]If you run this code in standalone PySide application, again it will be ok. I think cause there will be no Maya main window as parent.[/li][li]Not all PySide objects will be destroyed. As you can see from my code, QFont object is still alive.[/li][li]If you will create an instance of that particular object, instead of querying it (of course it is not the same by logic, just for testing), C++ object will not be destroyed. In my example it will be self.horHeaderCreated = QtGui.QHeaderView(QtCore.Qt.Horizontal)[/li][li]If you using PyQt, everything will be fine. You’ll need to change imports, shiboken to sip and shiboken.wrapInstance to sip.wrapinstance.[/li][li]Currently i’m having a problem with these methods and objects:[/li][/ul]
[PRE]
QScrollArea.horizontalScrollBar() -> QScrollBar
QScrollArea.verticalScrollBar() -> QScrollBar
QTreeView.header() -> QHeaderView
QTableView.horizontalHeader() -> QHeaderView
Q…View/Widget.selectionModel() -> QItemSelectionModel
[/PRE]
May be i’m doing something wrong?
Is there any workarounds except of: not making Maya window a parent of your ui, always get these objects from Qt controls and do not cache them?
Do you have any additional info about this issue (may be its a known problem or some discussion about it)?