views:

272

answers:

1

Hi Everybody,

I want to build multiple documents report using toolbox. Two pages is an option get a start. Formatting is ok, and can be worked latter.

I tried using QTextDocument in Html, and alternatively QPainter.

Of course, to make a test and keep things simple, I just ask in Qt to show the report title displayed on top of the document.

Here is the function for the toolbox main frame:

def toolbox_frame(self,MainWindow):
    self.toolBox = QtGui.QToolBox(self.centralwidget)
    self.toolBox.setGeometry(QtCore.QRect(10, 20, 471, 201))

    self.toolbox_page1()
    self.toolBox.addItem(self.page1, "")
    self.toolBox.setItemText(self.toolBox.indexOf(self.page1), QtGui.QApplication.translate("MainWindow", "Page 1", None, QtGui.QApplication.UnicodeUTF8))

    self.toolbox_page2()
    self.toolBox.addItem(self.page2, "")
    self.toolBox.setItemText(self.toolBox.indexOf(self.page2), QtGui.QApplication.translate("MainWindow", "Page 2", None, QtGui.QApplication.UnicodeUTF8))

... the function that holds the first page using QTextDocument with Html:

def toolbox_page1(self):
    self.page1 = QtGui.QWidget()
    self.page1.setGeometry(QtCore.QRect(0, 0, 471, 145))

    html = u""
    html += (" <p><font color=red><b>Title - Build "
                     "a Report : page 1.</b></font>")
    document = QtGui.QTextDocument(self.page1)
    document.setHtml(html)

and here the function using QPainter:

def toolbox_page2(self):
    self.page2 = QtGui.QWidget()
    self.page2.setGeometry(QtCore.QRect(0, 0, 471, 145))

    sansFont = QtGui.QFont("Helvetica", 10)
    painter = QtGui.QPainter(self.page2)
    painter.setFont(sansFont)
    painter.setPen(QtGui.QColor(168, 34, 3))
    x=50
    y=50
    painter.drawText(x, y, "Title - Build a Report : page 2")

The problem is, that it just displays the toolbox with the page 1 and page 2, but not the title for both report inside the page 1 and page 2.

What is missing here?

All comments and suggestions are highly appreciated.

+1  A: 

For page1, the document needs to be displayed by a widget. Add the following to that function

    textEdit = QtGui.QTextEdit(self.page1)
    textEdit.setDocument(document)
    layout = QtGui.QVBoxLayout(self.page1)
    layout.addWidget(textEdit)

For page2, painting on a widget must be in response to a paint event which requires creating a subclass or event filter. A simpler way to draw some text is using a QLabel. Change the function to the following

def toolbox_page2(self):
    self.page2 = QtGui.QWidget()
    self.page2.setGeometry(QtCore.QRect(0, 0, 471, 145))

    label = QtGui.QLabel(self.page2)
    label.setText("Title - Build a Report : page 2")
    label.setStyleSheet("font: 10pt 'Helvetica'; color: rgb(168, 34, 3)")
    label.setGeometry(QtCore.QRect(QtCore.QPoint(50, 50), label.sizeHint()))
baysmith
Thanks BaySmith! You just saved the day. It just worked beautifully!
ThreaderSlash