Incorrect warning MSB8027 reported for files excluded from build
vs2013更新到update5,或者
文本编辑器打开project插入: IgnoreWarnCompileDuplicatedFilename
…
true
…
evaluateJavaScript
=> runJavaScript
设置QWebEngineView背景色
=> page()->setBackgroundColor(QColor(“#0f0f10”));
Qt WebEngine ICU data not found The application MAY NOT work. Installed Qt WebEngine locales directory not found at location
=> 增加resources和translations目录,具体放的位置可以看日志哪里可以找到它,可以通过qt.conf文件来配置所在路径
QWebEngineView调试
方法1:html中加入https://getfirebug.com/firebug-lite.js, 在页面上按F12打开调试面板
缺点:这个js文件比较大,载入的时候影响效率;功能有限;
方法2:在程序开始的时候qputenv(“QTWEBENGINE_REMOTE_DEBUGGING”, 9000),之后在chrome中打开网址:localhost:9000
就可以看到QWebEngineView加载的html了,点击相应的html可以打开chrome的开发者工具;
这个方法比上面的好得多。
linkClicked(QUrl)
重载QWebEnginePage实现acceptNavigationRequest接口
bool WebPage::acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame)
{
if (isMainFrame) {
if (NavigationTypeLinkClicked == type) {
emit sigLoadUrl(url);
return false;
}
}
return true;
}
QPair<bool, QVariant> syncRunJavaScript(QWebEnginePage *page, const QString &javascript, int msec)
{
QPair<bool, QVariant> result = qMakePair(false, 0);
QSharedPointer loop = QSharedPointer(new QEventLoop());
QTimer::singleShot(msec, loop.data(), &QEventLoop::quit);
page->runJavaScript(javascript, [loop, &result](const QVariant &val) {
if (loop->isRunning()) {
result.first = true;
result.second = val;
loop->quit();
}
});
loop->exec();
return result;
}
在QWebEngineView重载函数contextMenuEvent中不能调用!
runJavaScript回调中不能进行长时间的操作,否则会阻塞JavaScript代码执行,如:
page()->runJavaScript("script", [](const QVariant &val) {
// ...
menu.exec(QCursor::pos());
}
解决办法,定义信号和槽函数,使用QueuedConnection的方式connect,在slotJavaScriptResult中处理耗时操作。
connect(this, &webview::sigJavaScriptResult, this, &webview::slotJavaScriptResult, Qt::QueuedConnection);
void sigJavaScriptResult(const QString &command, const QVariantMap &result);
void slotJavaScriptResult(const QString &command, const QVariantMap &result);
connect(this->page(), SIGNAL(loadFinished(bool)), this, SLOT(finish(bool)));
window.cppobj = null;
new QWebChannel(qt.webChannelTransport, function(channel) {
window.cppobj = channel.objects.cppobj;
cppobj.init(); // 通知C++初始化完成
});
web页面,右键鼠标点击的元素
function contextMenu(e) {
var targ;
if (!e) {
var e = window.event;
}
if (e.target) {
targ = e.target;
} else if (e.srcElement) {
targ = e.srcElement;
}
}
function getHTMLOfSelection () {
var range;
if (document.selection && document.selection.createRange) {
range = document.selection.createRange();
return range.htmlText;
}
else if (window.getSelection) {
var selection = window.getSelection();
if (selection.rangeCount > 0) {
range = selection.getRangeAt(0);
var clonedSelection = range.cloneContents();
var div = document.createElement('div');
div.appendChild(clonedSelection);
return div.innerHTML;
}
else {
return '';
}
}
else {
return '';
}
}
之前setAcceptDrops(true);就可以了,现在还需要:
void webview::dragEnterEvent(QDragEnterEvent *event)
{
event->accept();
QWebEngineView::dragEnterEvent(event);
}
才能触发void webview::dropEvent(QDropEvent * event)