【问题标题】:Qt Python - report in toolbox: QTextDocument and QPainterQt Python - 工具箱中的报告:QTextDocument 和 QPainter
【发布时间】:2010-01-01 07:55:32
【问题描述】:

我想使用工具箱构建多个文档报告。两页是一个开始的选择。格式化没问题,以后可以用。

我尝试在 Html 中使用 QTextDocument,或者 QPainter。

当然,为了进行测试并保持简单,我只是要求在 Qt 中显示显示在文档顶部的报告标题。

工具箱主框架的功能如下:

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

... 使用带有 Html 的 QTextDocument 保存第一页的函数:

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)

这里是使用 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")

问题在于,它只显示第 1 页和第 2 页的工具箱,而不是第 1 页和第 2 页内的报告的标题。

这里缺少什么?

非常感谢所有 cmets 和建议。

【问题讨论】:

    标签: python html qt reportviewer qpainter


    【解决方案1】:

    对于page1,文档需要通过一个小部件来显示。将以下内容添加到该函数中

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

    对于 page2,小部件上的绘画必须响应需要创建子类或事件过滤器的绘画事件。绘制一些文本的更简单方法是使用 QLabel。把函数改成如下

    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()))
    

    【讨论】:

    • 感谢贝史密斯!你刚刚挽救了这一天。效果很好!
    猜你喜欢
    • 1970-01-01
    • 2021-10-17
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 2016-11-26
    相关资源
    最近更新 更多