Alex TheWebGroup
Alex TheWebGroup

Reputation: 175

PyQt5 add text over browser page

I want to make a simple browser, but have an area (label) where I display the time and date over the actual webpage that is loaded, in a corner.

I currently have the text displayed in the statusbar, but I would prefer to get rid of it, and have the text constantly displayed over the browser.

Here is my code which works fine, I just can't figure out how to add text permanently on top of the browser/page currently displayed. See attached image:

class WebEnginePage(QWebEnginePage):
    def createWindow(self, _type):
        page = WebEnginePage(self)
        page.urlChanged.connect(self.on_url_changed)
        return page

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.browser = QWebEngineView()
        self.browser.setContextMenuPolicy(Qt.PreventContextMenu)
        page = WebEnginePage(self.browser)
        self.browser.setPage(page)
        self.setCentralWidget(self.browser)
        self.showMaximized()
        self.date = QDate.currentDate()
        self.browser.load(QUrl("http://stackoverflow.com"))

        timer = QTimer(self)
        timer.timeout.connect(self.showTime)
        timer.start(100)

        font =  QFont('Arial', 16, QFont.Bold)
        self.statusBar().setFont(font)

        self.show()

    def showTime(self):
        current_time = QTime.currentTime()
        label_time = current_time.toString('hh:mm')
        self.statusBar().showMessage('Time: ' + label_time + ' || Date: ' + self.date.toString('dd.MM.yyyy'))
           
def main():
    app = QApplication(sys.argv)
    QApplication.setApplicationName('TEST')
    window = MainWindow()
    app.exec_()

if __name__ == '__main__':
    sys.exit(main())

enter image description here

Upvotes: 0

Views: 351

Answers (1)

eyllanesc
eyllanesc

Reputation: 243993

If you want to place a widget on the QWebEngineView then a possible solution is to make it a child of the window and use raise so that it is on top:

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.browser = QWebEngineView()
        self.browser.setContextMenuPolicy(Qt.PreventContextMenu)
        page = WebEnginePage(self.browser)
        self.browser.setPage(page)
        self.setCentralWidget(self.browser)
        self.showMaximized()
        self.browser.load(QUrl("http://stackoverflow.com"))

        timer = QTimer(self)
        timer.timeout.connect(self.showTime)
        timer.start(100)

        self.label = QLabel(self)
        self.label.setStyleSheet("QLabel {color : black; }")
        self.label.move(10, 10)
        self.label.show()
        self.label.raise_()
        font = QFont("Arial", 16, QFont.Bold)
        self.label.setFont(font)

        self.show()

    def showTime(self):
        self.label.setText(
            "Time: "
            + QTime.currentTime().toString("hh:mm")
            + " || Date: "
            + QDate.currentDate().toString("dd.MM.yyyy")
        )
        self.label.adjustSize()

Upvotes: 2

Related Questions