QtWebEngine doesn't support JavascriptCanCloseWindows - qt

In QtWebkit, using QWebSettings class, I could enable like the permission to close the window using the JavaScript command window.close();:
setAttribute(QWebSettings::JavascriptCanCloseWindows, true);
But in QtWebEngine, such an attribute doesn't exist: http://doc.qt.io/qt-5/qwebenginesettings.html#WebAttribute-enum
How to allow JavaScript to close any QWebEngineView using window.close()?

Indeed, this attribute doesn't exist anymore in the Qt WebEngine.
However, you can close any views using the signal windowCloseRequested from your QWebEnginePage, and connecting it to a slot where you close the window. There is an example of use in the Demo Browser example, in the file webview.cpp:
connect(page(), &WebPage::windowCloseRequested, this, &QWidget::close);

Related

How to change WebView settings from QML

I'm using a simple WebView in my QML file.
WebView {
anchors.fill: parent
url: "file:///android_asset/example.html"
}
The problem is, that I'm getting an error about access-control-allow-origin is null. I found a fix for this here, which is using WebView settings property. It seems to be accessible from c++, but I haven't found any way of using this property from qml. So how can I use WebView settings from QML to get rid of the error? I'm using Qt 5.10.
Use Qt's resource system, add the file to it, and load the file via
url: "qrc:///android_assets/example.html"

QWebView Open in new Window

I want to open links from a QWebView with target="_blank" in a new window of a new process.
I tried using QWebView::createWindow(QWebPage::WebWindowType), but I didn't find a way to get the url to send to the new process. Actually, I tried a hack to get the url using a hidden QWebView (returned by the createWindow method) with the urlChanged event, but sometimes this event is triggered twice, so it is not reliable.
Is there any way to get the URL in the createWindow method?
If not, what can I do to open this kind of links in a new window of a new process?
I found a way to do what I want.
I had to overload the mousePressEvent to get the mouse position so that I can get the url from it.
Here is the code:
QWebView* WebView::createWindow(QWebPage::WebWindowType) {
QWebHitTestResult result{page()->mainFrame()->hitTestContent(lastClickPosition)};
openNewWindow(result.linkUrl());
return nullptr;
}
void WebView::mousePressEvent(QMouseEvent* mouseEvent) {
lastClickPosition = mouseEvent->pos();
QWebView::mousePressEvent(mouseEvent);
}
I let this question opened a few days to see if anyone can find a better solution.
If you're trying for a multi-process webview architecture, I would suggest looking at QtWebEngine. QtWebEngine will be replacing QtWebKit and is based on Chromium. Because of this, it has a multi-process architecture by default. QtWebEngine will no longer be updated, so I would suggest migrating to QtWebEngine anyway.
QtWebEngine overview

QFileDialog : how to set option to show content of folder in getExistingDirectory()

I am using QFileDialog as
filename = QFileDialog::getExistingDirectory(this,"Select Image File: ",dataDir,0);
I want that I can check files inside folder before selecting it. function getExistingDirectory() is setting QFileDialog::ShowDirsOnly as a default option. I checked in docs there is no any option that do opposite of this. So I set last parameter 0. But now it is not using native dialog. I want to use native dialog with this. I have no clue how to do this cause no flag found in options for UseNativeDialog. Please help.
Try creating the file dialog on your own, something like:
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::Directory);
dialog.setViewMode(QFileDialog::Detail);
dialog.setDirectory(datadir);
dialog.exec();
The code by Sebastian should create a native dialog, unless you make a line such as:
dialog.setOption(QFileDialog::DontUseNativeDialog, true);
However, I have not been able to get this working under Windows, even though the documentation says that the QFileDialog::Directory option should display files by default. Not only that, but doing:
qDebug() << dir_selector.testOption(QFileDialog::ShowDirsOnly);
displays false on my system, indicating that there is probably a bug somewhere.

WebDriver open rich:popupPanel

We are testing an application with Selenium WebDriver. HTMLUnitDriver is our choice (because of non-gui-testing) and sometimes IEDriver for Presentationpurposes. Anyway, i try to perform a click on a button, this one has to open an rich:popuPanel(modal=true) and click a Link on that Panel. With IEDriver that's no problem, but with HTMLUnitDriver the popupPanel doesn't open. I tried to perform these clicks in several ways:
JavascriptExecutor jsdriver = (JavascriptExecutor) driver;
jsdriver.executeScript("$('input[id$=freigabeCmd_id]').focus();");
//below are the other tries
// jsdriver.executeScript("$('input[id$=freigabeCmd_id]').click();");
// jsdriver.executeScript("window.document.getElementById('editorViewForm_id:freigabeCmd_id').click()");
// jsdriver.executeScript("arguments[0].click()", freigabeButton);
// jsdriver.executeScript("arguments[0].fireEvent('onclick');", freigabeButton);
further i tried it the "normal way":
freigabeButton.click();
//below are other ways i found here on stackoverflow
// freigabeButton.sendKeys(Keys.ENTER);
// new Actions(driver).moveToElement(freigabeButton).clickAndHold().release().build().perform();
but nothing brought me to get the popupPanel "visible". Anyone got an idea why?! i'm really stuck right now. If you need more Informations pls let me know.
using: HTMLUnit Version 2.12 and latest SeleniumVersion
It might be that your webapp uses javascript to launch the rich pop-up panel, and you are running HtmlUnitDriver with javascript disabled? It is disabled by default, so you need to explicitly enable it.

How to make commands of the inactive/hidden “Tool Window” accessible?

I have one VSPackage with some commands registered in the Initialize() function and are accessible globally using toolbar/menu/shortcut keys. This VSPackage also has one Tool Window (derived from ToolWindowPane) similar to Solution Explorer with its commands registered in OnCreate(). Tool Window commands are also working fine via toolbar/menu/shortcut keys when Tool Window is active/in focus. But when this is hidden behind another window (e.g. Solution Explorer), tool window commands are not accessible.
Please let me know how to make inactive tool window commands accessible.
I found the solution:
Command handler should be first added to the main Package and then to the ToolWindow otherwise ToolWindow command will work only when it has focus.
Please refer function DefineCommandHandler() in PersistedWindowPane.cs and PackageToolWindow.cs of VSSDK IDE Sample.

Resources