Issue
Hey there
I am new and this is my first little project, so be understanding.
Im in need of creating a layout for a document which will be used as a template, and which at the end will be filled with data gathered from database and printed. Havent created db yet
so, for now, Ive set up local vars to get my head around this. I want each of this little
pieces of data to be in very specific place. I Checked QTextDocument docs, and it looks like
good tool for the job, but I have no idea how to create layout for it.
Any pointers much appreciated.
EDIT: There is not much on the web about this subject. Although I was able to find this thread
This is nearly what Im looking for. However, there are many variables I wish to put into html, therefore ".format" options seems little overwhelming. Anyone knows of a better solution ?
Solution
There are many HTML enabled widgets in Qt. QTextDocument should work, also QLabel. In my project I've used QtWebKit.QWebView because I needed tables and I think other widgets didn't do them (but I'm not really sure anymore, check the docs if you need this).
In my opinion, ".format" is not overwhelming if you use named parameters, and was good enough for my purposes. Using a dictionary for the parameter values might also be helpful
Sample code:
from PyQt4 import QtGui
from PyQt4 import QtWebKit
class DisplayHTML(QtWebKit.QWebView):
def __init__(self, html, parent=None):
super().__init__(parent)
self.setHtml(html)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
html_template = "<html><p>Hello, {first}, how {second} you {third}?</p></html>"
values_dict = {
'first': 'Joe',
'second': 'are',
'third': 'today',
}
html_ready_to_render = html_template.format(**values_dict)
window = DisplayHTML(html_ready_to_render)
window.show()
sys.exit(app.exec_())
EDIT: you don't need to create a new class for doing something as simple as this, just use a QWebView() and call setHtml() on it...
Answered By - fstafforini
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.