当涉及到显示图像(png格式)时,我对QWebView
有一个问题,我的主要目的是将其用作报告生成器。我已经阅读了其他建议,比如将imageformats
放在应用程序文件夹下面,设置图像的完整路径,从资源加载图像,根据W3C标准验证我的html
,最后在另一台干净的机器中测试我的应用程序,看看问题是否只是来自我的机器。
void SemesterResultsReport::printerSetup() {
QPrinter *printer = new QPrinter(QPrinter::ScreenResolution);
printer->setFullPage(true);
printer->setResolution(90);
printer->setPaperSize(QPrinter::A4);
printer->setOrientation(QPrinter::Landscape);
printer->setPageMargins(5, 5, 5, 5, QPrinter::Millimeter);
QPrintPreviewDialog *dlg = new QPrintPreviewDialog(printer, this);
connect(dlg, SIGNAL(paintRequested(QPrinter *)), this, SLOT(semesterResultsReportPrint(QPrinter *)));
dlg->exec();
}
void SemesterResultsReport::semesterResultsReportPrint(QPrinter *printer) {
QString strStream;
QTextStream out(&strStream);
qDebug() << QCoreApplication::applicationDirPath();
QList<QByteArray> list = QImageReader::supportedImageFormats();
for (int i = 0; i < list.size(); ++i) {
qDebug() << list.at(i);
}
qDebug() << QDir(":/imgs/").entryList();
out << "<!DOCTYPE html>"
<< "<html>n"
<< "<head>"
<< "<title>ff</title>"
<< "<meta http-equiv="Content-Type" content ="text/html;charset=utf-8" >"
<< "<style type="text/css"> "
<< " html, body { margin: 5px; direction: rtl; }"
<< " table { border: 1; border-collapse: collapse; float:right; page-break-after:auto !important; width: 100%; }"
<< " th, td { border: 1px solid #000; padding: 0; align: center; text-align: center; font-family: "Times New Roman"; font-size: 16px; page-break-inside:avoid; page-break-after:auto; }"
<< " tr { page-break-inside:avoid; page-break-after:auto !important; }"
<< " thead { display:table-header-group; }"
<< " tfoot { display:table-footer-group; } "
<< "</style>"
<< "</head>n"
<< "<body>n"
<< "<table>"
<< " <tr>"
<< " <td rowspan="2">" + QString("الشعب") + "</td>"
<< " <td rowspan="2"><img class="vertical-center" src="imgs/registered-students2.png" alt=""></td>"
<< " <td rowspan="2"><img class="vertical-center" src="imgs/registered-students2.png" alt=""></td>"
<< " <td rowspan="2"><img class="vertical-center" src="imgs/num-students-above-avg2.png" alt=""></td>"
<< " <td colspan="2">" + QString("الجنس") + "</td>"
<< " <td colspan="18">" + QString("نسب النجاح") + "</td>"
<< " </tr>"
<< " <tr>"
<< " <td><img class="vertical-center" src="qrc:imgs/sex-male.jpg" alt=""></td>"
<< " <td><img class="vertical-center" src="imgs/sex-male.png" alt=""></td>"
<< " <td><img class="vertical-center" src=":imgs/math.png" alt=""></td>"
<< " <td><img class="vertical-center" src=":imgs/physic.png" alt=""></td>"
<< " <td><img class="vertical-center" src=":imgs/science.png" alt=""></td>"
<< " <td><img class="vertical-center" src=":imgs/arab.png" alt=""></td>"
<< " <td><img class="vertical-center" src=":imgs/frensh.png" alt=""></td>"
<< " <td><img class="vertical-center" src=":imgs/english.png" alt=""></td>"
<< " <td><img class="vertical-center" src=":imgs/3rd-language.png" alt=""></td>"
<< " <td><img class="vertical-center" src=":imgs/physolophie.png" alt=""></td>"
<< " <td><img class="vertical-center" src=":imgs/geographie.png" alt=""></td>"
<< " <td><img class="vertical-center" src=":imgs/islamic.png" alt=""></td>"
<< " <td><img class="vertical-center" src=":imgs/managment.png" alt=""></td>"
<< " <td><img class="vertical-center" src=":imgs/contable.png" alt=""></td>"
<< " <td><img class="vertical-center" src=":imgs/low.png" alt=""></td>"
<< " <td><img class="vertical-center" src=":imgs/mechanic.png" alt=""></td>"
<< " <td><img class="vertical-center" src=":imgs/electricity.png" alt=""></td>"
<< " <td><img class="vertical-center" src=":imgs/civil.png" alt=""></td>"
<< " <td><img class="vertical-center" src=":imgs/route.png" alt=""></td>"
<< " <td><img class="vertical-center" src=":imgs/sport.png" alt=""></td>"
<< " </tr>"
<< "</table>"
<< "</body>"
<< "</html>";
// Just for debugging purposes
QFile file("QWebViewReport.html");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
//MSG(QString("Can`t create file %1").arg(htmlFileName));
}
file.write(strStream.toUtf8());
file.close();/**/
/*
* Prepare QTextDocument
*/
/**/QSizeF paperSize;
paperSize.setWidth(printer->width());
paperSize.setHeight(printer->height());
QTextDocument *document = new QTextDocument();
QTextOption options;
options.setTextDirection(Qt::RightToLeft);
document->setDefaultTextOption(options);
document->setHtml(strStream);
document->setPageSize(paperSize);
//document->print(printer);
/**/QWebView *view = new QWebView;
view->settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, true);
view->settings()->setAttribute(QWebSettings::LocalContentCanAccessFileUrls, true);
view->settings()->setAttribute(QWebSettings::LocalStorageEnabled, true);
view->settings()->setAttribute(QWebSettings::AutoLoadImages, true);
view->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
view->setHtml(strStream);
view->print(printer);
}
qrc
中的所有图像都正确列出,插件加载良好,因为我可以看到png jpeg gif和其他格式。我的应用程序文件夹:
│ arcicalc.edb
│ icudt53.dll
│ icuin53.dll
│ icuuc53.dll
│ Qt5Core.dll
│ Qt5Gui.dll
│ Qt5Network.dll
│ Qt5OpenGL.dll
│ Qt5PrintSupport.dll
│ Qt5QuickWidgets.dll
│ Qt5Sql.dll
│ Qt5WebChannel.dll
│ Qt5WebEngine.dll
│ Qt5WebKit.dll
│ Qt5WebKitWidgets.dll
│ Qt5WebView.dll
│ Qt5Widgets.dll
│ QWebViewReport.html
│ StudentsNotesReporter.exe
│
├───imgs
│
├───platforms
│ qminimal.dll
│ qoffscreen.dll
│ qwindows.dll
│
├───printsupport
│ windowsprintersupport.dll
│
└───sqldrivers
qsqlite.dll
注意:如果我使用QTextDocument
,图像加载正常。
我的编程环境:
Visual studio 2013社区版。
Qt开源windows-x86-msvc2013-5.4.0。
更新:
我试图用qt-opensource-windows-x86-mingw491_opengl-5.4.0.exe
版本编译我的项目,但同样的问题是,QWebView没有显示图像。在Windows XP 32位和Windows 7 32位上进行了测试。
我在您的应用程序目录中没有看到imageformats目录。
将path/to/Qt/plugins/imageformats
目录复制到您的应用程序目录中。如果你不想使用所有的插件,你可以删除它们。