QT实现简单的浏览器
话不多说,直接上代码
.h文件
#pragma once
#include <QtWidgets/QWidget>
#include <QtWebEngineWidgets/qwebengineview.h>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
class gugemap : public QWidget
{
Q_OBJECT
public:
gugemap(QWidget *parent = Q_NULLPTR);
~gugemap();
protected slots:
void onBtnClick();
private:
QWebEngineView *m_pwebView;
QPushButton *m_sechbtn;
QLabel *m_label;
QLineEdit *m_lineEdit;
};
.cpp文件
#include "gugemap.h"
#include <QtWebEngineWidgets/qwebengineview.h>
#include <QHBoxLayout>
gugemap::gugemap(QWidget *parent)
: QWidget(parent)
{
m_pwebView = new QWebEngineView(this);
QHBoxLayout *hLayout = new QHBoxLayout(this);
m_label = new QLabel(tr("Please Input Url"));
m_lineEdit = new QLineEdit(this);
m_sechbtn = new QPushButton(tr("Search"));
hLayout->addWidget(m_label);
hLayout->addWidget(m_lineEdit);
hLayout->addWidget(m_sechbtn);
hLayout->setMargin(0);
QWidget *wgt = new QWidget(this);
wgt->setLayout(hLayout);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(wgt);
mainLayout->addWidget(m_pwebView);
this->setLayout(mainLayout);
connect(m_sechbtn, &QPushButton::clicked, this, &gugemap::onBtnClick);
}
gugemap::~gugemap()
{
}
void gugemap::onBtnClick()
{
QString strUrl = m_lineEdit->text();
if (strUrl == "")
return;
m_pwebView->load(strUrl);
}
最终运行效果
觉得有用的,希望帮作者点个赞