-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
59 lines (53 loc) · 2.77 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "mainwindow.h"
#include "./ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
regExp.setPattern("(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]");
v.setPage(&p);
QObject::connect(ui->lineEdit_inputurl, &QLineEdit::textChanged, this, [&](const QString &text) {
url = text;
});
QObject::connect(ui->pushButton_save, &QPushButton::clicked, this,
[&]() {
QUrl qurl = QUrl::fromUserInput(url);
qDebug() << "url: " << url << " qurl: " << qurl << " isValid: " << regExp.match(url).hasMatch();
if (url.isEmpty() || !regExp.match(url).hasMatch()) {
QMessageBox::warning(this,
"错误",
"网址无效,请检查输入: " + url,
QMessageBox::Close,
QMessageBox::Close);
return;
}
QString filePath = QFileDialog::getSaveFileName(this, "选择保存路径", "/", "文本文件(*.txt);; All(*.*);; ");
if (filePath.isEmpty()) {
QMessageBox::warning(this,
"错误",
"未选择正确的保存路径",
QMessageBox::Close,
QMessageBox::Close);
return;
}
QFileInfo fileInfo(filePath);
filePath = fileInfo.absoluteFilePath();
if (fileInfo.isDir()) {
filePath += "/urls.txt";
} else if (fileInfo.suffix() != "txt") {
filePath += ".txt";
}
qDebug() << "filePath: " << filePath;
p.profile()->setUrlRequestInterceptor(new WebEngineUrlRequestInterceptor(filePath));
v.setUrl(qurl);
v.show();
});
QObject::connect(&p, &QWebEnginePage::newWindowRequested, this, [&](QWebEngineNewWindowRequest &request) {
v.setUrl(request.requestedUrl());
});
/*QObject::connect(&p, &QWebEnginePage::windowCloseRequested,this, [&]() {
qDebug() << "got close signal";
});*/
}
MainWindow::~MainWindow() {
delete ui;
}