-1


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 ?

Community
  • 1
  • 1
yoK0
  • 325
  • 1
  • 3
  • 17

2 Answers2

1

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...

fstafforini
  • 143
  • 6
  • That is very helpful. To be honest, I didnt know I can use named params, and the dict option, awesome. Just one question tho, do the sequence of parameters has to be preserved ? or I could just use 'third' 'first' 'second' and get the same output ? Oh, and thanks a lot! – yoK0 Jun 05 '14 at 06:21
  • You're welcome. The order is not important in keyword parameters (ie myfunc(first='Joe', second='something'); it is important in positional parameters (ie myfunc('Joe', 'something'). This is a python language feature, not something specific to the format function, you can use keyword or positional parameters as you choose. Using myfunc(**some_dict) unpacks some_dict into keyword parameters for the function. Argument unpacking is also a language feature, use *my_list to unpack list or tuples or similar iterables, and **my_dict to unpack hash table types like dict. – fstafforini Jun 05 '14 at 14:03
0
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_())
IKavanagh
  • 6,089
  • 11
  • 42
  • 47