How to open a link in a default user browser in Qt? - c++

I wonder how to open a link in a default user browser using Qt (that would open it across all platforms (Win Mac Lin))?

In the doc: QDesktopServices
http://doc.qt.io/qt-4.8/qdesktopservices.html#openUrl
bool QDesktopServices::openUrl ( const QUrl & url ) [static]
Opens the given url in the appropriate Web browser for the user's desktop environment, and returns true if successful; otherwise returns false.

You can try this code
QString link = "http://www.google.com";
QDesktopServices::openUrl(QUrl(link));
Read QDesktopServices and QUrl to get further information.

you are looking for openUrl() in the desktop services class
http://qt-project.org/doc/qt-4.8/QDesktopServices.html

Related

How to create a session for Run Dialog in Appium?

By using powershell command I can get the run dialog program id, which is Microsoft.Windows.Shell.RunDialog. However, I can't get this working with the code below. Any idea?
DesiredCapabilities desktopCapabilities = new DesiredCapabilities();
desktopCapabilities.SetCapability("app", "Microsoft.Windows.Shell.RunDialog");
desktopCapabilities.SetCapability("deviceName", "WindowsPC");
desktopCapabilities.SetCapability("platformName", "Windows");
session = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), desktopCapabilities);
Take a look at the answers from this post about the run dialog. My best guess is that winappdriver is calling rundll32.exe and that's not the dialog itself.
You could try your luck changing this line
desktopCapabilities.SetCapability("app", "Microsoft.Windows.Shell.RunDialog");
into
desktopCapabilities.SetCapability("app", "c:\windows\system32\rundll32.exe shell32.dll,#61");
Alternatively, you could get the desktop session and send "windows key + r" to it. Here is how you can get the desktop session.

Link clicked signal QWebEngineView

We already have QWebView implementation and now we want to migrate to QWebEngineView.
Through QWebView we have registered below signal to receive notification for any link is clicked on webview or not and we are getting signal in QWebView.
connect(m_WebView, SIGNAL(linkClicked(const QUrl &)),SLOT(urlLinkClicked(const QUrl &)));
In "urlLinkClicked" slot, we are opening new tab and open that URL into new tab.
We are facing some issue with QWebEngineView. As there is no such signal "linkClicked" exist in QWebEngineView. So we have tried below options but still not able to find the solution.
In main class, we have created WebEngineView class instance and setting WebEnginePage. We are able to render the website in view class but when we click on any link then we are not getting any signal so we are not able to open that new website in new tab.
m_WebEngineView = new QWebEngineView(this);
m_WebEngineView->setPage(new QWebEnginePage());
We have also override "acceptNavigationRequest" method to get the link clicked event in mainWebEngineView but we are not able to get the link clicked event.
Any suggestion i can try ?
Thanks in Advance.
Override QWebEnginePage::acceptNavigationRequest in QWebEnginePage subclass:
bool MyWebPage::acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame)
{
if (type == QWebEnginePage::NavigationTypeLinkClicked)
{
qDebug() << url;
}
return true;
}
use this in inherited WebEngineView and in inherited webpage we can find hoverdUrl
QWebEngineView *WebEngineView::createWindow(QWebEnginePage::WebWindowType type)
{
if(type==QWebEnginePage::WebBrowserTab)
{
if(!hoverdUrl.isEmpty())
QDesktopServices::openUrl(QUrl(hoverdUrl));
qGlobalDbg("Open external Url requiested in chat, url : " + hoverdUrl, toKIBANA|toLOG);
}
//qDebug()<<"============== link Clicked "<<hoverdUrl;
return NULL;
}
I think you could use the signal "urlChanged".
See the header "Signals" on there official documentation page.
http://doc.qt.io/qt-5/qwebengineview.html
If this does not help I need to know what version of the Qt framework you are using.
Best Regards
/ Rasmus
Unfortunately urlChanged signal that QWebenginePage has emits only when url of current page is changed. Previous linkClicked signal was also emitted when url of current page was not changed. There is a way to handle this, but you would need to have access to page source code.
This functiionality is achieved through QWebChannel class. You would need to create webChannel object and a special callback class, which would handle call backs from webpage in a way you like. Then you would need to set this webChanell on a page you want to and do all the connections like this:
MyCallBackObject* callback= new MyCallBackClass();
mWebChannel = new QWebChannel(this);
mWebChannel->registerObject(QStringLiteral("MyCallBackObject"), callback);
mWebView->page()->setWebChannel(mWebChannel); // mWebView is QWebEngineView
connect(callback, SIGNAL(urlChanged(QUrl)),
this, SLOT(linkClickedSlot(QUrl))); // connect statement, urlChanged is defined in your callback class, linkClickedSlot is a slot where you process clicked signal
For more reference - please use official example from qt or the one from kdab or this video from Qt Developer Days conference

