0

I am trying to open a Url in a QWebEngineView window when a QTableWidget cell is clicked.

The code for opening the QWebEngineView works from 'main' but somehow it doesn't work when called from the subclass of QWidget. A window appears for a split second and then disappears. No error is shown on the console. Any suggestion will be much appreciated.

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.layout = QVBoxLayout()
        self.Aplayers = [[14, 134, 13], [11, 144, 13]]
        self.tableWidget = QTableWidget(len(self.Aplayers), 3)
        self.setWindowTitle('Game Stats')
        self.setGeometry(0, 0, 600, 400)
        self.layout.addWidget(self.tableWidget)
        self.setLayout(self.layout)
        self.setData(self.Aplayers, self.tableWidget)
        self.tableWidget.itemClicked.connect(self.updateUiCellClick)
        self.show()


    def setData(self, players, tbl: QTableWidget):
        index = -1
        for row in players:
            index += 1
            tbl.setItem(index, 0, QTableWidgetItem(str(row[1])))
            tbl.setItem(index, 1, QTableWidgetItem(str(row[2])))
            tbl.setItem(index, 2, QTableWidgetItem(str(row[0])))

    def updateUiCellClick(self, row):
        web = QWebEngineView()
        web.load(QUrl('https://www.google.com'))
        web.show()


if __name__ == '__main__':
    app = None
    app = QApplication(sys.argv)
    mainWidget = App()
    sys.exit(app.exec_())

1 Answers1

1

The QWebEngineView is only available in the local scope of that function, so it disappears immediately after the function returns. If you call it self.web instead it will work, since the application will retain a pointer to the web engine view.

def updateUiCellClick(self, row):
    self.web = QWebEngineView()
    self.web.load(QUrl('https://www.google.com'))
    self.web.show()
alec
  • 5,799
  • 1
  • 7
  • 20