MapInfo map control into Qt Python form


Tonight for a bit of fun, or shits and jiggles as we say here, I thought I would try and embed a MapInfo map control into a Qt python widget (although I should be studying, but it’s Saturday night) .

Turns out it is pretty easy!

pls send me teh codez? OK here you go.

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from win32com.client import Dispatch
import sys

app = QApplication(sys.argv)
app.setAttribute(Qt.AA_NativeWindows,True)
wnd = QMainWindow()
wnd.resize(400, 400)
widget = QWidget()
wnd.setCentralWidget(widget)
wnd.show()

handle = int(widget.winId())
mapinfo = Dispatch("MapInfo.Application")
mapinfo.do('Set Next Document Parent %s Style 1' % handle)
mapinfo.do('Open Table "D:\GIS\MAPS\Property.TAB"')
mapinfo.do('Map from Property')

app.exec_()

The above code will load MapInfo and open the property layer into the Qt Widget control, with the result below.

MapInfo map in python Qt based form

So this means you don’t “always” have to write your MapInfo based apps in C# or C++; of course I already knew this as anything that can use OLE and provide a native window handle to MapInfo will work, I just never tried it.

6 thoughts on “MapInfo map control into Qt Python form

  1. Many many thanks!!! You opened a door to me. I had just to use “/” instead of “\”.
    Waiting for your next trick ;-)

    1. Glad someone found it useful :D Just a note: The only thing I haven’t worked out yet is how to do MapInfo callbacks but I’m working on it.

  2. Thanks Nathan.

    This is how I got it going in wxPython:

    ==
    #!/usr/bin/env python

    import wx
    import sys
    import win32com.client

    class MyApp(wx.App):

    def OnInit(self):
    self.init_frame()
    return True

    def init_frame(self):
    self.frame = wx.Frame(None, wx.ID_ANY, “MapInfo window – compliments WxPython!”, (100,100), (640,480))
    self.init_panel()
    self.frame.Show()
    self.SetTopWindow(self.frame)

    def init_panel(self):
    self.panel = wx.Panel(self.frame, wx.ID_ANY,self.frame.GetPositionTuple(),self.frame.GetSizeTuple())
    self.init_MapInfo()

    def init_MapInfo(self):
    print self.panel.GetHandle()
    handle = int(self.panel.GetHandle())
    self.mapinfo = win32com.client.Dispatch(“MapInfo.Application”)
    self.mapinfo.do(‘Set Next Document Parent %s Style 1’ % handle)
    self.mapinfo.do(‘Open Table “W:\QM370642 MDF\_temp\mapping_test\STE06aAUST.tab”‘)
    self.mapinfo.do(‘Map from GroupLayer (“STATE_NAME_2006”,STE06aAUST)’)

    if __name__ == ‘__main__’:
    app = MyApp(False)
    app.MainLoop()

Leave a comment