简体   繁体   English

无法在 Pyqt5 中的 Qtext 编辑中使用 jinja2 设置表格的 CSS(文本换行和水平滚动条)

[英]Unable to set CSS of table (text-wrap and horizontal scroll-bar)using jinja2 inside Qtext edit in Pyqt5

I am trying to display a table inside Qtextedit using jinja2.我正在尝试使用 jinja2 在 Qtextedit 中显示一个表格。 My table contains several headers, and what it has done it, it tries to display all the headers all at once on single display due to which it wraps the text inside table.我的表格包含几个标题,它做了什么,它尝试在单个显示器上一次显示所有标题,因为它将文本包装在表格中。 I tried to add a x-scroll bar and white-space: nowrap, but it doesn't make any difference.我尝试添加一个 x-scroll bar 和 white-space: nowrap,但它没有任何区别。

Is there any specific problem due to Qtextedit or anything I am not able to understand.由于 Qtextedit 或任何我无法理解的问题,是否有任何具体问题。 Here is the code that I am using这是我正在使用的代码

table = """
                        <style>
                        .table_wrapper{
                                        display: block;
                                        overflow-x: auto;
                                        white-space: nowrap;
                                    }
                        table {
                            font-family: arial, sans-serif;
                            border-collapse: collapse;
                            width: 100%;
                            overflow-x: auto;
                            border: 1px solid black;
                            table-layout: fixed
                        }

                        td {
                            border: 1px solid #dddddd;
                            text-align: center;
                            padding: 8px;
                            white-space: nowrap;
                            width: 100px;

                        }
                        th { 
                            border: 1px solid #dddddd;
                            text-align: center;
                            padding: 8px;
                            white-space: nowrap;
                            width: 100px;
                         } 
                        div {
                              overflow: auto;;
                            }
                        </style>

                    <div class="table_wrapper">
                        <table border="1">
                            <tr>{% for header in headers %}<th>{{header}}</th>{% endfor %}</tr>
                            {% for row in rows %}<tr>
                                {% for element in row %}<td>
                                    {{element}}
                                </td>{% endfor %}
                            </tr>{% endfor %}
                        </table>
                    </div>
                        """

QTextDocument (which is the class that renders in QTextEdit) only supports CSS 2.1, so probably the style you use uses properties from a higher version, for example "overflow-x" is part of CSS3. QTextDocument(即在 QTextEdit 中呈现的 class)仅支持 CSS 2.1,因此您使用的样式可能使用了更高版本的属性,例如“overflow-x”是 Z2BE25667A1DD3387FFE30B9 的一部分。

In this case a possible solution is to use QWebEngineView.在这种情况下,一个可能的解决方案是使用 QWebEngineView。

In the following example I show how the same html is rendered in QTextEdit and QWebEngineView:在下面的示例中,我展示了相同的 html 如何在 QTextEdit 和 QWebEngineView 中呈现:

from jinja2 import Environment
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets

template = """
<style>
    .table_wrapper{
        display: block;
        overflow-x: auto;
        white-space: nowrap;
    }
    table {
        font-family: arial, sans-serif;
        border-collapse: collapse;
        width: 100%;
        overflow-x: auto;
        border: 1px solid black;
        table-layout: fixed
    }
    td {
        border: 1px solid #dddddd;
        text-align: center;
        padding: 8px;
        white-space: nowrap;
        width: 100px;
    }
    th {
        border: 1px solid #dddddd;
        text-align: center;
        padding: 8px;
        white-space: nowrap;
        width: 100px;
    }
    div {
        overflow: auto;;
    }
</style>

<div class="table_wrapper">
    <table border="1">
    <tr>{% for header in headers %}<th>{{header}}</th>{% endfor %}</tr>
    {% for row in rows %}<tr>
        {% for element in row %}<td> 
            {{element}}
            </td>
        {% endfor %}
        </tr>
    {% endfor %}
    </table>
</div>"""


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    app.setStyle("fusion")

    headers = "Stack Overflow".split()
    rows = [("col-1", "col-2") for i in range(100)]
    html = Environment().from_string(template).render(headers=headers, rows=rows)

    w = QtWidgets.QSplitter()

    te = QtWidgets.QTextEdit()
    te.setHtml(html)

    view = QtWebEngineWidgets.QWebEngineView()
    view.setHtml(html)

    w.addWidget(te)
    w.addWidget(view)
    w.show()
    sys.exit(app.exec_())

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM