webpopupwindow.cpp

本文介绍了一个自定义Web弹窗类WebPopupWindow的实现方法,该类利用QWebEngineProfile配置文件,通过UrlLineEdit和WebView组件展示网页内容,并实现了网址栏、标题及图标更新等功能。
#include "urllineedit.h"
#include "webpage.h"
#include "webpopupwindow.h"
#include "webview.h"
#include <QIcon>
#include <QVBoxLayout>

WebPopupWindow::WebPopupWindow(QWebEngineProfile *profile)
    : m_addressBar(new UrlLineEdit(this))
    , m_view(new WebView(this))
{
    setAttribute(Qt::WA_DeleteOnClose);
    setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);

    QVBoxLayout *layout = new QVBoxLayout;
    layout->setMargin(0);
    setLayout(layout);
    layout->addWidget(m_addressBar);
    layout->addWidget(m_view);

    m_view->setPage(new WebPage(profile, m_view));
    m_view->setFocus();
    m_addressBar->setReadOnly(true);
    m_addressBar->setFavIcon(QIcon(QStringLiteral(":defaulticon.png")));

    connect(m_view, &WebView::titleChanged, this, &QWidget::setWindowTitle);
    connect(m_view, &WebView::urlChanged, this, &WebPopupWindow::setUrl);
    connect(m_view, &WebView::iconChanged, this, &WebPopupWindow::handleIconChanged);
    connect(m_view->page(), &WebPage::geometryChangeRequested, this, &WebPopupWindow::handleGeometryChangeRequested);
    connect(m_view->page(), &WebPage::windowCloseRequested, this, &QWidget::close);
}

QWebEngineView *WebPopupWindow::view() const
{
    return m_view;
}

void WebPopupWindow::setUrl(const QUrl &url)
{
    m_addressBar->setUrl(url);
}

void WebPopupWindow::handleGeometryChangeRequested(const QRect &newGeometry)
{
    m_view->setMinimumSize(newGeometry.width(), newGeometry.height());
    move(newGeometry.topLeft() - m_view->pos());
    // let the layout do the magic
    resize(0, 0);
    show();
}

void WebPopupWindow::handleIconChanged(const QIcon &icon)
{
    m_addressBar->setFavIcon(icon);
}
代码概述 你在执行 `make` 命令时遇到了链接错误:`undefined reference to 'Channel::~Channel()'`,这表示链接器找不到 `Channel` 类的析构函数实现。 代码解析 你当前执行的编译命令如下: ```bash g++ Src/util.cpp Src/Epoll.cpp Src/InetAddress.cpp \ Src/Socket.cpp Src/Channel.cpp Src/EventLoop.cpp \ Src/Server.cpp Src/Acceptor.cpp server.cpp -o server ``` ### 错误分析: ``` undefined reference to `Channel::~Channel()' ``` 这个错误说明: - `Acceptor.cpp` 中使用了 `Channel` 类(可能通过 `delete acceptChannel` 或其它方式); - 但在链接阶段,链接器找不到 `Channel` 类的析构函数实现; - 通常是因为 `Channel.cpp` 中**没有实现析构函数**或**未正确编译进最终程序**。 ### 可能原因: 1. **`Channel.cpp` 中未定义析构函数** - 如果你在 `Channel.h` 中声明了虚析构函数(例如 `virtual ~Channel()`),但 `Channel.cpp` 中没有实现它,就会报这个错。 2. **`Channel.cpp` 没有参与编译** - 虽然你命令中包含了 `Channel.cpp`,但请确认拼写是否正确,文件是否真实存在。 --- ### 修复方法: #### ✅ 1. 确保 `Channel.cpp` 中实现了析构函数 如果你的 `Channel.h` 是这样定义的: ```cpp class Channel { public: virtual ~Channel(); // 虚析构函数 }; ``` 那你需要在 `Channel.cpp` 中实现: ```cpp Channel::~Channel() { // 可以留空 } ``` 或者如果你使用的是纯虚析构函数,也要提供一个实现(即使为空): ```cpp Channel::~Channel() {} ``` #### ✅ 2. 确保 `Channel.cpp` 正确参与编译 确认你的编译命令或 `Makefile` 包含了 `Channel.cpp`,你当前命令是包含的,因此没有问题。 --- ### 推荐修改后的 Makefile 示例: ```makefile server: g++ Src/util.cpp Src/Epoll.cpp Src/InetAddress.cpp \ Src/Socket.cpp Src/Channel.cpp Src/EventLoop.cpp \ Src/Server.cpp Src/Acceptor.cpp server.cpp -o server clean: rm -f server ``` --- 知识点(列出该代码中遇到的知识点) 1. **析构函数实现缺失**:如果类定义了析构函数但未实现,链接时会报 `undefined reference`。 2. **虚析构函数作用**:确保多态删除基类指针时能正确调用派生类析构函数。 3. **链接器行为**:链接阶段会检查所有函数符号是否完整,若未实现则报错。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值