Dragging an image from Chrome to a Qt application

I am trying to get the data from an image dragged directly from a browser to my Qt app. I have the following code:
void MyView::dropEvent( QDropEvent* event )
{
QGraphicsView::dropEvent( event );
if ( event->mimeData()->hasImage() )
{
QImage image = qvariant_cast<QImage>( event->mimeData()->imageData() );
...
This works fine with Firefox (Windows/Mac), Safari (Mac) and IE (Windows). But QMimeData::hasUrl() returns false for Chrome on both Windows and Mac.
On further investigation Qt expects image data in MIME format "application/x-qt-image". It seems that Chrome doesn't provide this. Is there a workaround for Chrome? I haven't been able to find anything.

QWebEngine: print a page?

The migration from QWebKit to QWebEngine seems to be much more complicated than Qt guys claimed. With QWebKit I could print a webpage easily via
QWebView->print(&printer);
With QWebEngine class QWebEngine view does not provide a method print(). Their browser example uses a class named QWebEngineFrame which offers a method print() - but the whole QWebEngineFrame is not defined anywhere!
So my question: how do I print a page using QWebEngine?
I think the correct way to use QWebEngineView::render method because QWebEngineView is a QWidget. It accepts paint device as a first argument and you may pass QPrinter there for printing.
Update: If you can use the latest version of Qt, in Qt 5.8 a new function for printing page was added:
void QWebEnginePage::print(QPrinter *printer, FunctorOrLambda resultCallback);
Actually it first prints to temp PDF with QPrinter settings.
Here is the link to Qt docs.
You can read about this in our blog also.
I would offer following code (for a while):
QTextEdit *textEdit = new QTextEdit;
ui.myWebView->page()->toHtml([textEdit](const QString &result){ textEdit->setHtml(result); });
textEdit->print(somerinter);
textEdit->deleteLater();
Qt 5.7 includes print support in to pdf files for QWebEngine.
Use printToPdf function to export the current page in a pdf file. Example:
const QString fileName = QFileDialog::getSaveFileName(0,
tr("Save pdf"),
".",
tr("PDF Files (*.pdf)"));
if (fileName.isEmpty()) {
return;
}
ui->webView->page()->printToPdf(fileName);
QWebView->page()->print(&printer, [=](bool){});

Open URL with ShellExecute - SW_SHOWMAXIMIZED dont active window in C++

I used this function to open new tab in Chrome and active it:
ShellExecuteA(0,0,"chrome.exe","http://google.com --incognito",0,SW_SHOWMAXIMIZED);
but Chrome only open new tab but it doesnt active window.
(I call this function from global keyboard-hook of an application with no user interface, if user press specified key).
How I can fix it?
Looks like a bug in chrome.exe. I could repro with your ShellExecute call from a simple console app, if a regular (non-incognito) chrome.exe session was running and no incognito session was running. In other words, if a new incognito chrome session needed to be spawned, the regular session did not appear to correctly propagate the ShowWindow flags to the spawned incognito process. Another factor was that the activation failure also required the regular chrome session to be active before the test app ran. If any other app was active (say notepad.exe), then activation of the incognito session succeeded. The same behavior occurs with ShellExecuteEx and CreateProcess. Observing in Process Explorer (from sysinternals), it's clear that chrome.exe is forking the child process as necessary and then terminating itself. This makes it difficult to intercept an hProcess or processId in order to ultimately call SetActiveWindow.
Its not possible. You have to make Google Chrome as default browser and than try this:
(only tested on WinXP using
IE6)
first a function, that finds the default app for any File extension:**
enter code here
#include<Registry.hpp>
AnsiString GetDefaultApp(AnsiString ext)
{
TRegistry* reg = new(TRegistry);
reg->RootKey = HKEY_CURRENT_USER;
if(!reg->OpenKeyReadOnly("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\."+ext+"\\OpenWithList"))
return(NULL);
try
{
AnsiString MRUList = reg->ReadString("MRUList");
AnsiString ret = reg->ReadString(AnsiString(char(MRUList[1])));
return(ret);
}
catch(...)
{
return(NULL);
}
}
now the code to launch the default app for html files and giving
the URL as parameter:**
#include<shellapi>
void OpenURL(AnsiString URL)
{
AnsiString app = GetDefaultApp("html");
if(app == NULL)
return;
ShellExecute(NULL,"open",app.c_str(),URL.c_str(),NULL,SW_SHOWDEFAULT);
}
Now you can open a URL in a new Browser using this command:
OpenURL("http://www.AlgorithMan.de/");