0

So, I've been working with PyQt5 and their implementation of chromium to make my own web browser in python. But, I've come across an issue, after adding a tabs system and a settings menu, some of the functions of the right click menu don't work. For instance, I can't open a link in a new tab or window, I can't view a page source, I can't save a page, I can't save a link, and I can't save a file. Although, the actual browser functions like going to a different page from the right click menu does work. I want to try and make it so at least I can open a link in a new tab or window and I can view the page source So, that is the issue.

At the start of defining my web browsers class I have a line.

self.setContextMenuPolicy(QtCore.Qt.NoContextMenu)

But, it doesn't seem to do anything for the program at all, even when removed.

This is the class.

#Main window.
class MainWindow(QMainWindow):
    #Constructor.
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.browser = QWebEngineView()
        self.browser.setUrl(QUrl(HOMEPAGE))
        self.setCentralWidget(self.browser)
        self.setWindowIcon(QtGui.QIcon("Logo.png"))
        self.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
        self.showMaximized()

The way I add tabs is through this function.

    #Method for adding new tab.
    def add_new_tab(self, qurl = None, label ="Blank"):
        #If url is blank.
        if qurl is None:
            #Creating a url to be the default one.
            qurl = QUrl(self.homepage)
 
        #Creating a QWebEngineView object.
        browser = QWebEngineView()
 
        #Setting url to browser.
        browser.setUrl(QUrl(self.homepage))
 
        #Setting tab index.
        i = self.tabs.addTab(browser, label)
        self.tabs.setCurrentIndex(i)

        #Update the url
        browser.urlChanged.connect(lambda qurl, browser = browser:
                                   self.update_urlbar(qurl, browser))

        #Set the tab title.
        browser.loadFinished.connect(lambda _, i = i, browser = browser:
                                     self.tabs.setTabText(i, browser.page().title()))

        #Update the tab's icon.
        self.tabs.setTabIcon(i, browser.icon())
        browser.iconChanged.connect(lambda icon, browser=browser: self.update_icon(browser, icon))

        #Making the page more readable while having a closer up view of it.
        #browser.setZoomFactor(1.5)

Also, I use this function to change the page of a tab.

    #When tab is changed.
    def current_tab_changed(self, i):
        #Get the current url.
        qurl = self.tabs.currentWidget().url()
 
        #Update that url.
        self.update_urlbar(qurl, self.tabs.currentWidget())
 
        #Update that title.
        self.update_title(self.tabs.currentWidget())

I have tried many solutions to getting the menus to work, for instance trying to change the menu, or trying to have functions that do those tasks but nothing has worked at all. Any help to fix this issue would be appreciated.

Viridian
  • 25
  • 3
  • The view cannot create new widgets on its own. Where should be put the new view? In a tab? In a new window? Qt cannot know that, because *you* must tell it. Override [`createWindow()`](https://doc.qt.io/qt-5/qwebenginepage.html#createWindow). – musicamante Feb 02 '23 at 19:57
  • Yeah, but with other programs it works just fine, so why is that, and how can I make it work on mine. That's what I'm wondering. – Viridian Feb 02 '23 at 20:40
  • Also, there are other functions in the menu that aren't working either. – Viridian Feb 02 '23 at 20:42
  • I don't know what "other programs" you're referring to, I can only tell you how the QtWebEngine works, and if you want to support internal requests to create new windows (via menu, caused by javascript or the `target` HTML attribute), including showing the source, you **must** implement `createWindow()`. For anything else, [study the documentation](https://doc.qt.io/qt-5/qwebenginepage.html). – musicamante Feb 02 '23 at 21:04
  • Okay well I mean I've tested out other pyqt5 browsers and looked at the source code of them but I can't see what is different with them compared to mine. So, what would I do though for this create window function, would I just make a function that changes it or something else. – Viridian Feb 02 '23 at 21:29
  • Read the answer in the post for which yours has been marked as duplicate, study the code it provides and apply it to your needs. – musicamante Feb 02 '23 at 21:33
  • But, I still don't get what changes I have to make and where, where can I put that function that will fix the menu. – Viridian Feb 03 '23 at 17:22

0 Answers0