我在使用QWebView
時遇到了顯示圖像(png格式)的問題,我的主要目的是將它用作報告生成器。我已閱讀其他建議,如在應用程序文件夾下放置imageformats
,爲圖像設置完整路徑,從資源加載圖像,驗證我的html
符合W3C標準,並最終將我的應用程序測試到另一臺清潔機器,以查看問題是否僅出自我的機器。QWebView不顯示png圖像
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
圖像,插件加載罰款,我可以看到JPEG,PNG和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-opensource windows-x86-msvc2013-5.4.0。
UPDATE:
我試圖編譯我的項目,qt-opensource-windows-x86-mingw491_opengl-5.4.0.exe
版本,它是同一個問題,沒有與QWebView顯示的圖像。 在Windows XP 32位和Windows 7 32位上進行測試。
把它添加爲'imageformats',和'plugins/imageformats'都沒有顯示圖像。如果我沒有錯,我想png與Qt本地支持。 – 2015-02-09 23:46:50