QTabWidget中添加QWebEngineView控件后QTabWidget无法切换怎么办
时间: 2024-03-09 10:44:20 浏览: 57
可能是因为QWebEngineView控件会捕获鼠标事件,导致QTabWidget无法响应切换事件。可以尝试在QWebEngineView控件中重写mousePressEvent和mouseReleaseEvent方法,将事件传递给父控件QTabWidget。代码示例如下:
```
class MyWebEngineView : public QWebEngineView
{
public:
MyWebEngineView(QWidget *parent = nullptr) : QWebEngineView(parent) {}
protected:
void mousePressEvent(QMouseEvent *event) override
{
QWebEngineView::mousePressEvent(event);
if (parentWidget())
parentWidget()->mousePressEvent(event);
}
void mouseReleaseEvent(QMouseEvent *event) override
{
QWebEngineView::mouseReleaseEvent(event);
if (parentWidget())
parentWidget()->mouseReleaseEvent(event);
}
};
```
然后在添加QWebEngineView控件时,使用这个自定义的MyWebEngineView控件代替原来的QWebEngineView控件即可。
```
QTabWidget *tabWidget = new QTabWidget(this);
MyWebEngineView *webView = new MyWebEngineView(tabWidget);
tabWidget->addTab(webView, tr("Tab"));
```
阅读全文