简体   繁体   English

PyQt6 QWebEngineView 添加白框

[英]PyQt6 QWebEngineView adding white box

When I add a QWebEngineView as an attribute of my QMainWindow, it adds this white box that shows the context menu when right-clicked.当我将 QWebEngineView 添加为我的 QMainWindow 的属性时,它会添加这个白色框,在右键单击时显示上下文菜单。 I do not want this white box in my program.我不想在我的程序中出现这个白框。

图片链接

I do not actually use this QWebEngineView attribute for anything.我实际上并没有将这个 QWebEngineView 属性用于任何事情。 My program creates tabs that show outputs from plotly and each tab gets it own instance of QWebEngineView.我的程序创建了显示plotly输出的选项卡,每个选项卡都有自己的 QWebEngineView 实例。

The simple solution would be to just remove QWebEngineView from my QMainWindow class and only add it to the tabs, but when I do that the window will close and open again the first time I add a tab.简单的解决方案是从我的 QMainWindow 类中删除 QWebEngineView 并将其添加到选项卡,但是当我这样做时,窗口将关闭并在我第一次添加选项卡时再次打开。 Not really sure what to make of that.不太确定该怎么做。

How can I remove this white box from my QMainWindow;我怎样才能从我的 QMainWindow 中删除这个白框; or alternatively, how can I keep my program from closing and restarting the first time I open a tab and implement QWebEngineView?或者,如何防止我的程序在我第一次打开选项卡并实现 QWebEngineView 时关闭并重新启动?

I've tried searching for the name of this element so I could figure out how to disable it but I was unsuccessful.我曾尝试搜索此元素的名称,以便弄清楚如何禁用它,但我没有成功。

Edit: Below is a minimal reproducible example.编辑:下面是一个最小的可重现示例。 Comment out the QWebEngineView line in __init__ to remove the white box and see the window restart mentioned above.注释掉__init__中的QWebEngineView这一行,去掉白框,看到上面提到的窗口重启。

from PyQt6.QtWidgets import *
from PyQt6 import QtGui, QtCore, QtWebEngineWidgets
import sys


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

        # --------------
        # central widget
        # --------------
        self.layout = QGridLayout()
        container   = QWidget()
        container.setLayout(self.layout)
        self.setCentralWidget(container)

        # --------------
        # webengine view
        # --------------
        QtWebEngineWidgets.QWebEngineView(self)

        # -----------------------
        # push button to add tabs
        # -----------------------
        btn = QPushButton('Add Tab')
        btn.released.connect(self.addTab)
        self.layout.addWidget(btn)

        # ----------
        # tab widget
        # ----------
        self.tabCount = 0
        self.tabs = QTabWidget()
        self.tabs.setTabsClosable(True)
        self.layout.addWidget(self.tabs)

    def addTab(self):
        # -------------
        # main elements
        # -------------
        layout = QVBoxLayout()
        container = QWidget()
        container.setLayout(layout)

        # ---------
        # add label
        # ---------
        tabBrowser = QtWebEngineWidgets.QWebEngineView(self)
        txt = '''
        <html>
        Hello World!
        </html>
        '''
        tabBrowser.setHtml(txt)
        layout.addWidget(tabBrowser)

        # -------
        # add tab
        # -------
        self.tabs.addTab(container, str(self.tabCount))
        self.tabCount += 1


if __name__ == '__main__':

    # -----------------
    # exception handler
    # -----------------
    def exception_hook(exc_type, exc_value, tb):
        sys.__excepthook__(exc_type, exc_value, tb)
        main.close()
        sys.exit(1)
    sys.excepthook = exception_hook

    # -----------
    # run program
    # -----------
    app = QApplication(sys.argv)
    main = Main()
    main.show()
    sys.exit(app.exec())

I found a workaround.我找到了解决方法。 Assign the QWebEngineView as an attribute of self , then set the maximum size to (0, 0) like this:QWebEngineView分配为self的属性,然后将最大尺寸设置为(0, 0)如下所示:

from PyQt6.QtWidgets import *
from PyQt6 import QtGui, QtCore, QtWebEngineWidgets
import sys


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

        # --------------
        # central widget
        # --------------
        self.layout = QGridLayout()
        container   = QWidget()
        container.setLayout(self.layout)
        self.setCentralWidget(container)

        # --------------
        # webengine view
        # --------------
        self.browser = QtWebEngineWidgets.QWebEngineView(self)
        self.browser.setMaximumSize(0, 0)

        # -----------------------
        # push button to add tabs
        # -----------------------
        btn = QPushButton('Add Tab')
        btn.released.connect(self.addTab)
        self.layout.addWidget(btn)

        # ----------
        # tab widget
        # ----------
        self.tabCount = 0
        self.tabs = QTabWidget()
        self.tabs.setTabsClosable(True)
        self.layout.addWidget(self.tabs)

    def addTab(self):
        # -------------
        # main elements
        # -------------
        layout = QVBoxLayout()
        container = QWidget()
        container.setLayout(layout)

        # ---------
        # add label
        # ---------
        tabBrowser = QtWebEngineWidgets.QWebEngineView(self)
        txt = '''
        <html>
        Hello World!
        </html>
        '''
        tabBrowser.setHtml(txt)
        layout.addWidget(tabBrowser)

        # -------
        # add tab
        # -------
        self.tabs.addTab(container, str(self.tabCount))
        self.tabCount += 1


if __name__ == '__main__':

    # -----------------
    # exception handler
    # -----------------
    def exception_hook(exc_type, exc_value, tb):
        sys.__excepthook__(exc_type, exc_value, tb)
        main.close()
        sys.exit(1)
    sys.excepthook = exception_hook

    # -----------
    # run program
    # -----------
    app = QApplication(sys.argv)
    main = Main()
    main.show()
    sys.exit(app.exec())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM