QT += core gui webenginewidgets //窗口引擎部件
//显示原码的dialog
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
void setWebSource(QString); //给dialog设置文本
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::setWebSource(QString str)
{
//提供一个接口 把文本显示到dialog
ui->textEdit->setText(str);
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QWebEngineView> //web 引擎
#include <QUrl> //url
#include <QProgressBar> //进度条
#include <QFile> //加载配置文件
#include <QTextStream> //文本流
#include <QMessageBox>
#include <QDebug>
#include "dialog.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void setmainpage(); //设置默认主页
private slots:
void setUrlSlot(QUrl url);
void deleteprogress(bool);
void setTitle(QString);
void setMainpageslot();
void showcodeaction();//显示原码
private:
Ui::MainWindow *ui;
QWebEngineView* m_webView; //全局的
QProgressBar *progress;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
//mainpage= http://www.baidu.com
//QString mainpage="mainpoage=http://www.baidu.com"
//QStingList list = mainpage.split("=");
//list = mainpage http://www.baidu.com
//ui.webView.lod(QUrl(list.at(1))); =http://www.baidu.com
//配置文件 win .ini linux .conf
//在Debug 文件下 新建文件 mainpage.conf 在程序的运行目录
//QML qt Mark lauguage language 标记性语言 模块渲染界面
// ui 用户界面 ux 用户体验
// qml
/*
注册表相当于一个数据库
查看源代码 add 新文件 qt -> 设计师类界面 dialog
*/
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//创建QWebEngineView 对象
this->
m_webView = new QWebEngineView();
//把webview 放入ui ui的主窗口的布局添加窗口
ui->centralWidget->layout()->addWidget(m_webView);
setmainpage();
//实例化进度条
this->progress = new QProgressBar;
//添加到状态栏
ui->statusBar->addWidget(this->progress);
//连接信号
connect(ui->frowardButton, &QPushButton::clicked, this, [this]() {
QString url = ui->lineEdit->text();
if (!url.isEmpty())
{
m_webView->load(url);
}
});
//连接webview 进度数据 到进度条
connect(m_webView,SIGNAL(loadProgress(int)),this->progress,SLOT(setValue(int)));
//连接entry信号
connect(ui->lineEdit, &QLineEdit::returnPressed, this, [this]() {
QString url = ui->lineEdit->text();
if (!url.isEmpty())
{
m_webView->load(url);
}
});
//调用Webview 的槽函数
connect(ui->frowardButton,SIGNAL(clicked(bool)),m_webView,SLOT(forward()));
connect(ui->back,SIGNAL(clicked(bool)),m_webView,SLOT(back()));
connect(ui->refreshButton,SIGNAL(clicked(bool)),m_webView,SLOT(reload()));
connect(m_webView,SIGNAL(urlChanged(QUrl)),this,SLOT(setUrlSlot(QUrl)));
//连接webView 加载完成信号 删除进度条
connect(m_webView,SIGNAL(loadFinished(bool)),this,SLOT(deleteprogress(bool)));
//设置标题
connect(m_webView,SIGNAL(titleChanged(QString)),this,SLOT(setTitle(QString)));
//设置主页
connect(ui->action_M,SIGNAL(triggered(bool)),this,SLOT(setMainpageslot()));
connect(ui->action_S,SIGNAL(triggered(bool)),this,SLOT(showcodeaction()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setmainpage()
{
QString filename = "mainpage.conf"; //文件路径
QFile *file = new QFile; //文件对象
file->setFileName(filename); //设置文件名
bool ok = file->open(QIODevice::ReadOnly); //打开方式
QTextStream in(file);
if(ok)
{
//把内容读入 输入框
m_webView->load(QUrl(in.readLine().split("=").at(1)));
file->close();
delete file;
file = NULL;
}else
{
QMessageBox::information(this,"Error Message","openfile Error"+file->errorString());
}
}
//获取地址
void MainWindow::setUrlSlot(QUrl url)
{
ui->lineEdit->setText(url.toString());
}
//删除进度条
void MainWindow::deleteprogress(bool ok)
{
if(ok)
{
delete this->progress;
this->progress = NULL;
this->ui->statusBar->showMessage("Load finish",5*1000);
}
}
void MainWindow::setTitle(QString title)
{
this->setWindowTitle(title);
}
void MainWindow::setMainpageslot()
{
QString filename = "mainpage.conf"; //文件路径
QFile *file = new QFile; //文件对象
file->setFileName(filename); //设置文件名
bool ok = file->open(QIODevice::WriteOnly); //打开方式
if(ok)
{
QTextStream out(file);
//把内容读入 输入框
out<<"mainpage="+ui->lineEdit->text();
file->close();
delete file;
file = NULL;
QMessageBox::information(this,"Information","setmain page sucess");
}else
{
qDebug()<<"set mainpage error";
QMessageBox::information(this,"Error Message","openfile Error"+file->errorString());
}
}
//查看原代码 没有实现
void MainWindow::showcodeaction()
{
QString content=m_webView->page()->selectedText();
Dialog *dialg=new Dialog;//Dialog
dialg->setWebSource(content);
dialg->show();
}