我正在尝试使用QWebEngineView创建一个窗口,并在窗口关闭后删除QWebEngineView。然而,QWebEngineView似乎从未被删除,并且一直运行我加载的任何QUrl (例如YouTube视频)。在我的程序中,我有一个带有行编辑和Push Button的QMainWindow,它创建了一个窗口来加载用户输入的URL。这是我的代码:
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "subwindow.h"
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
void init();
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
SubWindow *sub;
};
#endif // MAINWINDOW_HSubWindow.h
#ifndef SUBWINDOW_H
#define SUBWINDOW_H
#include <QMainWindow>
#include <QtWebEngineWidgets/qwebengineview.h>
#include <QTimer>
namespace Ui
{
class SubWindow;
}
class SubWindow : public QMainWindow
{
Q_OBJECT
public:
explicit SubWindow(QWidget *parent = 0);
void doWeb(QString link);
~SubWindow();
private:
Ui::SubWindow *ui;
QWebEngineView *web;
private slots:
void on_pushButton_clicked();
};
#endif // SUBWINDOW_HMainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
void MainWindow::init()
{
this->showMaximized();
sub = new SubWindow(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
sub->doWeb(ui->lineEdit->text());
}SubWindow.cpp
#include "subwindow.h"
#include "ui_subwindow.h"
SubWindow::SubWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::SubWindow)
{
ui->setupUi(this);
}
void SubWindow::doWeb(QString link)
{
this->show();
web = new QWebEngineView(this);
ui->verticalLayout->addWidget(web);
web->load(QUrl(link, QUrl::TolerantMode));
web->show();
}
SubWindow::~SubWindow()
{
delete web; //Doesn't seem to work, since memory is still allocated in task manager
delete ui;
}
void SubWindow::on_pushButton_clicked()
{
this->close(); //Artifact for testing purposes.
}main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.init();
return a.exec();
}我还尝试使用“web->close()”(我将其放在具有测试工件的行中)。
"web->setAttribute(Qt::WA_DeleteOnClose);“(我将其放在"web = new (This);”之后),但这也解决不了我的问题。有人知道我做错了什么吗?
发布于 2017-02-27 08:34:19
您正在创建带有父"this“(所有者)的QWebEngineView,这样您就不必自己删除它,当它的父方愿意时,它将被自动删除。请参阅有关内存管理的Qt文档:
http://doc.qt.io/qt-5/objecttrees.html
这样做也很危险,因为如果不使用QWebEngineView (QString链接)创建QString对象,您将尝试删除一些不存在的内容,因此它会导致您出现未定义的行为。
从析构函数中删除delete web,看看是否总是有问题。
编辑:没有销毁它的原因:您没有销毁SubWindow对象( QWebEngineView对象的所有者)。您可以通过在析构函数中打印某些内容来验证它。如果您正在销毁SubWindow对象,第二次按MainWindow中的按钮时,您的应用程序将崩溃。因为您不是在函数SubWindow中创建on_push_button对象,而是在MainWindow::init中创建对象。您的SubWindow对象只是隐藏的,而不是销毁的。只有当您关闭MainWindow时,它才被销毁。您还将在每次显示sub ( QWebEngineview对象)时创建一个QWebEngineView实例,因此,如果您显示Sub3次,那么在您的Subwindow中将有3个QWebEngineView。
我将对代码所做的更改:
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}mainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "subwindow.h"
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
SubWindow* sub;
};
#endifmainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow), sub(new SubWindow(this))
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
sub->show();
sub->doWeb(ui->lineEdit->text());
}子.h
#ifndef SUBWINDOW_H
#define SUBWINDOW_H
#include <QMainWindow>
#include <QtWebEngineWidgets/qwebengineview.h>
#include <QTimer>
namespace Ui
{
class SubWindow;
}
class SubWindow : public QMainWindow
{
Q_OBJECT
public:
explicit SubWindow(QWidget *parent = 0);
void doWeb(QString link);
~SubWindow();
private:
Ui::SubWindow *ui;
QWebEngineView *web;
private slots:
void on_pushButton_clicked();
};
#endif // SUBWINDOW_HsubWindow.cpp
#include "subwindow.h"
#include "ui_subwindow.h"
SubWindow::SubWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::SubWindow),
web(new QWebEngineView(this))
{
ui->setupUi(this);
ui->verticalLayout->addWidget(web);
}
void SubWindow::doWeb(QString link)
{
web->load(QUrl(link, QUrl::TolerantMode));
}
SubWindow::~SubWindow()
{
delete ui;
}
void SubWindow::on_pushButton_clicked()
{
this->close(); //Artifact for testing purposes.
}请注意,我也没有销毁subWindow对象,但每次调用SubWindow::doWeb(String)方法时都不会创建QWebView。如果由我决定,我将使用一个不是MainWindow成员的子类对话框,该对象将在MainWindow::on_pushButton_clicked()中创建,并在我使用完之后销毁。
发布于 2017-02-26 03:58:06
好的,在调用"this->close();“之前,我显然需要调用”web->删除close()“。“删除网页;”未被删除。不过,我还是想知道为什么删除在这种情况下不起作用.
发布于 2020-10-28 15:23:22
好像有什么东西关错了。
我使用QT5.15使用工具包MSVC2019 32位编译器。但是我的计算机上安装了Visual 2017 (而不是2019年)。因此,Qt创建者->项目->管理工具包->编译器中检测到的编译器是MSVC2017的编译器,即: Microsoft C++编译器15.8 (x86)。
解决方案是安装VisualStudio2019,然后用正确的编译器( 2019 )替换Qt创建者,即: Microsoft c++编译器16.7 (X86)。这样我就可以在没有任何内存泄漏的情况下进行编译。

https://stackoverflow.com/questions/42463581
复制相似问题