From pyQt5 to pyQt6

Hello guys, I am a newbie in programming and trying to fix my code.
I save, and export into an executable I get an error that certain things are deprecated and when I am trying to fix it it’s even worse. So that’s my stable version so far. I mention that I used and chat GPT but gives me headaches with his own loops of breaking the code. I am not looking for someone to fix my entire source but to point me in the right direction.
All I am trying is to recreate a simple browser, nothing special just functional for any website that I visit.
Here is the source:

import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QLineEdit, QTabWidget, QToolBar, QPushButton
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtGui import QAction, QIcon


class BrowserWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Simple Browser")
        self.setWindowIcon(QIcon("icon.png"))

        self.create_actions()
        self.create_menus()
        self.create_toolbar()

        self.tab_widget = QTabWidget()
        self.tab_widget.setDocumentMode(True)
        self.tab_widget.setTabsClosable(True)
        self.tab_widget.tabCloseRequested.connect(self.close_tab)
        self.tab_widget.currentChanged.connect(self.update_tab)

        self.setCentralWidget(self.tab_widget)

        self.add_tab()

    def create_actions(self):
        self.new_tab_action = QAction(QIcon("tab-new.png"), "New Tab", self)
        self.new_tab_action.setShortcut("Ctrl+T")
        self.new_tab_action.triggered.connect(self.add_tab)

        self.close_tab_action = QAction(QIcon("window-close.png"), "Close Tab", self)
        self.close_tab_action.setShortcut("Ctrl+W")
        self.close_tab_action.triggered.connect(self.close_current_tab)

        self.exit_action = QAction("Exit", self)
        self.exit_action.setShortcut("Ctrl+Q")
        self.exit_action.triggered.connect(self.close)

    def create_menus(self):
        self.file_menu = self.menuBar().addMenu("File")
        self.file_menu.addAction(self.new_tab_action)
        self.file_menu.addAction(self.close_tab_action)
        self.file_menu.addSeparator()
        self.file_menu.addAction(self.exit_action)

    def create_toolbar(self):
        self.toolbar = QToolBar()
        self.addToolBar(self.toolbar)

        self.url_input = QLineEdit()
        self.url_input.returnPressed.connect(self.load_current_tab)

        self.go_button = QPushButton("Go")
        self.go_button.clicked.connect(self.load_current_tab)

        self.toolbar.addWidget(self.url_input)
        self.toolbar.addWidget(self.go_button)

    def add_tab(self):
        web_view = QWebEngineView()
        web_view.load(QUrl("https://duckduckgo.com/"))

        layout = QVBoxLayout()
        layout.addWidget(web_view)

        tab_widget = QWidget()
        tab_widget.setLayout(layout)

        self.tab_widget.addTab(tab_widget, "New Tab")
        self.tab_widget.setCurrentWidget(tab_widget)

    def load_url(self, web_view, url):
        if not url.startswith("http://") and not url.startswith("https://"):
            url = "https://" + url
        if not url.startswith("www."):
            url = url.replace("://", "://www.", 1)
        web_view.setUrl(QUrl(url))
        self.url_input.setText(url)

    def load_current_tab(self):
        current_widget = self.tab_widget.currentWidget()
        web_view = current_widget.findChild(QWebEngineView)
        url = self.url_input.text()
        self.load_url(web_view, url)

    def close_tab(self, index):
        self.tab_widget.removeTab(index)

    def close_current_tab(self):
        current_index = self.tab_widget.currentIndex()
        self.tab_widget.removeTab(current_index)

    def update_tab(self, index):
        tab_widget = self.tab_widget.currentWidget()
        if tab_widget:
            web_view = tab_widget.findChild(QWebEngineView)
            if web_view:
                self.setWindowTitle(web_view.page().title())
                self.setWindowIcon(web_view.page().icon())

    def closeEvent(self, event):
        if self.tab_widget.count() > 1:
            event.accept()
        else:
            event.ignore()


if __name__ == "__main__":
    app = QApplication(sys.argv)

    window = BrowserWindow()
    window.show()

    sys.exit(app.exec())

Various articles like this one go over the PyQt5 / 6 differences.

I’d suggest simply working through the points in your code matching points mentioned in the article.

One particular bit of advice seems likely to make things easier for you: as the enum changes work in PyQt5 too, see about making those changes in your PyQt5 code before beginning the upgrade progress. If you can get it running under PyQt5 with those changes, you’ll have far fewer remaining things left for the step to PyQt6.

Hope that helps. Best of luck with it!

1 Like

Thank you.

1 Like