我正在使用Qtextdocument生成报告,我已经以编程方式将表字符串准备为html字符串,但是当渲染它时,边框未显示(图2)
这是字符串示例:
<html>
<head>
<style>
table, th, td { white-space: nowrap; border: 1px solid black; border-collapse: collapse; padding: 2px 4px; text-align: center;}
</style>
</head>
<body>
<table width="100%">
<colgroup span="2" style="border: 2px solid black;">
<colgroup span="7" style="border: 2px solid black;">
<colgroup span="7" style="border: 2px solid black;">
<colgroup span="9" style="border: 2px solid black;">
<colgroup span="8" style="border: 2px solid black;">
<colgroup span="1" style="border: 2px solid black;">
<tr>
<td colspan="2" > <img src="Log1.png" width="192" height="86"/></td>
<td colspan="31" > Title line1<br/>line2</td>
<td> <img src="Log2.png" width="86" height="86" /> </td>
</tr>
<tr>
<td>Month:</td><td>june-19</td><td>01</td><td>02</td><td>03</td><td>04</td><td>05</td><td>06</td><td>07</td><td>08</td><td>09</td><td>10</td><td>11</td><td>12</td><td>13</td><td>14</td><td>15</td><td>16</td><td>17</td><td>18</td><td>19</td><td>20</td><td>21</td><td>22</td><td>23</td><td>24</td><td>25</td><td>26</td><td>27</td><td>28</td><td>29</td><td>30</td><td>31</td><td>Total</td>
</tr>
<tr>
<td colspan="2">col span 2</td><td colspan="7">col span 7</td><td colspan="7">col span 7</td><td colspan="9">col span 9</td><td colspan="8">col span 8</td>
</tr>
<tr style="border:2px solid black;">
<td colspan="34">long line sub title</td>
</tr>
<tr>
<td rowspan="3">item1</td>
<td>item1.1</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td></td><td></td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>29</td>
<tr>
<td>item1.2</td><td></td><td></td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>29</td>
</tr>
<tr style="border-bottom:2px solid black;">
<td>item1.3</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>31</td>
</tr>
<tr>
<td rowspan="2">item2</td>
<td>item2.1</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>31</td>
<tr style="border-bottom:2px solid black;">
<td>item2.2</td><td>X</td><td></td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td></td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td></td><td>28</td>
</tr>
<tr>
<td> item3</td>
<td>item3.1</td><td>X</td><td>X</td><td>X</td><td>X</td><td></td><td></td><td></td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>28</td>
</tr>
</table>
</body>
</html>
答案 0 :(得分:0)
表面上仅支持一部分HTML,而不是使用QTextDocument
和QLineEdit
的半支持方法,您应该使用QWebEngineView
来追求成熟的方法,如果这样做的话符合您的要求。
如果您的要求仅仅是创建报告,则用户不应该在QLineEdit
中对其进行编辑,那么您绝对应该尝试QWebEngineView
。我正在使用它在各种应用程序中创建报告,并且作为PDF的打印选项绝对是一项资产。
以下内容对我有用,并很好地绘制了边框:
#include <QApplication>
#include <QWebEngineView>
#include <QIODevice>
#include <QFile>
int main(int argc, char** args) {
QApplication app(argc, args);
QFile file("./data/test.html");
file.open(QIODevice::ReadOnly);
QString content=QString::fromStdString(file.readAll().toStdString());
auto webView = new QWebEngineView;
webView->setHtml(content);
webView->show();
QObject::connect(&app, &QApplication::aboutToQuit, [&webView]()
{
webView->deleteLater(); // Prevents crash
});
app.exec();
}