gpt4 book ai didi

c++ - Qt:在默认浏览器中打开带有目标的链接,不会泄漏内存

转载 作者:可可西里 更新时间:2023-11-01 18:37:00 26 4
gpt4 key购买 nike

通过 Internet 搜索,我遇到了很多使用 QWebView 和打开 URL 来做各种事情的方法,其中大部分是非功能性的、非特定的或部分功能性的。

经过多次咒骂和诅咒,我设法得到一个例子来做我想做的事,正常打开普通链接,并在外部浏览器中打开任何请求新窗口的东西;但是,有一个障碍。它会泄漏内存,因为我做了一堆额外的 WebViews,直到进程退出才被清理。如何在不泄漏内存的情况下执行此操作?

请提前原谅我对Qt的浅薄理解。目前我只使用了几个小时。

中南合作:

测试.hpp

#include <QMainWindow>
#include <QWebView>

class Window : public QMainWindow {
Q_OBJECT

public:
Window();

private:
QWebView* m_web;

private slots:
};

class WebPage : public QWebPage {
Q_OBJECT

public:
bool acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type);
};

class WebView : public QWebView {
Q_OBJECT

public:
QWebView* createWindow(QWebPage::WebWindowType type);
};

测试.cpp

#include <QApplication>
#include <QGridLayout>
#include <QNetworkRequest>
#include <QDesktopServices>

#include "test.hpp"

Window::Window() :
QMainWindow() {
m_web = new WebView;
m_web->setHtml("<div align=\"center\"><a href=\"http://www.google.com/\">Same Window</a> <a href=\"http://www.google.com/\" target=\"_blank\">New Window</a></div>");

setCentralWidget(m_web);
}

bool WebPage::acceptNavigationRequest(QWebFrame*, QNetworkRequest const& request, NavigationType) {
QDesktopServices::openUrl(request.url());
return false;
}

QWebView* WebView::createWindow(QWebPage::WebWindowType) {
auto res = new WebView;
auto page = new WebPage;
res->setPage(page);
return res;
}

int main(int argc, char *argv[]) {
QApplication a(argc, argv);

Window w;
w.show();

return a.exec();
}

测试.pro

QT += core gui network webkitwidgets widgets

TEMPLATE = app
TARGET = test
INCLUDEPATH += .

CONFIG += c++11

# Input
SOURCES += test.cpp
HEADERS += test.hpp

编译运行

qmake test.pro
make
./test

最佳答案

在外部浏览器中呈现页面后, View 似乎变得无用。您可能只是安排使用 deleteLater() 删除 ExternalWebView:

#include <iostream>

#include <QApplication>
#include <QGridLayout>
#include <QNetworkRequest>
#include <QDesktopServices>

#include <QEvent>
#include <QMainWindow>
#include <QWebView>

class ExternWebPage : public QWebPage {
//Q_OBJECT

public:
ExternWebPage(QObject* parent = 0)
: QWebPage(parent)
{
std::cout << "ExternWebPage" << std::endl;
}

~ExternWebPage() {
std::cout << "Destroy ExternWebPage" << std::endl;
}

virtual bool event(QEvent *e) {
static unsigned counter;
std::cout << ++counter << " ExternWebPage: " << e->type() << std::endl;
return QWebPage::event(e);
}

bool acceptNavigationRequest(QWebFrame *, const QNetworkRequest &request, NavigationType) {
QDesktopServices::openUrl(request.url());
return false;
}
};


class ExternWebView : public QWebView {
//Q_OBJECT

public:
ExternWebView(QWidget* parent = 0)
: QWebView(parent)
{
std::cout << "ExternWebView" << std::endl;
}
~ExternWebView() { std::cout << "Destroy ExternWebView" << std::endl; }
virtual bool event(QEvent *e) {
static unsigned counter;
std::cout << ++counter << " ExternWebView: " << e->type() << std::endl;
return QWebView::event(e);
}
};


class InternalWebView : public QWebView {
//Q_OBJECT

public:
InternalWebView(QWidget* parent = 0)
: QWebView(parent)
{}
QWebView* createWindow(QWebPage::WebWindowType) {
auto res = new ExternWebView();
res->setPage(new ExternWebPage(res));
res->deleteLater();
return res;
}
};


class Window : public QMainWindow {
//Q_OBJECT

public:
Window()
: QMainWindow()
{
std::cout << "Window" << std::endl;
auto web = new InternalWebView(this);
web->setHtml("<div align=\"center\"><a href=\"http://www.google.com/\">Same Window</a> <a href=\"http://www.google.com/\" target=\"_blank\">New Window</a></div>");
setCentralWidget(web);
}
~Window() { std::cout << "Destroy Window" << std::endl; }

};


int main(int argc, char *argv[]) {
QApplication a(argc, argv);
Window w;
w.show();
return a.exec();
}

不删除测试:

Window
ExternWebView
1 ExternWebView: 68
ExternWebPage
2 ExternWebView: 74
3 ExternWebView: 75
1 ExternWebPage: 43
2 ExternWebPage: 43
3 ExternWebPage: 43
4 ExternWebPage: 43
5 ExternWebPage: 43
Destroy Window

稍后删除测试:

ExternWebView
1 ExternWebView: 68
ExternWebPage
2 ExternWebView: 74
3 ExternWebView: 75
4 ExternWebView: 52
Destroy ExternWebView
Destroy ExternWebPage
Destroy Window

关于c++ - Qt:在默认浏览器中打开带有目标的链接,不会泄漏内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20308329/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com