PyQt

PyQt is a Python wrapper around the cross-platform Qt application framework. This post describes how to install PyQt with Vizard. It also includes a sample script showing how to embed the Vizard graphics window inside a PyQt application.

Installation:

  • Go to the PyQt download page
  • Download the Windows 32-bit version of the binary installer for the version of Python used by Vizard.  Use the table below to determine which version of Python your Vizard installation uses:
    • Vizard 2.x uses Python 2.3
    • Vizard 3.x uses Python 2.4
    • Vizard 4.x uses Python 2.7
    • Vizard 5.x uses Python 2.7
  • Run the installer. It should automatically detect Vizard's Python installation. If you have multiple Python installations on your computer, make sure you select the Vizard Python installation.

You should now be able to use the PyQt library within Vizard. Browse through the documentation and example scripts to learn how to use the module.

Once you have some experience using Qt you can start embedding your Vizard scripts into a Qt application.  The biggest difference between a standard Vizard script and an embedded Vizard script is that you must manually control the Vizard mainloop within the embedded version.

The sample script below shows how to embedd the Vizard graphics window within a Qt application.  The key steps are noted with the TODO comments.

# This is only needed for Python v2 but is harmless for Python v3.
import sip
sip.setapi('QVariant', 2)

from PyQt4 import QtCore, QtGui

import viz
import vizact

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.resize(800,600)
        self.setWindowTitle('Vizard PyQt Example')

        # Create a blank widget to be used as Vizard graphics window
        self.widget = QtGui.QWidget()
        self.setCentralWidget(self.widget)

        # TODO: Initialize Vizard using the existing QtWidget window ID
        viz.go(viz.EMBEDDED,window=int(self.widget.winId()))
       
        # Prevent default escape key from closing Vizard window
        viz.setOption('viz.default_key.quit',0)

        # Add a spinning logo
        self.model = viz.add('logo.ive',pos=(0,1,4))
        self.model.addAction(vizact.spin(0,1,0,90))

        # TODO: Setup a 60 Hz timer to update the Vizard graphics loop
        self.vizTimer = QtCore.QTimer()
        self.vizTimer.start(16)
        QtCore.QObject.connect(self.vizTimer,
                QtCore.SIGNAL("timeout()"),
                self.updateVizard)
       
        # Initialize menu, toolbar, etc..
        self.createActions()
        self.createMenus()
        self.createStatusBar()
        self.setUnifiedTitleAndToolBarOnMac(True)

    def updateVizard(self):
        # TODO: Manually update the Vizard graphics frame
        viz.frame()

    def open(self):
        fileName = QtGui.QFileDialog.getOpenFileName(self)

    def save(self):
        fileName = QtGui.QFileDialog.getSaveFileName(self)

    def about(self):
        QtGui.QMessageBox.about(self, "About",
                "This application demonsrates how to "
                "integrate Vizard within a PyQt application")

    def createActions(self):
        self.openAct = QtGui.QAction("&Open...", self,
                shortcut=QtGui.QKeySequence.Open,
                statusTip="Open an existing file",
                triggered=self.open)

        self.saveAct = QtGui.QAction("&Save", self,
                shortcut=QtGui.QKeySequence.Save,
                statusTip="Save the document to disk",
                triggered=self.save)

        self.exitAct = QtGui.QAction("E&xit", self,
                shortcut="Ctrl+Q",
                statusTip="Exit the application",
                triggered=self.close)

        self.aboutAct = QtGui.QAction("&About", self,
                statusTip="Show the application's About box",
                triggered=self.about)

        self.aboutQtAct = QtGui.QAction("About &Qt", self,
                statusTip="Show the Qt library's About box",
                triggered=QtGui.qApp.aboutQt)

    def createMenus(self):
        self.fileMenu = self.menuBar().addMenu("&File")
        self.fileMenu.addAction(self.openAct)
        self.fileMenu.addAction(self.saveAct)
        self.fileMenu.addSeparator();
        self.fileMenu.addAction(self.exitAct)

        self.helpMenu = self.menuBar().addMenu("&Help")
        self.helpMenu.addAction(self.aboutAct)
        self.helpMenu.addAction(self.aboutQtAct)

    def createStatusBar(self):
        self.statusBar().showMessage("Ready")


if __name__ == '__main__':

    import sys

    app = QtGui.QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    app.exec_()