PyQt embedding using qt designer
- Dominant language
- Python
- Stars
- 1.4k
- Forks
- 316
- Avg merge
- 7h 44m
- Merged PRs (30d)
- 5
Description
Hi!
I am working on an open source project (IRV360.tk) and I have a problem when I try to embed Mayavi widget in my application. The problem is in using pyqt 4 since it does not recognize the type of widget.
I was trying several ways but I can not correct it.
I leave a code with the same problem below.
Thanks!
import sys
import os
os.environ['ETS_TOOLKIT'] = 'qt4'
import sip
sip.setapi('QString', 2)
###################################################################################
# THIS WILL CREATE THE INTERFACE (MADE WITH QT DESIGNER)
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(370, 371)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gridLayout = QtGui.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 370, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.dockWidget_mplwidget = QtGui.QDockWidget(MainWindow)
self.dockWidget_mplwidget.setObjectName(_fromUtf8("dockWidget_mplwidget"))
self.dockWidgetContents_mplwidget = QtGui.QWidget()
self.dockWidgetContents_mplwidget.setObjectName(_fromUtf8("dockWidgetContents_mplwidget"))
self.gridLayout_2 = QtGui.QGridLayout(self.dockWidgetContents_mplwidget)
self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))
self.mplwidget = MatplotlibWidget(self.dockWidgetContents_mplwidget)
self.mplwidget.setObjectName(_fromUtf8("mplwidget"))
self.gridLayout_2.addWidget(self.mplwidget, 0, 0, 1, 1)
self.dockWidget_mplwidget.setWidget(self.dockWidgetContents_mplwidget)
MainWindow.addDockWidget(QtCore.Qt.DockWidgetArea(1), self.dockWidget_mplwidget)
self.dockWidget_Mayavi = QtGui.QDockWidget(MainWindow)
self.dockWidget_Mayavi.setObjectName(_fromUtf8("dockWidget_Mayavi"))
self.dockWidgetMayavi = QtGui.QWidget()
self.dockWidgetMayavi.setObjectName(_fromUtf8("dockWidgetMayavi"))
self.dockWidget_Mayavi.setWidget(self.dockWidgetMayavi)
MainWindow.addDockWidget(QtCore.Qt.DockWidgetArea(1), self.dockWidget_Mayavi)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
from matplotlibwidget import MatplotlibWidget
class MainView(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
#########################################
#MAYAVI WIDGET
# By default, the PySide binding will be used. If you want the PyQt bindings
# to be used, you need to set the QT_API environment variable to 'pyqt'
#os.environ['QT_API'] = 'pyqt'
# To be able to use PySide or PyQt4 and not run in conflicts with traits,
# we need to import QtGui and QtCore from pyface.qt
#from pyface.qt import QtGui, QtCore
# Alternatively, you can bypass this line, but you need to make sure that
# the following lines are executed before the import of PyQT:
# import sip
# sip.setapi('QString', 2)
from traits.api import HasTraits, Instance, on_trait_change
from traitsui.api import View, Item
from mayavi.core.ui.api import MayaviScene, MlabSceneModel, \
SceneEditor
################################################################################
#The actual visualization
class Visualization(HasTraits):
scene = Instance(MlabSceneModel, ())
@on_trait_change('scene.activated')
def update_plot(self):
# This function is called when the view is opened. We don't
# populate the scene when the view is not yet open, as some
# VTK features require a GLContext.
# We can do normal mlab calls on the embedded scene.
self.scene.mlab.test_points3d()
# the layout of the dialog screated
view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene),
height=250, width=300, show_label=False),
resizable=True # We need this to resize with the parent widget
)
################################################################################
# The QWidget containing the visualization, this is pure PyQt4 code.
class MayaviQWidget(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
layout_vi = QtGui.QVBoxLayout(self)
layout_vi.setContentsMargins(0,0,0,0)
layout_vi.setSpacing(0)
self.visualization = Visualization()
# If you want to debug, beware that you need to remove the Qt
# input hook.
#QtCore.pyqtRemoveInputHook()
#import pdb ; pdb.set_trace()
#QtCore.pyqtRestoreInputHook()
# The edit_traits call will generate the widget to embed.
self.vi = self.visualization.edit_traits(parent=self,
kind='subpanel').control
layout_vi.addWidget(self.vi)
self.vi.setParent(self)
#####################################################################
# THE MAIN LOOP
if __name__== "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MainView()
myapp.ui.dockWidgetMayavi = QtGui.QWidget()
myapp.ui.dockWidget_Mayavi.setWindowTitle("Embedding Mayavi in a PyQt4 Application")
# define a "complex" layout to test the behaviour
layout = QtGui.QGridLayout(myapp.ui.dockWidgetMayavi)
mayavi_widget = MayaviQWidget(myapp.ui.dockWidgetMayavi)
layout.addWidget(mayavi_widget, 1, 1)
myapp.ui.dockWidgetMayavi.show()
myapp.show()
sys.exit(app.exec_())
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.