How to embed Veusz in another PyQt application

Here is a simple program which embeds a Veusz plot window in another Qt widget. The SimpleWindow class provides a simple plot containing a document.

   1 import sys
   2 from math import pi
   3 
   4 import PyQt4.QtCore as QtCore
   5 import PyQt4.QtGui as QtGui
   6 
   7 from veusz.windows.simplewindow import SimpleWindow
   8 from veusz.document import CommandInterface
   9 
  10 class VeuszWin(SimpleWindow):
  11     """A veusz window displaying a sin function."""
  12 
  13     def __init__(self, title):
  14         SimpleWindow.__init__(self, title)
  15 
  16         # send commands to this object to modify the window
  17         # the commands are from the standard veusz api
  18         ifc = self.interface = CommandInterface(self.document)
  19 
  20         # a basic plot win a sin function
  21         ifc.To( ifc.Add('page') )
  22         ifc.To( ifc.Add('graph') )
  23         ifc.Add('function', name='myfunc')
  24 
  25         ifc.Set( 'myfunc/function', 'sin(x)' )
  26         ifc.Set( 'x/max', pi*2 )
  27 
  28 class MainWindow(QtGui.QWidget):
  29     """Put veusz window in layout with push button."""
  30 
  31     def __init__(self):
  32         QtGui.QWidget.__init__(self)
  33 
  34         lt = QtGui.QVBoxLayout()
  35         self.veuszwin = VeuszWin("")
  36         lt.addWidget(self.veuszwin)
  37         self.button = QtGui.QPushButton("hi there")
  38         lt.addWidget(self.button)
  39         self.setLayout(lt)
  40         self.connect(self.button, QtCore.SIGNAL('clicked()'),
  41                      self.slotClicked)
  42 
  43     def slotClicked(self):
  44         filename = 'out.png'
  45         print "Writing", filename
  46         self.veuszwin.interface.Export(filename)
  47 
  48 if __name__ == '__main__':
  49     app = QtGui.QApplication(sys.argv)
  50 
  51     win = MainWindow()
  52     win.show()
  53 
  54     app.exec_()

The SimpleWindow class has some useful methods:

   1     def enableToolbar(self, enable=True):
   2         """Enable or disable the zoom toolbar in this window."""
   3             
   4     def setZoom(self, zoom):
   5         """Zoom(zoom)
   6 
   7         Set the plot zoom level:
   8         This is a number to for the zoom from 1:1 or
   9         'page': zoom to page
  10         'width': zoom to fit width
  11         'height': zoom to fit height
  12         """
  13 
  14     def setAntiAliasing(self, ison):
  15         """AntiAliasing(ison)
  16 
  17         Switches on or off anti aliasing in the plot."""

EmbeddingPyQt (last edited 2010-06-19 17:42:52 by JeremySanders)