Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I've successfully populated a sqlite table with an HTML-encoded-table as a field attribute, and so there is literally a Table within a single field value in sqlite, for each record. This is going to join with spatial features in a spatialite table to replicate a One polygon to Many records relationship. Map Tips can already successfully pop up the table when the mouse moves over the feature.

Now I'd like to increase functionality so that I can copy from the table, and scroll down through larger tables. I want it to open up in a browser box ( The largest tables don't show properly because the Map Tips pop-up window has its top margin aligned outside the monitor space, and the html table itself is aligned to the top of that map tips pop-up widow -- which I'm not worrying about trying to resolve )

I'm looking at a QGIS-Python-QT4-Brower example from this site: http://linfiniti.com/2012/03/a-python-layer-action-to-open-a-wikipedia-page-in-qgis/ titled "A python layer action to open a wikipedia page in QGIS" I saw that I might be able to do what I want with Python and QT4-Brower Window that's already a part of the QT4 packages that come with QGIS. I just need to run an Action on the field that contains my HTML table and have it pop up in that QT4 Browser, which would be more versatile than the Map Tips pop-up. The only difference with what I want to do is that I want to view the html-data in my field with the Qt4-browser rather than have the Qt4 browser open a designated wikipedia page. But I can't figure out how to pass the data to the browser.. I need it to already be in a file.

My knowledge of Python is limited to basic scripting tasks, iterating through Lists with cursors etc. I figure the "A python layer action to open a wikipedia page in QGIS" might provide part of the code that someone else could more easily come up it. Does anyone know how to pass data into a Qt4 web-browser, rather than opening a file with the data already in it?

UPDATE: I'm now very close to solving this by scripting Python to write output to a myTable.html file. But, when passing the string of data from my attribute table field [% MyFieldName %] I get an error

PYTHON

mytable = [% MyFieldName %]

(that causes: " TypeError: expected a character buffer object")

I can't pass the string of data from the [% MyFieldName %] to the mytable variable without a Python Error box popping up instead of Qt4 Browser Renderer, but using this test script below (as a single line) I can get the Qt4 Browser to popup with the text "my html table will go right here":

mytable = "my html table will go right here"; 
tabfile = open('C:\MyFolder\myTable.html', 'w'); 
tabfile.write(mytable);
tabfile.close(); 
from PyQt4.QtCore import QUrl; 
from PyQt4.QtWebKit import QWebView; 
myWV = QWebView(None); 
myWV.load(QUrl('file:///C:/OSGeo4W/myTable.html')); 
myWV.show()

The funny thing is that by changing the value of the mytable variable above to = [% MyFieldName %] causes the QGIS Action to throw up a popup PYTHON ERROR Dialog Box THAT ACTUALLY RENDERS MY HTML STRING INTO A TABLE PROPERLY, JUST LIKE I WANT IT, but followed by syntax error message and other details of the program and entire script. And so I know that [% MyFieldName %] does pass the data from QGIS (to atleast) the Python Error Dialog box which can render html tables :-/

Now when I only do a one line test script setting a Python Variable to someXYZ = [% MyFieldName %] then that generates the TypeError: expected a character buffer object

share|improve this question
add comment

1 Answer

The problem is in the Python script. [% MyFieldName %] is sent to Python, replacing the object with the data within the field, AS A VARIABLE, NOT AS A LITERAL, and so whatever the replacement value is, in my case a long html-table text string, perhaps hundreds of characters long, Python could only interpret it as a variable name.

Instead of:

*mytable = [% MyFieldName %] *

simply replace with

mytable = u'[% MyFieldName %]'

and so my entire python script, on one line, is...

mytable = u'[% "MyFieldName" %]'; tabfile = open('C:\myFolder\myFile.html', 'w'); tabfile.write(mytable); tabfile.close(); from PyQt4.QtCore import QUrl; from PyQt4.QtWebKit import QWebView; myWV = QWebView(None); myWV.load(QUrl('file:///C:/myFolder/myFile.html')); myWV.show()

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.