Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a URL in a certain browser tab/window

From within my Qt application, I would like to open URLs repeatedly in the same browser tab/window. (Kind of "refreshing" this tab programmatically)

Using

QDesktopServices::openUrl(QUrl("http://www.domain.tld"));

opens a new tab/window for every call. Is there a possibility to add a "target=" parameter somewhere?

like image 948
Elwood Avatar asked Nov 02 '12 14:11

Elwood


People also ask

How do I open a link in a separate browser tab?

How to Open Hyperlinks in a New Browser Tab or Window. The short answer is: just add a target="_blank" attribute to your links (anchor tags). Now when your visitors click that link, it will open in a new window or tab (depending on which web browser they are using and how they configured that browser).

How can you open a link in a new tab browser window?

To open a link in a new tab, click the link by pressing down your middle mouse button, or right-click the link and select Open link in New Tab. If your mouse has a wheel, it can be used as a button if you press down on the wheel. These methods work in all of the major Internet browsers available for Microsoft Windows.

How do you force open a link in the current window tab?

Click General tab -> Tabs -> Settings. Choose "When a pop-up is encountered"-> Always open pop up in new tab option.


1 Answers

What you are asking for is impossible to do in the way you imagine it. openUrl() uses the operating system to specify the program to open the argument as mentioned in its documentation.

There might be some workarounds, but none of them will work well, or work on all browsers. It's just that this kind of fine-grained control is likely to be impossible for you.

If you want control of a tab in a browser, you could find the window represented by that tab and close it right before opening the new one. This solution is kind of hacky.

Another hacky solution is to find the HWND of the edit box holding the URL, and to try changing its text using SendMessage(). This won't work on Chrome, however, as it does not use a separate control for the URL window. It might work on Firefox or IE.

The better solution is to make your own web browser you control using the Qt WebKit. It is pretty easy to render a page in it and change the url viewed. The QWebView is an easy to use implementation of the QtWebKit.

like image 142
phyatt Avatar answered Nov 02 '22 00:11

phyatt